简体   繁体   中英

c# Asynchronous TCP server blocking clients

I have developed an asynchronous TCP server in c#. Code goes like this

m_mainSocket = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream,
    ProtocolType.Tcp);

IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, portno);

m_mainSocket.Blocking = false;

m_mainSocket.Bind(ipLocal);

m_mainSocket.Listen(5000);

m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null); 

OnclienetConnect is like this,

deviceSocket soc = new deviceSocket();
soc.bytecount = 0;
soc.socket = m_mainSocket.EndAccept(asyn);
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);

m_workerSocket.Add(soc);              

WaitForData(m_workerSocket[m_workerSocket.Count - 1].socket);

It works fine for some time(up to 30-40 minutes). After that it starts blocking clients. Can anybody tell what may be the problem with this?

If it works fine for some time, and then stops, my main guess would be you're opening new Client sockets (need to look at your code to make sure), and not closing them when you're done.

Windows (quite reasonably) has a limit of the sockets that you can open on a given port. If your application opens new sockets over time, and doesn't close them, you can reach this limit at which point the server will stop accepting new connections. I have experienced similar issues in the past.

Here is an interesting article you can look at: http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM