Search Unity

Choosing Port Number

Discussion in 'Connected Games' started by atmuc, Feb 4, 2019.

  1. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,035
    I develop my own network multiplayer framework. I use both rest services and socket connections. I have an authoritative server.

    Which port numbers should I use for client-server connection? Is there any restricted ports for some countries?

    I plan to use just socket connections for all type of games. Should I use rest services dynamic single player games instead of socket connection?
     
  2. LukeDawn

    LukeDawn

    Joined:
    Nov 10, 2016
    Posts:
    322
    I've always started at 5000, for no real reason than that's from one of the earliest networking tutorials I saw. I'll change if new info comes along though.
     
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,217
    https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

    Summary:

    Ports 0-1023 are reserved, should never be used. On linux they can't be used without super user privs.

    1024 - 49151 are registered. Or actually reserved for registration, not all are actually in use.

    49152–65535 are ephemeral, not assigned free to use for whatever.

    Ephemeral ports are used by quite a bit of software, usually on the server but sometimes on the client. If you use numbers from that range, you really need to write logic that picks an available port from a range, going down the range until you find one available. Because a lot of server daemons assign a local port for each connection from this range.

    Ephemeral ports on the client are generally a bad idea for games. Higher chance of conflict, and you really need those to be known/stable so users can unblock them if necessary.

    So I usually pick my ports from the registered list, using ones not actually in use. Chances of a conflict are low and you can have stable port numbers.
     
  4. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,035
    @snacktime Thanks for this info.

    I will use a port number just on the server side. There will be no socket listener on the client side. For each server, I need nearly 10 port numbers to listen to incoming connections. I plan to handle 10.000 CCU on a server. I hope each client connection uses the same port number on the server side. For example, each client will connect to the port 5555 on the server. I hope those connections will be handled on the serverside port 5555. I will test if the server creates another port for connection coming to port 5555.

    Is it advantageous to use rest services instead of a socket connection? If I use socket connection 5555 instead of a rest service, Will fewer players to be able to connect my server?
     
  5. LukeDawn

    LukeDawn

    Joined:
    Nov 10, 2016
    Posts:
    322
    You're main issue will be how many messages per second the server will be sending to each of those 10k clients.
     
  6. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,035
    @LukeDawn It is a turn-based game, so message count will not be a problem.
     
    LukeDawn likes this.