First rule of network programming: DO NOT use CSocket
Second rule of network programming: if you think you want CSocket, consult the First Rule.
Do no ever consider CSocket as a viable means of writing network programs. CAsyncSocket
is the only working mechanism. CSocket as a paradigm sucks, and besides, the MFC
implementation has bugs. Avoid it. There is no point in trying to "fix" a program that
uses CSocket, except by rewriting it to use CAsyncSocket.
Post by SakthiHai,
I am having problem with OnReceive notification of my own class derived
from CSocket. I created a thread, and attaches many client sockets to the
thread (accepting clients in another thread, and posting a message to this
thread to attach). Now, my OnReceive notification, has not always called.
Some times, it is called, and some times, I dont know, why it is not called.
Can anybody answer?
Hi, Sakthi
I think I also got problem abount CSocket OnReceive not always trigger..
Could you advise me about the solution ?
How to program "use only one receive() inside OnReceive()." ??
I had set receive timeout for 5 sec. and prepare a large memory for receive()
to
int timeout = 5000;
SetSockOpt(SO_RCVTIMEO, &timeout, sizeof(timeout));
TCHAR msg[409600];
int nRead;
nRead = Receive(msg, 409600);
****
First, it is not clear why you repeated the number twice. Have you ever heard of #define
or const UINT?
Second, the chances of ever receiving more than 1536 bytes is zero. That is the size of
an Ethernet packet. But you might receive fewer bytes. There is no reason you should
expect the Receive, as written, to work.
****
but the first receive also not the all data ,and i need to call Receive()
again
& again to complete.
****
Yes, this is how you program sockets. There is NO GUARANTEE that a Receive will EVER
receive what has been sent in a single operation. The only guarantee is that you will
receive all the bytes, in order, with no duplicates, or both the sender and receiver will
get an error message.
Note that the number of times you need to call Receive is undefined; it is up to you to
determine when you have read all the data. This is why most protocols include a count of
the transmission size. Note that the count, if sent as a multibyte field, could be split
between two Receive calls, where one receives the first part of the field and another
Receive receives the rest of the field.
There is no way to change this behavior, since it is built in to how network programming
works. If you expect to send 409600 bytes, you might expect to execute at least 266
Receive calls to receive it, perhaps a lot more.
This will not change if you go to CAsyncSocket, except that now you will understand better
why this can happen.
****
Couldn't be "use only one receive() inside OnReceive()."
****
Not in this world. I'm not even sure why you would expect to be able to do this, given
how sockets are defined to work. You DID read about socket programming, didn't you?
WHat you described is not a "problem" it is "the way sockets work".
But throw out CSocket. It is a poor choice of mechanism. NEVER use CSocket. Or
synchronous sockets.
joe
****
tnaks for any advise..
Best Regards
Jackie Hsu
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm