Re: blocking non blocking
- From: "Daniel" <DanielV@xxxxxxxxxxxxxxxx>
- Date: Thu, 6 Jul 2006 09:04:17 +0100
I see. I coded mine to use asynchronous calls, (tcp stream), stays connected
for the duration of the client app.
I have thought that in an async set up that on connecting it spawns a thread
for that client?
Here is some of my server code, as you can see on every connect i fire off
the asyncallback method to handle the client connection. Which is where i
presumed a thread is spawned and handles that connection? Have i missed out
on some fundamental understanding and inadvertently coded this correctly
anyway?
public void Listen(int PortNo)
{
_portNo = PortNo;
try
{
//create the listening socket...
_socListener = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,_portNo);
//bind to local IP Address...
_socListener.Bind( ipLocal );
//start listening...
_socListener.Listen (4);
// create the call back for any client connections...
_socListener.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
}
catch(SocketException se)
{
MessageBox.Show ( se.Message );
}
}
protected void OnClientConnect(IAsyncResult asyn)
{
try
{
_socWorker = _socListener.EndAccept (asyn);
ClientConnected();
WaitForData(_socWorker);
// create the call back for any client connections...
_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null); //to
allow next client to be able to connect
}
catch(ObjectDisposedException)
{
Console.WriteLine("OnClientConnect: Connection closed
{0}",_socWorker.RemoteEndPoint.ToString());
}
catch(SocketException se)
{
MessageBox.Show ( se.Message, "OnClientConnect::NetServer" );
}
catch(Exception exc)
{
MessageBox.Show("NetServer::OnClientConnect-" + exc.Message,"Failed to
connect - " + exc.GetType());
}
}
protected virtual void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
// if (soc.Connected)
// {
dvSocketPacket theSocPkt = new dvSocketPacket(_bufferSize);
theSocPkt.thisSocket = soc;
theSocPkt.remoteIP = soc.RemoteEndPoint;
// now start to listen for any data...
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length,
SocketFlags.None, new AsyncCallback(OnDataReceived), theSocPkt);
// }
// else
// {
// disconnectEvent(soc.RemoteEndPoint);
//}
}
catch (SocketException se)
{
MessageBox.Show(se.Message, "Error while waiting for data");
}
}
.
- Follow-Ups:
- Re: blocking non blocking
- From: Arkady Frenkel
- Re: blocking non blocking
- References:
- Re: blocking non blocking
- From: Alexander Nickolov
- Re: blocking non blocking
- From: Daniel
- Re: blocking non blocking
- From: Arkady Frenkel
- Re: blocking non blocking
- Prev by Date: Re: Windows ZeroConfig (WZC) in WinXP SP2
- Next by Date: Re: Can I use Windows Sockets API in DllMain or CWinApp::InitInsta
- Previous by thread: Re: blocking non blocking
- Next by thread: Re: blocking non blocking
- Index(es):
Relevant Pages
|