470,619 Members | 1,483 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,619 developers. It's quick & easy.

Asynchronous Sockets and Threading

Dear all,

I have written a TCP server as a windows service that accepts connections using the System.Net.Sockets.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 ConnectionListener = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
ConnectionListener.Bind( new IPEndPoint( HostAddress, PortNumber ) );
ConnectionListener.Listen( 10 );
while (!ShuttingDown)
{
Listen.Reset();
ConnectionListener.BeginAccept( new AsyncCallback( ConnectionRequest ), ConnectionListener );
Listen.WaitOne();
}
}

/// <summary>
/// Stop listening for clients
/// </summary>
public void StopListener()
{
ShuttingDown = true;
Socket ConnectionListener = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
ConnectionListener.Connect( new IPEndPoint( HostAddress, PortNumber ) );
ConnectionListener.Shutdown( SocketShutdown.Both );
ConnectionListener.Close();
ConnectionListener = null;
}

private void ConnectionRequest( IAsyncResult ar )
{
Socket Listener = (Socket) ar.AsyncState;

Socket WorkingSocket = Listener.EndAccept( ar );
if ( ConnectedClients.Count < MaximumClients )
AcceptClient( WorkingSocket );
else
RefuseClient( WorkingSocket );

Listen.Set();
}
private void AcceptClient( Socket WorkingSocket )
{
Client Client = new Client();
Client.Connected += new Client.ConnectHandler( AddClient );
Client.Disconnected += new Client.DisconnectHandler( RemoveClient );
Client.WorkingSocket = WorkingSocket;
}

private void RefuseClient( Socket WorkingSocket )
{
WorkingSocket.Send( Encoding.ASCII.GetBytes("To many Clients") );
WorkingSocket.Shutdown( SocketShutdown.Both );
WorkingSocket.Close();
}
Jul 21 '05 #1
0 1170

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Corne Oosthuizen | last post: by
1 post views Thread by Darren | last post: by
48 posts views Thread by Steve - DND | last post: by
9 posts views Thread by Michael Lindsey | last post: by
7 posts views Thread by Colin | last post: by
2 posts views Thread by Stressed Out Developer | last post: by
4 posts views Thread by taskswap | last post: by
2 posts views Thread by Nicolas Le Gland | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.