I can't use CArchive as I said. As far as I know, Serialize() saves various CObject-relative information, and could be used only if client is MFC based.
Am I wrong?
Main Topics
Browse All TopicsI need to make a TCP/IP server, and naturally have decided to use CSocket class.
Need to receive a stream of characters of unknown format and length from a non-MFC client.
I use OnReceive() event to catch the arrival of data, and something like while (CAsyncSocket::Receive()) loop to receive the message, one char in every iteration. But Receive seems to be blocking!
How can I make it non-blocking, or find some other way of receiving whole message and exit the loop?
BTW, can't use CArchive-CFile, because Serialize() doesn´t work with non-MFC data...
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The default behaviour is to block until data is received.
Note that unless you decide on a protocol for messages you receive, you have no way of knowing you've reached the "end" of a particular method. That's the nature of TCP/IP -- you're guaranteed to receive everything sent over the socket, in the order sent, but otherwise there are no inherent "message" boundaries. While a client may "write" 2048 bytes, you may not necessarily read them in a single chunk, depending on the network configuration between hosts.
One common, simple message format is to send a fixed number of bytes to indicate the length of the following message. That way, after you've read the length, you know exactly how many more bytes to read to get the complete message.
"I use OnReceive() event to catch the arrival of data, and something like while (CAsyncSocket::Receive()) "
Why don't you 'query' the socket to find out how many bytes of data are available for you to read and read only those many ?
Whenever there is arrival of data, OnReceive will be called. If you manage to read all the data that is available during a single OnReceive, you will keep getting further notifications of data arrival , till the time the client continues to transmit the data or shutsdown the virtual circuit.
You can try the following code:
void MySock::OnReceive(int nErrorCode)
{
int ret_ioctl;
unsigned long no_of_bytes_readable;
ret_ioctl=ioctlsocket ( m_hSocket,
,FIONREAD,
&no_of_bytes_readable);
if (ret_ioctl != 0)
{
//an error occurred, retrieve the error code
return(WSAGetLastError());
}
else
{
//go ahead and read 'no_of_bytes_readable' number of bytes only,
char *buffer = ::new char[no_of_bytes_readable]
recv (m_hSocket,buffer,no_of_by
}
}
A good strategy would be to maintain a character buffer member variable, that would get incrementally populated every time OnReceive would be called.
Business Accounts
Answer for Membership
by: danny_pavPosted on 2000-06-02 at 14:46:17ID: 2883285
use the CArchive::IsBufferEmpty to determine when you have reached the end of your data