1
votes

Say I have a public server on the Internet, written in C, that is connected to two independent clients. Both clients initiated the connection. One client is (e.g.) an iOS app, the other is a native Windows app.

The purpose of my server is to allow these clients to send text messages to each other. Client 1 sends a message, the server receives it, and then forwards it onto Client 2. The same thing happens in reverse when Client 2 sends a message.

This feels inefficient. What I would really like is for both of these connections to contact the server and then for the server to connect these two clients directly to each other - after which my server can forget both clients as they are responsible for communicating with each other. My server is then free to connect up other clients in the same way.

My question is: is this even possible (with TCP and/or UDP)? Neither client necessarily has a public IP address, which is why they have to initiate the connection. Once the connection is established however, my server knows the connection address of both clients. Is there a way to connect them together? A syscall that can do this sort of thing, perhaps?

1
No, there is not. Sure, your server can supply the IP adresses of the two clients to each other, and one or both can try to communicate directly with either UDP or TCP, (one of both would have to run a server, probably both, like peer-to-peer), but you cannot just hand off your existing server-client connections:(Martin James

1 Answers

4
votes

No, you can not join two existing TCP connections to one.

There are 2 options:

1) Keep your server in the middle as it already is. It should not be problem, if you don't have thousands of clients under one server.

2) Server could send control messages to clients, and order those open new TCP connection between each other. Server should also make decision which of the two clients must be initiator of the new TCP connection.

Option 2 is problematic, because client may have firewall rule that prevents in coming connection to the wanted port. Also NAT would cause problems.