UnigineScript
The Language
Core Library
Engine Library
Node-Related Classes
GUI-Related Classes
Plugins Library
High-Level Systems
Samples
C++ API
API Reference
Integration Samples
Usage Examples
Content Creation
Materials
Unigine Material Library
Tutorials

Connecting / Disconnecting with NetworkAddress

NetworkAddress Class

This class is used to create network address (IP-address and port number) for the network node.

  • NetworkAddress() — default constructor.
  • NetworkAddress(string ip,int port) — constructor:
    • ip — an IP-address
    • port — a port number
  • void setIpAddress(string ip_address) — sets a new IP-address.
  • string getIpAddress() — gets the current IP-address.
  • void setPort(int port) — sets a new port number.
  • int getPort() — gets the current port number.
  • string toString() — gets an IP-address in form of a string (ip_address : port). Can be used, for example, to output it on the screen or to the log file.
  • void convertDomainNameToIp() — converts a domain name into numeric representation.
    Notice
    This function calls gethostbyname() function that can take considerable time to complete and temporarily blocks the entire application!
  • int equals(NetworkAddress network_address) — compares two network addresses.
    It returns 1 if addresses match; otherwise, 0.
  • int equals(string ip_address, int port) — checks whether the current network address matches the specified one.
    It returns 1 if addresses match; otherwise, 0.

NetworkAddress class can be used in the following way:

Source code (UnigineScript)
NetworkAddress network_address = NetworkAddress("127.0.0.1",60010)
string ip = network_address.getIpAddress();
int port = network.getPort();
log.message("network_address = %s\n",network_address.toString());

Connect and Disconnect Functions

  • int network.startServer(NetworkAddress network_address, int max_connections_allowed) — starts the server on the specified IP-address:
    • network_address — the network address of the server (IP-address and port number. See details on NetworkAddress class.)
    • max_connections_allowed — the maximum number of clients that can be connected to the server.
      The function returns 1 if the server was successfully started; otherwise, 0.
  • int network.startServer(string ip,int port,int max_connections_allowed) — starts the server on the specified IP-address and port.
    • ip — an IP-address on which to start the server on
    • port — a port to be used
    • max_connections_allowed — the maximum number of clients that can be connected to the server
      The function returns 1 if the server was successfully started; otherwise, 0.
  • int network.startServer(string ip, int start_port, int end_port, int max_connections_allowed) — starts the server on the specified IP-address and uses the first available port from the range [start_port; end_port].
    • ip — an IP-address on which to start the server on
    • start_port — the start value of the port range
    • end_port — the end value of the port range
    • max_connections_allowed — the maximum number of clients that can be connected to the server
      The function returns 1 if the server was successfully started; otherwise, 0.
  • void network.stopServer() — stops the server.
  • void network.setMaximumIncomingConnections(int max_connections) — limits the maximum number of incoming connections to the server. When the limit is already reached, new connections will be rejected.
    • max_incoming_connections — the maximum number of connections to the server. Can not exceed the max_connections_allowed value set when the server was run. By default these values are equal.
  • int network.getMaximumIncomingConnections() — returns the set maximum number of connections to the server.
  • int network.connectToServer(NetworkAddress network_address, int num_attempts, int attempt_interval) — connects to the server as a client.
    • network_address — network address of the server
    • num_attempts — the maximum number of connection attempts. This is an optional parameter. By default it is equal to 12.
    • attempt_interval — time interval between connection attempts (in ms). This is an optional parameter. By default it is equal to 500 ms.
      The function returns 1 if the connection request was successfully sent; otherwise, 0. The returned value of 1 does not mean the connection has been established! By successful connection a dedicated packet is received and NETWORK_ON_SERVER_ACCEPTED_CONNECTION network event is called.
      If all connection attempts failed, NETWORK_ON_SERVER_CONNECTION_ATTEMPT_ERROR is received. It contains an error code together with a text description.

      Possible error codes are:

      • CONNECTION_ERROR_NO_ERROR - no error (the attempt to establish connection was successful)
      • CONNECTION_ERROR_ATTEMPT_FAILED - the attempt to send a connection request to the server failed (possibly, there is no connection to the server)
      • CONNECTION_ERROR_ALREADY_CONNECTED - connection to the specified server is already established
      • CONNECTION_ERROR_NO_FREE_INCOMING_CONNECTIONS - the server cannot accept connection request because a limit of maximum connected clients is already reached. (This limit is set via max_connections_allowed parameter or using network.setMaximumIncomingConnections()).
      • CONNECTION_ERROR_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY - the server requires protected connection
      • CONNECTION_ERROR_OUR_SYSTEM_REQUIRES_SECURITY - a client tries to establish protected connection not supported by the server
      • CONNECTION_ERROR_PUBLIC_KEY_MISMATCH - invalid public key is used
      • CONNECTION_ERROR_INVALID_PASSWORD - invalid password is sent from a client to the server
      • CONNECTION_ERROR_BANNED - a client is banned by the server
      • CONNECTION_ERROR_INCOMPATIBLE_PROTOCOL_VERSION - protocols used by a client and the server are incompatible (different versions of RakNet libraries)
      • CONNECTION_ERROR_IP_RECENTLY_CONNECTED - a client with the given IP-address has already connected to the server. Connection is rejected due to security reasons.
  • int network.connectToServer(string ip, int port, int num_attempts, int attempt_interval) — connects to the server as a client. It is the same as the function above except for two first arguments:
    • ip — an IP-address to connect to as a client to a server
    • port — a port to be used
  • void network.disconnectFromServer() — disconnects from the server.
  • void network.cancelConnectionAttempt(NetworkAddress server_address) — cancels all outgoing connection attempts.
    • server_address — an address of the server
  • void network.setTimeout(int ms) — sets time out period for lost connection (in ms). By default it is equal to 10 000 ms (10 s) in the release version; in the debug version it is equal to 30 000 ms (30 s).
  • int network.getTimeout() — returns the set time out period for lost connection.
  • void network.disconnect(Variable target) — closes connection with the network node. This function can be used to force the server to break connection with a specified client.
    • target — an address of the network node, connection with which needs to be broken. It can be one of the following:
  • int network.isConnected() — returns the current connection status: whether it was initialized or not.
    This function returns 1 if the connection was initialized; otherwise, 0. If the server is running, but no clients has connected to it, the returned value is still 1.
  • int network.getNumActiveConnections() — returns the current number of active connections.
  • void network.getActiveConnections(int address_array[]) — returns the list of active connections in an array of NetworkAddress. The order of connections in the array can differ from the order with which clients have connected to the server.
    • address_array — an array of network connections

Samples

Example of limiting the number of incoming connections on the server after the game has started, can be found here:
<UnigineSDK>/data/network/samples/limit_max_connections

Last update: 2017-07-03