Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A lot of Sockets

A lot of Sockets

Scheduled Pinned Locked Moved C#
sysadmintutorialquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nahumtakum
    wrote on last edited by
    #1

    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.

    N 1 Reply Last reply
    0
    • N nahumtakum

      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.

      N Offline
      N Offline
      neroknights
      wrote on last edited by
      #2

      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.

      N 1 Reply Last reply
      0
      • N neroknights

        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.

        N Offline
        N Offline
        nahumtakum
        wrote on last edited by
        #3

        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.

        N 1 Reply Last reply
        0
        • N nahumtakum

          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.

          N Offline
          N Offline
          neroknights
          wrote on last edited by
          #4

          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.

          N 1 Reply Last reply
          0
          • N neroknights

            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.

            N Offline
            N Offline
            nahumtakum
            wrote on last edited by
            #5

            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 !

            N 1 Reply Last reply
            0
            • N nahumtakum

              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 !

              N Offline
              N Offline
              neroknights
              wrote on last edited by
              #6

              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

              N 1 Reply Last reply
              0
              • N neroknights

                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

                N Offline
                N Offline
                nahumtakum
                wrote on last edited by
                #7

                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 ?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups