Exemple #1
0
    //renaming to something other than start, client attempts to connect before login attempted --Anthony
    // Triggers by clicking the Log In button.
    public void connecting()
    {
        // Connect to a remote device.
        if (connected)       //prevent trying to connect twice
        {
            return;
        }
        try
        {
            // Establish the remote endpoint for the socket.
            // The name of the remote device is "host.contoso.com".
            //ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());

            // You don't have to touch this. Determine server IP address:
            IPAddress ipAddress = IPAddress.Parse(IPManager.getServerIP());

            // Make the connection
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // Create the state object for sending.
            send_so            = new StateObject();
            send_so.workSocket = client;
            // Create the state object for receiving.
            recv_so            = new StateObject();
            recv_so.workSocket = client;

            // Connect to the remote endpoint.
            client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), send_so);
            // Waits for 5 seconds for connection to be done
            send_so.connectDone.WaitOne(5000);

            // Send test data to the remote device.
            Send(client, "This is a test<EOF>", send_so);
            send_so.sendDone.WaitOne(/*5000*/);

            // Receive the response from the remote device.
            Receive(recv_so);
            recv_so.receiveDone.WaitOne(1000);

            connected = true;
            SceneEnter();            //check the scene once a connection has been established
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }
    }