Skip to content

EN_Cooperate With Tcp Server

winlin edited this page Feb 17, 2017 · 2 revisions

Integrate with TCP Server

When integrating KCP to TCP server, when client handshake to server, server can response the port of UDP(Maybe with other information, such as security key), then client can try UDP handshake with server(Maybe with other informations, for instance, security key or TCP id). Finally, both TCP and UDP channel are ready, and server can find out about the map between TCP and UDP for a client. User can use UDP first, then fallback to TCP if has any problem.

For example:

  1. Client connect to TCP server, the id for this client is generated as identify on server-side.
  2. Server response client, including port of UDP, id, a 32 bits security key.
  3. Client send UDP packet to server, with (id and key).
  4. Server send UDP packet to client, map the TCP and UDP as a pair.
  5. Client try UDP, each UDP packet carry (id and key).
  6. Server can validate the client by (id and key).

Remark: Client should send ping message for UDP to keep-alive, or NAT may change the address or port. Client can also reconnect when network changed, for example, the mobile device change base station.

Client can use a general API like:

connection.send(channel, data, size)

When channel is 0, send data over TCP; over KCP when channel is 1; use original UDP when channel is 2. It's similar on server-side.

In china, it's recommented to try UDP, and fallback to TCP when failed.

Clone this wiki locally