1
votes

I've made a server (python, twisted) for my online game. Started with TCP, then later added constant updates with UDP (saw a big speed improvement). But now, I need to connect each UDP socket client with each TCP client.

I'm doing this by having each client first connect to the TCP server, and getting a unique ID. Then the client sends this ID to the UDP server, connecting it also. I then have a main list of TCP clients (ordered by the unique ID).

My goal is to be able to send messages to the same client over both TCP and UDP.
What is the best way to link a UDP and TCP socket to the same client?

Can I just take the IP address of a new TCP client, and send them data over UDP to that IP? Or is it necessary for the client to connect twice, once for TCP and once for UDP (by sending a 'connect' message)?

Finally, if anyone with knowledge of TCP/UDP could tell me (i'm new!), will the same client have the same IP address when connecting over UDP vs TCP (from the same machine)? (I need to know this, to secure my server, but I don't want to accidentally block some fair users)

3

3 Answers

1
votes

Answering your last question: no. Because:

  1. If client is behind NAT, and the gateway (with NAT) has more than one IP, every connection can be seen by you as connection from different IP.
  2. Another problem is when few different clients that are behind the same NAT will connect with your server, you will have more than one pair of TCP-UDP clients. And it will be impossible to join correct pairs.

Your method seems to be good solution for the problem.

1
votes

1- Can I just take the IP address of a new TCP client, and send them data over UDP to that IP? NO in the general case, but ...

2- is it necessary for the client to connect twice, once for TCP and once for UDP ? NO, definitively

3- will the same client have the same IP address when connecting over UDP vs TCP (from the same machine)? YES except in special cases

You really need some basic knowledge of the TCP, UDP and IP protocol to go further, and idealy, on the OSI model.

Basics (but you should read articles on wikipedia to have a deeper understanding) :

  • TCP and UDP are 2 protocol over IP
  • IP is a routable protocol : it can pass through routers
  • TCP is a connected protocol : it can pass through gateways or proxies (firewalls and NATs)
  • UDP in a not connected protocol : it cannot pass through gateways
  • a single machine may have more than one network interface (hardware slot) : each will have different IP address
  • a single interface may have more than one IP address
  • in the general case, client machines have only one network interface and one IP address - anyway you can require that a client presents same address to TCP and UDP when connecting to your server
  • Network Address Translation is when there is a gateway between a local network and the wild internet that always presents its own IP address and keep track of TCP connections to send back packets to the correct client

In fact the most serious problem is if there is a gateway between the client and your server. While the client and the server are two (virtual) machines for which you have direct keyboard access, no problem, but corporate networks are generally protected by a firewall acting as a NAT, and many domestic ADSL routers also include a firewall and a NAT. In that case just forget UDP. It is possible to instruct a domestic router to pass all UDP traffic to a single local IP, but it is not necessarily an easy job. In addition, that means that if a user of yours has more than one machine at home, he will be allowed to use only one at a time and will have to reconfigure his router to switch to another one !

1
votes

First of all when you send data with TCP or UDP you have to give the port.

If your client connect with TCP and after your server send a response with UDP the packet will be reject by the client.

Why? Because you have to register a port for connection and you can not be sure the port is correctly open on the client.

So when you begin a connection in TCP the client open a port to send data and receive the response. You have to make the same with UDP. When client begin all communication with server you can be sure all the necessary port are open.

Don't forget to send data on the port which the connection was open.

Can I just take the IP address of a new TCP client, and send them data over UDP to that IP? Or is it necessary for the client to connect twice, once for TCP and once for UDP (by sending a 'connect' message)?

Why you don't want create 2 connections? You have to use UDP for movement for example. because if you create an FPS you can send the player's position every 50ms so it's really important to use UDP.

It's not just a question of better connection. If you want to have a really good connection between client and server you need to use Async connection and use STREAM. But if you use stream you'r TCP socket do not signal the end of a socket but you have a better transmition. So you have to write something to show the packet end (for example <EOF>). But you have a problem with this. Every socket you receive you have to analyze the data and split over the <EOF>. It can take a lot a processor.

With UDP the packet always have a end signal. But you need to implement a security check.