index > Winsock Kernel (WSK) > Winsock2 API - receiving data

Winsock2 API - receiving data


Hi, all!

I know it is a simple question, but I assure that have searched in maaaany places for an answer for it and haven't found yet. I've looked even at HTTP RFC and Transmission Control Protocol RFC, but didn't find at first sight, not so deep.

I also don't know if here at WSK Forum is the real place to take this question, but I didn't find any better.

I just want to know how can I know if one side of the connection has ended up sending data. I am trying to connect to an IIS webserver, for example. I am the client and send to the server any GET command. Then, I enter in a loop of recv(), like this:

char buffer[1024];
int bytesRecv=0, totalBytes=0;
while(bytesRecv!=SOCKET_ERROR) {
bytesRecv=recv(ConnectSocket, buffer, 1024, 0);
if(bytesRecv!=SOCKET_ERROR) totalBytes+=bytesRecv;
if(bytesRecv==0) {printf("bytes received: %d\n", totalBytes);break;}
}

When the server sends the last data, then the recv returns a number different of 1024 (400, for instance), and the loop take the program to another call of recv(), that stays stopped, waiting for more data... And my program stays locked.

Of course that I've already tried an IF inside of the loop that tests if the bytesRecv is different from 1024 to break the loop. Theorically, this must be the last data received. But I have two problems: if the transaction was perfect, with no errors, than I could find someday one transaction that has a data of 1024*x size, with x an integer. Than the LAST call of recv would return 1024, and would not be TRUE with my IF test. And also... The transactions aren't perfect. My first "IF test" receiving 80kB from a webserver broke the loop at the first 12kB of data. Because at this point, my recv returned less than 1024 bytes... And was not the end of the transaction.

The example code in MSDN doesn't have a loop of recv(), because it's just... Too simple! Just take a buffer larger than the data, than make ONE call to the recv() function. But the common case is when the buffer is smaller than the data. And I just don't find an example of it! The various examples that I found in the web that shows buffers smaller than the data uses a convention created by the owners of the examples... And all conventions sends the first data how many bytes will come in the transaction. But that's still not the common case of the HTTP servers, or the FTP servers, or the POP servers, ....

Can somebody help me with that?

Gratefully,
Thales Medeiros.

Thales Medeiros

A quote from MSDN:

"For connection-oriented sockets (type SOCK_STREAM for example), calling recv will return as much data as is currently available??p to the size of the buffer specified.

"

So don't assume that only the last packet is smaller than 1024 bytes. Other packets you receive cand be smaller too.

You say:

"...from a webserver broke the loop at the first 12kB of data. Because at this point, my recv returned less than 1024 bytes..."

but I don't see any check for < 1024 bytes. You just check for SOCKET_ERROR and 0 bytes receive which is fine.

"And all conventions sends the first data how many bytes will come in the transaction"

It depends. HTTP may send the content length header but usually only for static content. That's why when you download something using HTTP you get a progress dialog of some sort. FTP uses 2 connections, one is the command connection where commands are separated with CR LF characters and the other one is the data connection which is opened when you download a file and closed at the end of the download (so recv should return 0). Also you can get file size information from commands like ls/dir. If I remember correctly POP3 tells you the size of the message when you do a LIST command or something similar. It has to do this because you can retreive more than one mail using the same connection so you cannot relay on the fact that the end of the mail is when the connection is closed.

Mike Danes

You said:

"So don't assume that only the last packet is smaller than 1024 bytes. Other packets you receive cand be smaller too."

I know that! It's written in my POST. I've said:

"The transactions aren't perfect. My first "IF test" receiving 80kB from a webserver broke the loop at the first 12kB of data. Because at this point, my recv returned less than 1024 bytes..."

That proves that I know that recv may return a number smaller than the size of the buffer. I've already tested it.

"but I don't see any check for < 1024 bytes. You just check for SOCKET_ERROR and 0 bytes receive which is fine."

I said in my POST:

"Of course that I've already tried an IF inside of the loop that tests if the bytesRecv is different from 1024 to break the loop. "

It's not written in the code I've pasted, but that's just for not posting a "complex" code here. No one would help me. I have just pasted the simplest thing that I'm trying to do.

"It depends. HTTP may send the content length header but usually only for static content. Etc., etc., etc..."

I know that all. I am just trying to solve the problem when I DON'T know the size of the coming data. Do you know how to solve that?

Thanks for the attention... I still hope there's a way... :)

Sincerely,
Thales Medeiros.

Thales Medeiros

Hmm... seems like I was a bit dizzy when reading the post but anyway... let's see if we can find the solution

recv returns

  • SOCKET_ERROR when there is an error, nothing special here
  • 0, that means that the server closed the connection so there is no more data to read
  • > 0, some data was received, maybe the number of bytes that you specified in the recv call or maybe less

If you only use the result of recv the only way to know the size of the data is when the result is 0. Server tells "ok, that was all the data I had to send".

Since I understand that you want to connect to IIS then you should read this section from the HTTP RFC:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4

If this does not answer your question then it means I don't understand your question...

And indeed, this is not the appropiate forum for this question...

Perhaps you can try the MSDN newsgroups:

http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.win32.programmer.networks&lang=en&cr=US

Mike Danes
reply 4

You can use google to search for other answers

 

More Articles

• HTTP Keep Alive and Local Port
• Trying to use Winsock control in Microsoft Access Database Form
• winsock app works on win 2k server but _slow_ on win 2003
• UDP client and server application program on the same machine in ...
• Communicating with/from VLAN machines - Issues
• Enable double ip addresses
• Problem with UDP
• InternetOpen with specific IP address when Client machine has mul...
• Windows Core Networking Online Chat today at 11AM PST
• KeepAlive (Keep Alive) not working on Win2003 Enterprise 64 bit E...
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

• winsock app works on win 2k server but _
• Need to know the IP address and mask in
• KeepAlive (Keep Alive) not working on Wi
• Keepalive Timeout Detection
• Problem with UDP
• UDP client and server application progra
• Why can't maximum length of the queue of
• InternetOpen with specific IP address wh
• WSK and Game Development
• Trying to use Winsock control in Microso
• Windows kernel mode ARP
• Identifying WiFi Access Points
• Communicating with/from VLAN machines -
• Windows Core Networking Online Chat toda
• New to Socket Programming

Hot Articles

• Communicating with/from VLAN machines -
• Identifying WiFi Access Points
• WSK and Game Development
• UDP client and server application progra
• Need to know the IP address and mask in
• Windows Core Networking Online Chat toda
• InternetOpen with specific IP address wh
• Keepalive Timeout Detection
• Why can't maximum length of the queue of
• Windows kernel mode ARP
• HTTP Keep Alive and Local Port
• Trying to use Winsock control in Microso
• Problem with UDP
• Enable double ip addresses
• New to Socket Programming

Recommend Articles

• New to Socket Programming
• Need to know the IP address and mask in
• InternetOpen with specific IP address wh
• Problem with UDP
• Communicating with/from VLAN machines -
• Trying to use Winsock control in Microso
• Windows kernel mode ARP
• Why can't maximum length of the queue of
• winsock app works on win 2k server but _
• HTTP Keep Alive and Local Port
• Identifying WiFi Access Points
• Keepalive Timeout Detection
• WSK and Game Development
• Windows Core Networking Online Chat toda
• Enable double ip addresses