Client.connectToServer C# (CSharp) Method

connectToServer() public method

public connectToServer ( ClientSettings settings ) : bool
settings ClientSettings
return bool
    public bool connectToServer(ClientSettings settings)
    {
        if (isConnected)
            return false;

        clientSettings = settings;

        tcpClient = new TcpClient();

        //Look for a port-number in the hostname
        int port = DEFAULT_PORT;
        String trimmed_hostname = clientSettings.hostname;

        int port_start_index = clientSettings.hostname.LastIndexOf(':');
        if (port_start_index >= 0 && port_start_index < (clientSettings.hostname.Length - 1))
        {
            String port_substring = clientSettings.hostname.Substring(port_start_index + 1);
            if (!int.TryParse(port_substring, out port) || port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
                port = DEFAULT_PORT;

            trimmed_hostname = clientSettings.hostname.Substring(0, port_start_index);
        }

        //Look up the actual IP address
        IPHostEntry host_entry = new IPHostEntry();
        try
        {
            host_entry = Dns.GetHostEntry(trimmed_hostname);
        }
        catch (SocketException)
        {
            host_entry = null;
        }
        catch (ArgumentException)
        {
            host_entry = null;
        }

        IPAddress address = null;
        if (host_entry != null && host_entry.AddressList.Length == 1)
            address = host_entry.AddressList.First();
        else
            IPAddress.TryParse(trimmed_hostname, out address);

        if (address == null)
        {
            Console.WriteLine("Invalid server address.");
            return false;
        }

        IPEndPoint endpoint = new IPEndPoint(address, port);

        Console.WriteLine("Connecting to server...");

        try
        {
            tcpClient.Connect(endpoint);

            if (tcpClient.Connected) {

                //Init udp socket
                try
                {
                    udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    udpSocket.Connect(endpoint);
                }
                catch
                {
                    if (udpSocket != null)
                        udpSocket.Close();

                    udpSocket = null;
                }

                udpConnected = false;
                lastUDPAckReceiveTime = 0;
                lastUDPMessageSendTime = stopwatch.ElapsedMilliseconds;

                connectionStarted();

                return true;
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("Exception: " + e.ToString());
        }
        catch (ObjectDisposedException e)
        {
            Console.WriteLine("Exception: " + e.ToString());
        }

        return false;
    }

Usage Example

    public void ConnectToServerButton()
    {
        string hostAddress = GameObject.Find("HostInput").GetComponent <InputField>().text;

        if (hostAddress == "")
        {
            hostAddress = "127.0.0.1";
        }

        try
        {
            Client c = Instantiate(clientPrefab).GetComponent <Client>();
            c.clientName = nameInput.text;
            if (c.clientName == "")
            {
                c.clientName = "Host";
            }
            c.connectToServer(hostAddress, 6321);
            connectMenu.SetActive(false);
        }
        catch (Exception e)
        {
            Debug.Log("Error: " + e.Message);
        }
    }
All Usage Examples Of Client::connectToServer