Multiple Socket Connection
-
How do I create a server socket which can accept multiple connection on the same port? Just about every source I found on the internet explains that a Socket-array is used to do this, but that doesn't make sense to me. Even if I use a socket array, if mySocket(1) listens on port 9700, mySocket(2) cannot listen on that same port since the "address is already in use". I've read sources explaining that the socket should be "moved off" to another port after the initial connection, which means that the host should reply to the incoming connection with a new port number... is there a standard global way of doing this? Example... I can VNC my PC over the internet from two different VNC clients at the same time i.e on the same port, how does that server accept more than one connection? "Moving the port off" doesn't seem like a viable solution since the router only has one port forwarding entry on a single port, yet it work fine for multiple connections... but how does this work? The application I am tasked to upgrade is a VB6 application, hence I'm using Winsock, but help in .Net is just as fine, I'm more after the principal than the actual code. Kind Regards
-
How do I create a server socket which can accept multiple connection on the same port? Just about every source I found on the internet explains that a Socket-array is used to do this, but that doesn't make sense to me. Even if I use a socket array, if mySocket(1) listens on port 9700, mySocket(2) cannot listen on that same port since the "address is already in use". I've read sources explaining that the socket should be "moved off" to another port after the initial connection, which means that the host should reply to the incoming connection with a new port number... is there a standard global way of doing this? Example... I can VNC my PC over the internet from two different VNC clients at the same time i.e on the same port, how does that server accept more than one connection? "Moving the port off" doesn't seem like a viable solution since the router only has one port forwarding entry on a single port, yet it work fine for multiple connections... but how does this work? The application I am tasked to upgrade is a VB6 application, hence I'm using Winsock, but help in .Net is just as fine, I'm more after the principal than the actual code. Kind Regards
-
How do I create a server socket which can accept multiple connection on the same port? Just about every source I found on the internet explains that a Socket-array is used to do this, but that doesn't make sense to me. Even if I use a socket array, if mySocket(1) listens on port 9700, mySocket(2) cannot listen on that same port since the "address is already in use". I've read sources explaining that the socket should be "moved off" to another port after the initial connection, which means that the host should reply to the incoming connection with a new port number... is there a standard global way of doing this? Example... I can VNC my PC over the internet from two different VNC clients at the same time i.e on the same port, how does that server accept more than one connection? "Moving the port off" doesn't seem like a viable solution since the router only has one port forwarding entry on a single port, yet it work fine for multiple connections... but how does this work? The application I am tasked to upgrade is a VB6 application, hence I'm using Winsock, but help in .Net is just as fine, I'm more after the principal than the actual code. Kind Regards
evilnoodle wrote:
How do I create a server socket which can accept multiple connection on the same port?
When a socket is 'listening' and someone connects, the .Accept() method of that socket will return a new socket which is 'talking' to whoever called in. Once that has been done, the 'listening' socket can accept another connection. When that connection comes in, .Accept() will return a different socket which can exchange data with the new caller. After a connection is handed off to a new socket with .Accept(), the original listening socket will no longer "care" about it. There are practical limits to how many sockets can be connected at once, but if one creates a new thread for each socket it's pretty easy to handle quite a few. Note that starting a new thread for each socket isn't strictly necessary, but it's often the easiest way to do things. The biggest 'gotcha' is that the new threads won't be able to interact with the UI directly.
-
evilnoodle wrote:
How do I create a server socket which can accept multiple connection on the same port?
When a socket is 'listening' and someone connects, the .Accept() method of that socket will return a new socket which is 'talking' to whoever called in. Once that has been done, the 'listening' socket can accept another connection. When that connection comes in, .Accept() will return a different socket which can exchange data with the new caller. After a connection is handed off to a new socket with .Accept(), the original listening socket will no longer "care" about it. There are practical limits to how many sockets can be connected at once, but if one creates a new thread for each socket it's pretty easy to handle quite a few. Note that starting a new thread for each socket isn't strictly necessary, but it's often the easiest way to do things. The biggest 'gotcha' is that the new threads won't be able to interact with the UI directly.