Menu

#3 BeginRead at wrong position

open
nobody
Server (2)
5
2007-07-20
2007-07-20
liuhongbo
No

public void StartHandler(Socket Client, bool ForceSSL)
{
try
{
//Setup the async socket
_asyncSocket = new AsyncSocket(Client);
_asyncSocket.OnReceive += new AsyncSocket.OnReceiveEvent(asyncSocket_OnReceive);
_asyncSocket.OnSendComplete += new AsyncSocket.OnSendCompleteEvent(asyncSocket_OnSendComplete);
_asyncSocket.OnDisconnect += new AsyncSocket.OnDisconnectEvent(asyncSocket_OnDisconnect);
_asyncSocket.OnException += new AsyncSocket.OnExceptionEvent(asyncSocket_OnException);
_asyncSocket.OnSecure += new AsyncSocket.OnSecureEvent(_asyncSocket_OnSecure);

if (ForceSSL)
{
_asyncSocket.SecureSocketAsServer(Settings.CertificateData.Certificate, false, System.Security.Authentication.SslProtocols.Tls, false);
}
else
{
//Tell the derived handler that we have started.
HandlerStarted();
}
}
catch
{
//If an error occured here, we won't get very far. Just shutdown
Dispose();
}
}

public AsyncSocket(Socket Connection)
{
//Setup the vars
Reset();
_lastActivity = DateTime.Now;
_timer = new Timer(new TimerCallback(TimerCallback), null, 1000, 1000);
_socket = Connection;

//Get the stream and wait for data
NetworkStream ns = new NetworkStream(_socket);
_stream = (Stream)ns;
_stream.BeginRead(_buffer, 0, _buffer.Length, new AsyncCallback(ReadCallback), null);
}

problem:
you can not call BeginRead here before you hook up the events. because you might receive the data after you call BeginRead, so, you might lost some data.

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.