Reputation: 49
i'am trying to do Socket programming in C# and now i need to understand that do we need array of sockets in Server-side in order to handle multiple clients or one socket in server-side is sufficient for handling many clients;
And need to configure whether all data from server to client has been reached and the availability of server,discarding the client request .
Do we need to create multiple thread to handle each client also ? and i need to handle each client separetely.
Upvotes: 1
Views: 802
Reputation: 1964
you create one listener(welcome socket) which creates a dedicate connection socket for every accepted client.
I think you need some thing like.
private void listen(){
TcpListener listener = new TcpListener(5000);
listener.Start();
while (true)
{
// Accept a new client
Socket clientSocket = listener.AcceptSocket();
//Create a thread for every client.
ParameterizedThreadStart pThreadStart = new ParameterizedThreadStart(handleClient);
Thread thread = new Thread(pThreadStart);
thread.Start(clientSocket);
}
}
Upvotes: 0
Reputation: 4587
Please have a look at Writing scalable server applications using IOCP. This's a good tutorial on how to create a robust server application.
Upvotes: 0
Reputation: 1
You can use one socket to response multiple clients. When a client connet with the server, your application creates a socket to attend this conection.
Upvotes: 0
Reputation: 283614
Each connection will require a new socket. As spender says, you only listen with one socket, the socket API will create the other sockets when connection requests come in.
Read the documentation for accept
Upvotes: 6