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. multi threaded tcp listeners possible

multi threaded tcp listeners possible

Scheduled Pinned Locked Moved C#
sysadminhelp
8 Posts 5 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.
  • M Offline
    M Offline
    Mubeen asim
    wrote on last edited by
    #1

    helu.... i am writing code for a server which listens on a port 1000; and i start the listener as a thread. but the problem is that when the client connections increase then the server is unable to handle the requests and errors n exceoptions occur. i think the single thread is unable to handle that many conections. so is it possible to start multiple threads on the same port 1000; thanks

    L E 2 Replies Last reply
    0
    • M Mubeen asim

      helu.... i am writing code for a server which listens on a port 1000; and i start the listener as a thread. but the problem is that when the client connections increase then the server is unable to handle the requests and errors n exceoptions occur. i think the single thread is unable to handle that many conections. so is it possible to start multiple threads on the same port 1000; thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      No, you'd need more than 1 TCP socket on port 1000 (you can use a socket across multiple threads, but you shouldn't Listen() on multiple threads with the same socket) which is clearly illegal. However, you could handle the request on an other thread if you're not already doing that.

      M 1 Reply Last reply
      0
      • L Lost User

        No, you'd need more than 1 TCP socket on port 1000 (you can use a socket across multiple threads, but you shouldn't Listen() on multiple threads with the same socket) which is clearly illegal. However, you could handle the request on an other thread if you're not already doing that.

        M Offline
        M Offline
        Mubeen asim
        wrote on last edited by
        #3

        thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server

        L C M 3 Replies Last reply
        0
        • M Mubeen asim

          thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          There are several ways, starting a new thread wouldn't be the best of them (too much overhead), but you could use the threadpool Or, you could let the current thread handle the request but Before that, give the socket to an other thread to listen for more connections. As long as the listening for more connections doesn't happen on the same thread as the handling of the existing connections doesn't happen on the same thread. I'd probably loop the listener on its own thread and add the handling to the threadpool queue.

          1 Reply Last reply
          0
          • M Mubeen asim

            helu.... i am writing code for a server which listens on a port 1000; and i start the listener as a thread. but the problem is that when the client connections increase then the server is unable to handle the requests and errors n exceoptions occur. i think the single thread is unable to handle that many conections. so is it possible to start multiple threads on the same port 1000; thanks

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            The AcceptSocket method will return a TcpSocket object moved to a different port so you can begin the accept cycle again. No code has to be written to change the port. You can then queue the Socket objects and parse them in a separate thread. Or you can use Asynchronous sockets. In no case will you need more than two threads to handle the connections. Read the documentation here, http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.acceptsocket.aspx[^] and to verify that the ports are indeed automatically assigned for you run the command line method netstat.

            Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

            1 Reply Last reply
            0
            • M Mubeen asim

              thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server

              C Offline
              C Offline
              caix
              wrote on last edited by
              #6

              Hey Max, Harold is dead on - you cannot run multiple listeners you MUST have only one listener, however you can then run separate threads to actually convert the socket into a connection... I eventually used: http://www.codeproject.com/KB/IP/Generic_TCP_IP_server.aspx?fid=431256&select=2489157&fr=26#xx0xx[^] The code is easy to understand and very powerful - i've added encryption/authentication as i went through the code - took a couple of days and ive been trying to get a solid TCP stack working with async methods for a few weeks! However i did eventually used the async method for the listening socket, everything else is similar to the code in the link On the 'new connection' i pass the async socket result to a connection event in a separate thread - as the code above looks at - minimising the time taken to process new socket before your listener can go back and wait for the next connection. HAving said all that i haven't load tested it just yet :) Good luck, C

              1 Reply Last reply
              0
              • M Mubeen asim

                thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server

                M Offline
                M Offline
                mcldev
                wrote on last edited by
                #7

                Every time accept() returns successfully you will have a new client. You should start a new thread as pass the client socket to that thread so it can process indepently of the main thread. incidentally the only thing your main thread should do is open the socket, accept new connections, dispatch new threads, and look for shutdown. Move your action to worker threads and you will see a notable improvement in perfomance. However be advised you will need to take care of critical sections, and thread synchronization.

                M 1 Reply Last reply
                0
                • M mcldev

                  Every time accept() returns successfully you will have a new client. You should start a new thread as pass the client socket to that thread so it can process indepently of the main thread. incidentally the only thing your main thread should do is open the socket, accept new connections, dispatch new threads, and look for shutdown. Move your action to worker threads and you will see a notable improvement in perfomance. However be advised you will need to take care of critical sections, and thread synchronization.

                  M Offline
                  M Offline
                  Mubeen asim
                  wrote on last edited by
                  #8

                  thank you very mch...

                  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