Asynchronous socket EndReceive method not returning correct number of bytes?

C

Chotchkie

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,callBackProc,s);
s.EndSend(ar3);

any ideas? anyone run into this problem as well?

Thanks in advance for any help!
 
W

William Stacey [MVP]

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/d...html/cpconnon-blockingserversocketexample.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,callBackProc,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.
 
C

Chotchkie

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.
 
W

William Stacey [MVP]

ahhh. One of the many joys of async programming. :)

--
William Stacey, MVP

Chotchkie said:
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.



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/d...html/cpconnon-blockingserversocketexample.asp
for one way to do a async server.


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.
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top