Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Multi-Threaded TCP Listener

rellison

New member
Joined
Mar 14, 2001
Messages
27
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
 
Joined
May 20, 2005
Messages
104,556
Follow the CodeBank link in my signature below and check out my Asynchronous TCP thread.
 

passel

Sinecure devotee
Joined
Aug 15, 2013
Messages
5,882
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.
 
Joined
May 20, 2005
Messages
104,556
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.
 
Top