Problem: read block of bytes from socket.

G

grouzen hexy

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?
 
J

James Kuyper

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

Lew Pitcher

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).
 
S

Siri Cruise

grouzen hexy said:
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.
 
M

Michael Angelo Ravera

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top