Archived

This topic is now archived and is closed to further replies.

Sending large amounts of data with Async. Sockets

This topic is 5149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a server and a client using Asynchronous Sockets with Winsock - i.e. I use WSAAsyncSelect on my sockets and handling the messages received in the WndProc, such as FD_READ, etc. Now, receiving data is no problem. I just read the data into a buffer until I have the whole "packet" of information and then process it. I know that in TCP there really aren't any "packets", but I have created a system in which the continuous data stream is divided up into packets, so I can send a move packet or a "chat" packet, if you know what I mean. Anyway, with the sending of data, all I am doing is calling send() and letting the built-in winsock send buffer handle everything. However, this buffer is limited. Let's say I wanted to have my own buffer, perhaps a vector<char> which would be able to grow enough to handle any size packet. My problem is that I can't use FD_WRITE, since FD_WRITE gets called whenever data can be sent, such as when you first connect, and then blocks all messages until a send() is done - but what if I have nothing to send? Will it be a problem if I just don't send anything? MSDN seems to imply that you always have to have something to send. Is this true? So let's say I post a 1000000 byte packet to my own send buffer. Then I want to send data 1000 bytes at a time. Will this work with FD_WRITE? How can I do this? [edited by - Tron3k on November 9, 2003 3:09:23 PM]

Share this post


Link to post
Share on other sites
I stopped using the WSAAsyncSelect function quite a few years ago but I don''t remember anything about messages being blocked until you send some data (?) The messages are simply a notification of when the status of your socket changes. Therefore it is true that you will only receive the FD_WRITE notification once, until you send data. If you envelop your socket in a class you can simply set a flag in the class when the notification is received.

If you have 1000000 bytes and a 1000 buffer, simply send the contents of the buffer each time you get the notification. Once you send one buffer and the socket is ready to send more you will get the FD_WRITE notification again. Keep doing this until you have sent your 1000000 bytes.



"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan

Share this post


Link to post
Share on other sites