Problem: read block of bytes from socket.

Discussion in 'C Programming' started by grouzen hexy, Jul 9, 2013.

  1. grouzen hexy

    grouzen hexy Guest

    Hi all!

    I'm new here, but I'm stuck with a problem.
    I'm trying to write xmpp library, and I need to read various xml stanzas from my socket stream. But unfortunately xmpp server doesn't send any \n, eof, etc, so I couldn't determine where the message end.

    I used usocket library for support of majority of CL implementations, and I found that there is no function to receive(recv in C) number of bytes from the TCP socket, there is only for UDP.

    So, could you please help me with this problem?
    grouzen hexy, Jul 9, 2013
    #1
    1. Advertising

  2. grouzen hexy

    James Kuyper Guest

    On 07/09/2013 03:12 PM, grouzen hexy wrote:
    > Hi all!
    >
    > I'm new here, but I'm stuck with a problem.
    > I'm trying to write xmpp library, and I need to read various xml stanzas from my socket stream. But unfortunately xmpp server doesn't send any \n, eof, etc, so I couldn't determine where the message end.
    >
    > I used usocket library for support of majority of CL implementations, and I found that there is no function to receive(recv in C) number of bytes from the TCP socket, there is only for UDP.
    >
    > So, could you please help me with this problem?


    C does not provide any language-level support for TCP or UDP or any
    other network interface, so this is not a good place to ask your
    question. It is about a specific library that does provide such support,
    and you'll get the best answers if you choose a forum for your question
    that's specific to that library.
    James Kuyper, Jul 9, 2013
    #2
    1. Advertising

  3. grouzen hexy

    Lew Pitcher Guest

    On Tuesday 09 July 2013 15:25, in comp.lang.c,
    wrote:

    > On 07/09/2013 03:12 PM, grouzen hexy wrote:
    >> Hi all!
    >>
    >> I'm new here, but I'm stuck with a problem.
    >> I'm trying to write xmpp library, and I need to read various xml stanzas
    >> from my socket stream. But unfortunately xmpp server doesn't send any \n,
    >> eof, etc, so I couldn't determine where the message end.
    >>
    >> I used usocket library for support of majority of CL implementations, and
    >> I found that there is no function to receive(recv in C) number of bytes
    >> from the TCP socket, there is only for UDP.
    >>
    >> So, could you please help me with this problem?

    >
    > C does not provide any language-level support for TCP or UDP or any
    > other network interface, so this is not a good place to ask your
    > question. It is about a specific library that does provide such support,
    > and you'll get the best answers if you choose a forum for your question
    > that's specific to that library.


    What he said.


    In addition, there is no provision in the TCP protocol for the prior
    establishment of the length of a TCP stream. In other words, you are
    looking for something that doesn't exist in the TCP protocol.

    What you need is to read and interpret the data sent in the TCP stream, in
    order to determine the message end. Look for EOF (which typically signals
    that the sender has closed their outgoing connection). Look for the logical
    end of the data (for instance, a terminating "?>" that matches the
    initial "<?xml"). Look for length information within the message (I don't
    know XMPP, but there might be an XML entity that gives the length of the
    message).

    --
    Lew Pitcher
    "In Skills, We Trust"
    Lew Pitcher, Jul 9, 2013
    #3
  4. grouzen hexy

    Siri Cruise Guest

    In article <>,
    grouzen hexy <> wrote:

    > Hi all!
    >
    > I'm new here, but I'm stuck with a problem.
    > I'm trying to write xmpp library, and I need to read various xml stanzas from
    > my socket stream. But unfortunately xmpp server doesn't send any \n, eof,
    > etc, so I couldn't determine where the message end.
    >
    > I used usocket library for support of majority of CL implementations, and I
    > found that there is no function to receive(recv in C) number of bytes from
    > the TCP socket, there is only for UDP.
    >
    > So, could you please help me with this problem?


    You can call fgetc n times to read n bytes, or fread to read n bytes. On Unix
    and some other systems you can use nonstandard read to read n bytes bypassing
    buffers.

    You have to agree with whoever is sending you the stream on the syntax of the
    stream. I don't know what xmpp, so I would look for some kind of definition
    document and study that. You also have to agree when you switch reading and
    writing and ensure you get bufferred data properly flushed.
    --
    Mommy is giving the world some kind of bird.
    :-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
    NSA CIA Constitution patriot terrorism freedom Snowden Paid Maternity Leave
    Siri Cruise, Jul 10, 2013
    #4
  5. On Tuesday, July 9, 2013 12:25:04 PM UTC-7, James Kuyper wrote:
    > On 07/09/2013 03:12 PM, grouzen hexy wrote:
    >
    > > Hi all!

    >
    > >

    >
    > > I'm new here, but I'm stuck with a problem.

    >
    > > I'm trying to write xmpp library, and I need to read various xml stanzas from my socket stream. But unfortunately xmpp server doesn't send any \n, eof, etc, so I couldn't determine where the message end.

    >
    > >

    >
    > > I used usocket library for support of majority of CL implementations, and I found that there is no function to receive(recv in C) number of bytes from the TCP socket, there is only for UDP.


    Others have rightly told you that you are technically off topic.
    recv does work for TCP, but you have to know how many bytes to read. I haven't looked at the xmpp spec, but you have either to
    1) read the length word (if provided by xmpp) to determine how much to recv and then recv that amount.
    2) recv a single byte at a time until you reach your delimiter
    3) recv in buffered blocks and return information of interest up to a delimiter

    It sounds like you would want to write a C function to do any of the 3 of these things as required.
    Michael Angelo Ravera, Jul 11, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. morrell
    Replies:
    1
    Views:
    946
    roy axenov
    Oct 10, 2006
  2. Laszlo Nagy
    Replies:
    1
    Views:
    4,808
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    960
    Jean-Paul Calderone
    Jan 27, 2009
  4. Ashwin Rao
    Replies:
    2
    Views:
    1,046
    Martien Verbruggen
    Dec 3, 2009
  5. Replies:
    19
    Views:
    1,268
    Nigel Wade
    Mar 15, 2012
Loading...

Share This Page