A lot of Sockets
-
Hi There ! I am trying to build a server - client application, using tcp/ip and sockets. I would like that the server will be able to listen to some clients, and if a new client "shows up", the server notice the new client and will add it to other clients. Can someone show me an example for something like this ? Thanks.
-
Hi There ! I am trying to build a server - client application, using tcp/ip and sockets. I would like that the server will be able to listen to some clients, and if a new client "shows up", the server notice the new client and will add it to other clients. Can someone show me an example for something like this ? Thanks.
Your server socket must use the Accept method to handle incoming client requests. Actually you should use BeginAccept uses the thread pool) for server apps for performance. In accepting these requests a handler handles the request and it is in this handler that you can add all incoming requests to wherever you want.
-
Your server socket must use the Accept method to handle incoming client requests. Actually you should use BeginAccept uses the thread pool) for server apps for performance. In accepting these requests a handler handles the request and it is in this handler that you can add all incoming requests to wherever you want.
My server socket already uses Accept method. The thing is that I eant to avoid this situation: IPEndPoint a_EndPoint = new IPEndPoint(ipAddress, a_port); IPEndPoint b_EndPoint = new IPEndPoint(ipAddress, b_port); ... // Create a TCP/IP socket. Socket a_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); Socket b_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Bind the socket to the local endpoint and listen for incoming connections. try { a_listener.Bind(a_EndPoint); a_listener.Listen(num_of_clients); // The maximum length of the queue of pending connections b_listener.Bind(b_EndPoint); b_listener.Listen(num_of_clients); // The maximum length of the queue of pending connections ... while (true) { // Set the event to nonsignaled state. all_done.Reset(); // Start an asynchronous socket to listen for connections. Console.WriteLine("Waiting for a connection..."); a_listener.BeginAccept( new AsyncCallback(accept_callback_a), a_listener ); b_listener.BeginAccept( new AsyncCallback(accept_callback_b), b_listener ); ... // Wait until a connection is made before continuing. all_done.WaitOne(); You see, I have to create a new object for socket, and to create each one its own function and its own delegat... How can I avoid this ? Thanks.
-
My server socket already uses Accept method. The thing is that I eant to avoid this situation: IPEndPoint a_EndPoint = new IPEndPoint(ipAddress, a_port); IPEndPoint b_EndPoint = new IPEndPoint(ipAddress, b_port); ... // Create a TCP/IP socket. Socket a_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); Socket b_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Bind the socket to the local endpoint and listen for incoming connections. try { a_listener.Bind(a_EndPoint); a_listener.Listen(num_of_clients); // The maximum length of the queue of pending connections b_listener.Bind(b_EndPoint); b_listener.Listen(num_of_clients); // The maximum length of the queue of pending connections ... while (true) { // Set the event to nonsignaled state. all_done.Reset(); // Start an asynchronous socket to listen for connections. Console.WriteLine("Waiting for a connection..."); a_listener.BeginAccept( new AsyncCallback(accept_callback_a), a_listener ); b_listener.BeginAccept( new AsyncCallback(accept_callback_b), b_listener ); ... // Wait until a connection is made before continuing. all_done.WaitOne(); You see, I have to create a new object for socket, and to create each one its own function and its own delegat... How can I avoid this ? Thanks.
I'm noy quite sure what you mean exactly? Are you saying that an accept is required for every client? If so, and if I'm understanding exactly your situation, this is not correct. You only need one server socket and it will be "accepting" ALL client requests. Again, all clients will be making a request to the one server listening socket. Now when the server "accepts" the a request you can process that one request as you see fit while at the same time the server may be "accepting" other requests. All these things happen concurrently. That is why we call the server an asynchronous socket.
-
I'm noy quite sure what you mean exactly? Are you saying that an accept is required for every client? If so, and if I'm understanding exactly your situation, this is not correct. You only need one server socket and it will be "accepting" ALL client requests. Again, all clients will be making a request to the one server listening socket. Now when the server "accepts" the a request you can process that one request as you see fit while at the same time the server may be "accepting" other requests. All these things happen concurrently. That is why we call the server an asynchronous socket.
Hi and thanks for you help ! I know that I can recieve the information through one socket. But, I need my server to distinguish between the various clients. I want to know who send a message to the server. In adition, I want to be able to send a message to a specific client - not to all of them together !
-
Hi and thanks for you help ! I know that I can recieve the information through one socket. But, I need my server to distinguish between the various clients. I want to know who send a message to the server. In adition, I want to be able to send a message to a specific client - not to all of them together !
Hi, In adition, I want to be able to send a message to a specific client - not to all of them together ! When you "accept" a request, you then get the socket, whereby this socket responds solely to the requesting client and no other. But, I need my server to distinguish between the various clients. I want to know who send a message to the server. The client will have to pass some identification from itself to the server within the buffer that it sends. The server can then identify the client. Cheers
-
Hi, In adition, I want to be able to send a message to a specific client - not to all of them together ! When you "accept" a request, you then get the socket, whereby this socket responds solely to the requesting client and no other. But, I need my server to distinguish between the various clients. I want to know who send a message to the server. The client will have to pass some identification from itself to the server within the buffer that it sends. The server can then identify the client. Cheers
Hi again ! YOu wrote: "When you "accept" a request, you then get the socket, whereby this socket responds solely to the requesting client and no other." But, I want to send data to some of my clients: 1. I don't want to reply - I want to send something to a certain client without the need to recieve something from him right now. 2. I want to be able to send the message to some client - for example, to send data to clietn_ID: 1, 3, 5. How can I do this ?