472,119 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Asynchronous socket EndReceive method not returning correct number of bytes?

Hello,
I am running into a problem with Asynchronous socket communication. I
am developing a simple telnet client to interact with unix. I am
parsing received data for prompts and sending data on found. Problem
is, that sometimes EndReceive returns only a few bytes, much less than
what is in the buffer. Since I am parsing what is in the buffer up to
how large bytes received is, I end up skipping data. Odd thing is
that it works 1/2 the time and fails the rest. Here is some of my
code calls:

to initialize:
s.BeginConnect(ipep,callBackProc,s);

in callBackProc:
AsyncCallback receive = new AsyncCallback(OnReceivedData);

s1.BeginReceive(buffer,0,buffer.Length,SocketFlags .None,receive,s1);

in OnReceivedData:
nBytes = s1.EndReceive(ar); //here sometimes nBytes is X and number of
elements in buffer is Y where X<Y.

also in OnReceivedData to send such things as login, password, etc:
IAsyncResult ar3 = s.BeginSend(tmp,0,tmp.Length,SocketFlags.None,call BackProc,s);
s.EndSend(ar3);

any ideas? anyone run into this problem as well?

Thanks in advance for any help!
Nov 16 '05 #1
3 3868
> in callBackProc:
AsyncCallback receive = new AsyncCallback(OnReceivedData);

s1.BeginReceive(buffer,0,buffer.Length,SocketFlags .None,receive,s1);

in OnReceivedData:
nBytes = s1.EndReceive(ar); //here sometimes nBytes is X and number of
elements in buffer is Y where X<Y.
If you don't get all the data in the callback, you need to call BeginReceive
again in the callback.
See
http://msdn.microsoft.com/library/de...ketexample.asp
for one way to do a async server.
also in OnReceivedData to send such things as login, password, etc:
IAsyncResult ar3 = s.BeginSend(tmp,0,tmp.Length,SocketFlags.None,call BackProc,s); s.EndSend(ar3);


EndSend is supposed to block until count bytes sent. Are you seeing
something different? See above link and post back if still having issues.
Cheers.

--
William Stacey, MVP

Nov 16 '05 #2
Thanks for the response, I did get it working with the help of that
example you posted. Problem was that I was using a global buffer
variable, rather than one stored in a state object, so the buffer was
getting overwritten before I could process the data within.

"William Stacey [MVP]" <st***********@mvps.org> wrote in message news:<uZ**************@TK2MSFTNGP09.phx.gbl>...
in callBackProc:
AsyncCallback receive = new AsyncCallback(OnReceivedData);

s1.BeginReceive(buffer,0,buffer.Length,SocketFlags .None,receive,s1);

in OnReceivedData:
nBytes = s1.EndReceive(ar); //here sometimes nBytes is X and number of
elements in buffer is Y where X<Y.


If you don't get all the data in the callback, you need to call BeginReceive
again in the callback.
See
http://msdn.microsoft.com/library/de...ketexample.asp
for one way to do a async server.
also in OnReceivedData to send such things as login, password, etc:
IAsyncResult ar3 =

s.BeginSend(tmp,0,tmp.Length,SocketFlags.None,call BackProc,s);
s.EndSend(ar3);


EndSend is supposed to block until count bytes sent. Are you seeing
something different? See above link and post back if still having issues.
Cheers.

Nov 16 '05 #3
ahhh. One of the many joys of async programming. :-)

--
William Stacey, MVP

"Chotchkie" <ch****@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
Thanks for the response, I did get it working with the help of that
example you posted. Problem was that I was using a global buffer
variable, rather than one stored in a state object, so the buffer was
getting overwritten before I could process the data within.

"William Stacey [MVP]" <st***********@mvps.org> wrote in message

news:<uZ**************@TK2MSFTNGP09.phx.gbl>...
in callBackProc:
AsyncCallback receive = new AsyncCallback(OnReceivedData);

s1.BeginReceive(buffer,0,buffer.Length,SocketFlags .None,receive,s1);

in OnReceivedData:
nBytes = s1.EndReceive(ar); //here sometimes nBytes is X and number of
elements in buffer is Y where X<Y.


If you don't get all the data in the callback, you need to call BeginReceive again in the callback.
See
http://msdn.microsoft.com/library/de...ketexample.asp for one way to do a async server.
also in OnReceivedData to send such things as login, password, etc:
IAsyncResult ar3 =

s.BeginSend(tmp,0,tmp.Length,SocketFlags.None,call BackProc,s);
s.EndSend(ar3);


EndSend is supposed to block until count bytes sent. Are you seeing
something different? See above link and post back if still having issues. Cheers.


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.