Dear all,
I have written a TCP server as a windows service that accepts connections
using the System.Net.Sock ets.Socket class. I use the various asynchronous
methods in the socket class. I'm having a problem at the moment when I shut
the service down. When I stop the service I attempt to shutdown and close
the socket. But when the service is started again and the Socket.Bind
statement is encountered, an error occurs telling me that it is already in
use? How can this be if I have shutdown and closed the socket?
Further more can anyone explain what happens when an Asynchronous method is
called. Is a new thread allocated for the callback? If the method has been
called before in the socket do you get the same thread? I have stepped
through the code using the debugger and I don't understand where the threads
have been created.
Any information on these things would be gratefully received!
Darren
Example Code:
The windows service calls the StartListener and StopListener methods
public void StartListener()
{
Socket ConnectionListe ner = new Socket( AddressFamily.I nterNetwork,
SocketType.Stre am, ProtocolType.Tc p );
ConnectionListe ner.Bind( new IPEndPoint( HostAddress, PortNumber ) );
ConnectionListe ner.Listen( 10 );
while (!ShuttingDown)
{
Listen.Reset();
ConnectionListe ner.BeginAccept ( new AsyncCallback(
ConnectionReque st ), ConnectionListe ner );
Listen.WaitOne( );
}
}
/// <summary>
/// Stop listening for clients
/// </summary>
public void StopListener()
{
ShuttingDown = true;
Socket ConnectionListe ner = new Socket( AddressFamily.I nterNetwork,
SocketType.Stre am, ProtocolType.Tc p );
ConnectionListe ner.Connect( new IPEndPoint( HostAddress, PortNumber ) );
ConnectionListe ner.Shutdown( SocketShutdown. Both );
ConnectionListe ner.Close();
ConnectionListe ner = null;
}
private void ConnectionReque st( IAsyncResult ar )
{
Socket Listener = (Socket) ar.AsyncState;
Socket WorkingSocket = Listener.EndAcc ept( ar );
if ( ConnectedClient s.Count < MaximumClients )
AcceptClient( WorkingSocket );
else
RefuseClient( WorkingSocket );
Listen.Set();
}
private void AcceptClient( Socket WorkingSocket )
{
Client Client = new Client();
Client.Connecte d += new Client.ConnectH andler( AddClient );
Client.Disconne cted += new Client.Disconne ctHandler( RemoveClient );
Client.WorkingS ocket = WorkingSocket;
}
private void RefuseClient( Socket WorkingSocket )
{
WorkingSocket.S end( Encoding.ASCII. GetBytes("To many Clients") );
WorkingSocket.S hutdown( SocketShutdown. Both );
WorkingSocket.C lose();
}
I have written a TCP server as a windows service that accepts connections
using the System.Net.Sock ets.Socket class. I use the various asynchronous
methods in the socket class. I'm having a problem at the moment when I shut
the service down. When I stop the service I attempt to shutdown and close
the socket. But when the service is started again and the Socket.Bind
statement is encountered, an error occurs telling me that it is already in
use? How can this be if I have shutdown and closed the socket?
Further more can anyone explain what happens when an Asynchronous method is
called. Is a new thread allocated for the callback? If the method has been
called before in the socket do you get the same thread? I have stepped
through the code using the debugger and I don't understand where the threads
have been created.
Any information on these things would be gratefully received!
Darren
Example Code:
The windows service calls the StartListener and StopListener methods
public void StartListener()
{
Socket ConnectionListe ner = new Socket( AddressFamily.I nterNetwork,
SocketType.Stre am, ProtocolType.Tc p );
ConnectionListe ner.Bind( new IPEndPoint( HostAddress, PortNumber ) );
ConnectionListe ner.Listen( 10 );
while (!ShuttingDown)
{
Listen.Reset();
ConnectionListe ner.BeginAccept ( new AsyncCallback(
ConnectionReque st ), ConnectionListe ner );
Listen.WaitOne( );
}
}
/// <summary>
/// Stop listening for clients
/// </summary>
public void StopListener()
{
ShuttingDown = true;
Socket ConnectionListe ner = new Socket( AddressFamily.I nterNetwork,
SocketType.Stre am, ProtocolType.Tc p );
ConnectionListe ner.Connect( new IPEndPoint( HostAddress, PortNumber ) );
ConnectionListe ner.Shutdown( SocketShutdown. Both );
ConnectionListe ner.Close();
ConnectionListe ner = null;
}
private void ConnectionReque st( IAsyncResult ar )
{
Socket Listener = (Socket) ar.AsyncState;
Socket WorkingSocket = Listener.EndAcc ept( ar );
if ( ConnectedClient s.Count < MaximumClients )
AcceptClient( WorkingSocket );
else
RefuseClient( WorkingSocket );
Listen.Set();
}
private void AcceptClient( Socket WorkingSocket )
{
Client Client = new Client();
Client.Connecte d += new Client.ConnectH andler( AddClient );
Client.Disconne cted += new Client.Disconne ctHandler( RemoveClient );
Client.WorkingS ocket = WorkingSocket;
}
private void RefuseClient( Socket WorkingSocket )
{
WorkingSocket.S end( Encoding.ASCII. GetBytes("To many Clients") );
WorkingSocket.S hutdown( SocketShutdown. Both );
WorkingSocket.C lose();
}
Comment