Power
Power

Reputation: 49

socket programming in C#

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

Answers (5)

HforHisham
HforHisham

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

anhldbk
anhldbk

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

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

Ben Voigt
Ben Voigt

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

spender
spender

Reputation: 120380

One listening socket can service many clients.

Upvotes: 1

Related Questions