473,411 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 1292

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I...
1
by: Darren | last post by:
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...
48
by: Steve - DND | last post by:
I'm trying to determine if I need to make my application multi-threaded, or if it can be done with asynchronous programming. I realize that asynch calls do create a new thread in the background,...
9
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am...
7
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...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
0
by: Raymondr | last post by:
Hi, First a brief description of out application: We have a webapplication which calls a couple of webservices during one request (postback). These calls to the webservices are made concurrent...
2
by: Nicolas Le Gland | last post by:
Hello everyone here. This is my first post in this newsgroup, I hope I won't be to much off-topic. Feel free to redirect me to any better group. I am getting strange timing issues when...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.