recv() not returning

hey guys,

i have a loop for recieving data like this:

1
2
3
4
while((length = recv(main_socket, rcv_buffer, BUF_SIZE-1, 0))>0){
			cout<<rcv_buffer;
			memset(rcv_buffer,0,BUF_SIZE);
		}


the thing is that if all the data are already recieved, recv() is blocking even further so it actually doesn't return 0 if there is nothing from the server.
I don't know what amount of data is supposed to be recieved so i need to do it bit by bit. does anyone know about better and functional solution?
If this is a TCP socket (I suspect it is), then you have the classic framing problem.
TCP is stream-oriented. It sounds like what you want to do is make it datagram-
oriented.

You either have to know ahead of time the amount of data you want to receive
or you have to provide a "sentinel" of sorts in the data stream that signifies
you've read everything.
when the other side will close the recv will get a 0 and the loop will end. otherwise the recv will be blocked.
when you are receiving the data you need to device a way to know how much data will be coming. you can have the following simple ways:
1. send a data header in the beginning so that you can know how much data will come which can be received at one go or in packets.
2. when all the data is received the other side should send some hint like an empty string or some sequence of characters which confirm that all the data is finished now and you can break the loop.
yep it's TCP
you mean like... ending sequence for each message? well yea that seems to be a solution... thx a lot man
Topic archived. No new replies allowed.