473,546 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,callBackPr oc,s);

in callBackProc:
AsyncCallback receive = new AsyncCallback(O nReceivedData);

s1.BeginReceive (buffer,0,buffe r.Length,Socket Flags.None,rece ive,s1);

in OnReceivedData:
nBytes = s1.EndReceive(a r); //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,S ocketFlags.None ,callBackProc,s );
s.EndSend(ar3);

any ideas? anyone run into this problem as well?

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

s1.BeginReceive (buffer,0,buffe r.Length,Socket Flags.None,rece ive,s1);

in OnReceivedData:
nBytes = s1.EndReceive(a r); //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,S ocketFlags.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.

--
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******* *******@TK2MSFT NGP09.phx.gbl>. ..
in callBackProc:
AsyncCallback receive = new AsyncCallback(O nReceivedData);

s1.BeginReceive (buffer,0,buffe r.Length,Socket Flags.None,rece ive,s1);

in OnReceivedData:
nBytes = s1.EndReceive(a r); //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,S ocketFlags.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.

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

--
William Stacey, MVP

"Chotchkie" <ch****@gmail.c om> wrote in message
news:ee******** *************** ***@posting.goo gle.com...
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******* *******@TK2MSFT NGP09.phx.gbl>. ..
in callBackProc:
AsyncCallback receive = new AsyncCallback(O nReceivedData);

s1.BeginReceive (buffer,0,buffe r.Length,Socket Flags.None,rece ive,s1);

in OnReceivedData:
nBytes = s1.EndReceive(a r); //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,S ocketFlags.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.


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3379
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This...
7
2370
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew what they were doing could take a look at my code and tell me where and why I'm going wrong. Any suggestions of groups or forums?
0
4658
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a number of different types of data coming in. I have a databuffer for each type of data coming in.
2
2209
by: Giulio Petrucci | last post by:
Hi everytbody, I'm experiencing some problems trying to asynchronously read data from a Socket object. What I do now is: 1) call th BeginReceive(...) method on the Socket object; 2) within the asynchronous callback method, call the EndReceive method, getting the number of read bytes (stored in a variable called rb) 3) if (rb>0) I can...
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.