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. Advertisements

  2. grouzen hexy

    James Kuyper Guest

    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
    1. Advertisements

  3. grouzen hexy

    Lew Pitcher Guest

    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
    Lew Pitcher, Jul 9, 2013
  4. grouzen hexy

    Siri Cruise Guest

    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

    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.
    Siri Cruise, Jul 10, 2013
  5. 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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.