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! 3 3956
> 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
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.
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |