dcsimg
Results 1 to 4 of 4

Thread: Multi-Threaded TCP Listener

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Posts
    27

    Question Multi-Threaded TCP Listener

    I have a program that listens on a tcp port for another application to connect to it and send data to it. and it stays connected until the other application closes.
    I'm using a threaded tcp listener to do this. I had some user of my program report that from time to time the other program shuts down and restarts itself to reset settings that have changed. When the program does this then my program loses the connection and thinks its lost connection so it shuts everything down.
    When I asked the developer of the other program about this he told me that I am single threaded and I need to use a multi-threaded asynchronous TCP Listener. He says this way when his application drops and reopens that my tcp listener will just think its another client trying to connect.

    I have never heard of a multi-threaded asynchronous TCP Listener. I have googled and find some things on an asynchronous TCP Listener but none show how it would work in a multi-threaded application. I don't want to use any outside dll file I would like the tcp listener to run is the same form like my current tcp listener does.

    Can someone point me in the right direction or send me to a site where I can look at some code to figure out how to accomplish this..

    Thanks in advance..

    Rick

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,324

    Re: Multi-Threaded TCP Listener

    Follow the CodeBank link in my signature below and check out my Asynchronous TCP thread.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,546

    Re: Multi-Threaded TCP Listener

    Since it sounds like you only support one connection at a time, it would seem like you would just need an outer loop that restarts listening again for a new connection, once the connection is lost.
    This would be a method that is between what you're doing now, and most likely what jmc will instruct.

    jmc will most likely be the standard way a tcp server is done, which is to listen for a connection on one socket (and port), and accept the connection with a new socket (and system assigned port). That leaves the server free to accept another connection on the listening port.
    You can have multiple connections running at the same time in that case.

    I have the case where I only want one connection, so use the listening port for the connection as well (probably like you do) so only one connection at a time, as I only have one client that will want to connect, but if the connection is lost, then I fall back into the initial state of listening for a connection again.

    It certainly can't hurt to learn the standard paradigm jmc will teach, as I'm sure there is plenty of useful material to benefit from, and you can adjust or apply what you learn as desired in your case.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,324

    Re: Multi-Threaded TCP Listener

    Quote Originally Posted by passel View Post
    Since it sounds like you only support one connection at a time, it would seem like you would just need an outer loop that restarts listening again for a new connection, once the connection is lost.
    Fair point.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width