public void Start(int portNo, int MaxPendingCon) { try { /*dca isStarted este true inseaman ca severul este deja pornit si nul mai porneste odata*/ if (!isStarted) { mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (AsynConnect == null) AsynConnect = new AsyncCallback(OnConnect);
if (AsynDataRecive == null) AsynDataRecive = new AsyncCallback(OnDataRecive);
3879 clients and then you use the default ThreadPool... i am in shock that you allready got 3879 connected clients.
Forget the BeginConnect, BegintReceive, etc.. methods and create your own multi-threaded listening server. Because the ThreadPool only has 25 thread it isn't a good idea to trust on that.
The ThreadPool only has 25 Threads, so it they are all used, no other async process can be started with that pool.
I have writen a server application that can handle 10.000+ connected clients at the time without any problems. But i haven't used the default ThreadPool of .NET but created a own pooling meganism. I used 1 thread for each 1.000 connected users and the thread didn't do anything more than polling the connected sockets. Then i had one thread that only handles the accept process and a couple of threads that do the processing of data.
I suggest to write you own meganism and don't use the default ThreadPool for such great and havy server applications.
You can use the Socket.Listen method to places a Socket in a listening state. The method takes one int parameter that specifies the maximum length of the pending connections queue.
ya.. but begin accept are async methods arent they already executed on a seperate thread
Anyway.. i thought that my other client class is the problem... but then i tried to connect with telnet.. and got message that the it cant connect to that port..
it seams that that is the max connections.. but the numer of connection is variable.. sometimes 3870 , 3840, ... the biggest i got 4011 clients but that just once..
public void Start(int portNo, int MaxPendingCon) { try { /*dca isStarted este true inseaman ca severul este deja pornit si nul mai porneste odata*/ if (!isStarted) { mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (AsynConnect == null) AsynConnect = new AsyncCallback(OnConnect);
if (AsynDataRecive == null) AsynDataRecive = new AsyncCallback(OnDataRecive);
I have a mainSocket that server is watching for connection request
when some user wants to connect i associate him a socket returned form the EndAccept method .. then i create a objecet SocketPack (which holds the socket of the new client, buffer, id ...) and then add it to a ArrayList --> live_connections... the new socket retrived from the EndAccept i set it to watch for data incoming with BeginRecive..
socket exception
Wolvorine
maybe you cant connect more then that number on a single socket..
klez
public void Start(int portNo, int MaxPendingCon)
{
try
{
/*dca isStarted este true inseaman ca severul este deja pornit si nul mai porneste odata*/
if (!isStarted)
{
mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (AsynConnect == null) AsynConnect = new AsyncCallback(OnConnect);
if (AsynDataRecive == null) AsynDataRecive = new AsyncCallback(OnDataRecive);
mainSocket.Bind(new IPEndPoint(IPAddress.Any, portNo));
mainSocket.Listen(MaxPendingCon);
mainSocket.BeginAccept(AsynConnect, null);
isStarted = true;
port = portNo;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
#endregion
#region OnConnect()
/// <summary>
/// Cand se conecteaza un client
/// </summary>
/// <param name="asyn">IAsyncResult: returnat de BeginRecive</param>
private void OnConnect(IAsyncResult asyn)
{
try
{
// id clientul
int newID = 1;
Socket newClientSocket = mainSocket.EndAccept(asyn);
if (live_connections.Count > 0)
{
newID = (live_connections[live_connections.Count - 1] as SocketPack).SocketID + 1;
}
SocketPack newClient = new SocketPack(newClientSocket, newID);
newClient.Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);
live_connections.Add(newClient);
OnClient_Connect(this, new Client_Connect_Args(newClient));
try
{
newClient.Socket.BeginReceive(newClient.Buffer, 0, newClient.Buffer.Length, SocketFlags.None, AsynDataRecive, newClient);
}
catch (Exception m)
{
OnServer_Error_Occured(new Server_Error_Occured_Args(m));
}
mainSocket.BeginAccept(AsynConnect, null);
as you see i put the new client for watching for data and the main socket Begins yo accept again..
Cal Zant
Forget the BeginConnect, BegintReceive, etc.. methods and create your own multi-threaded listening server. Because the ThreadPool only has 25 thread it isn't a good idea to trust on that.
Reuomi
How do you handle the server socket's accept Can you post some relevant code
Thanks!
I know, no problem. Only closed that thread because here in this post there is a discussion about how to solve this. No offence!
ParadoxNut
I have writen a server application that can handle 10.000+ connected clients at the time without any problems. But i haven't used the default ThreadPool of .NET but created a own pooling meganism. I used 1 thread for each 1.000 connected users and the thread didn't do anything more than polling the connected sockets. Then i had one thread that only handles the accept process and a couple of threads that do the processing of data.
I suggest to write you own meganism and don't use the default ThreadPool for such great and havy server applications.
wei kiat lam
int queueSize = 50;
Socket serverSocket = GetServerSocket();
serverSocket.Listen( queueSize );
Carl M.
how
Pavan Kurimilla
David DeVey
Anyway.. i thought that my other client class is the problem... but then i tried to connect with telnet.. and got message that the it cant connect to that port..
it seams that that is the max connections.. but the numer of connection is variable..
sometimes 3870 , 3840, ... the biggest i got 4011 clients but that just once..
fredzh
hi,
you can ask this question in .net networking forum
http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=40&SiteID=1
Ralf Herrmann
In you code i can't really see bottlenecks.
&#35; broxi &#35;
IDisposable
public void Start(int portNo, int MaxPendingCon)
{
try
{
/*dca isStarted este true inseaman ca severul este deja pornit si nul mai porneste odata*/
if (!isStarted)
{
mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (AsynConnect == null) AsynConnect = new AsyncCallback(OnConnect);
if (AsynDataRecive == null) AsynDataRecive = new AsyncCallback(OnDataRecive);
mainSocket.Bind(new IPEndPoint(IPAddress.Any, portNo));
mainSocket.Listen(MaxPendingCon);
mainSocket.BeginAccept(AsynConnect, null);
isStarted = true;
port = portNo;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
#endregion
#region OnConnect()
/// <summary>
/// Cand se conecteaza un client
/// </summary>
/// <param name="asyn">IAsyncResult: returnat de BeginRecive</param>
private void OnConnect(IAsyncResult asyn)
{
try
{
// id clientul
int newID = 1;
Socket newClientSocket = mainSocket.EndAccept(asyn);
if (live_connections.Count > 0)
{
newID = (live_connections[live_connections.Count - 1] as SocketPack).SocketID + 1;
}
SocketPack newClient = new SocketPack(newClientSocket, newID);
newClient.Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);
live_connections.Add(newClient);
OnClient_Connect(this, new Client_Connect_Args(newClient));
try
{
newClient.Socket.BeginReceive(newClient.Buffer, 0, newClient.Buffer.Length, SocketFlags.None, AsynDataRecive, newClient);
}
catch (Exception m)
{
OnServer_Error_Occured(new Server_Error_Occured_Args(m));
}
mainSocket.BeginAccept(AsynConnect, null);
}
catch (Exception e)
{
OnServer_Error_Occured(new Server_Error_Occured_Args(e));
}
}
Ok so here it how it goes..
I have a mainSocket that server is watching for connection request
when some user wants to connect i associate him a socket returned form the EndAccept method .. then i create a objecet SocketPack (which holds the socket of the new client, buffer, id ...) and then add it to a ArrayList --> live_connections... the new socket retrived from the EndAccept i set it to watch for data incoming with BeginRecive..
Hope this helps to help me
Count Infinity
I get this exactly message:
"An operation on a socket coult not be performed because the system lacked sufficient buffer space or because a queue was full"
btw shakalama said i should post on the net comunication forum.. thats why i started the other thread...