Пример #1
0
        public Client(string address, int port)
        {
            gsv = Game.GetInstance().GUI.GameSetupView;

            Console.WriteLine("Connecting to " + address + " at " + port);
            gsv.addStatusText("Connecting to " + address + " at " + port);

            try
            {
            gameIP = new IPEndPoint(IPAddress.Parse(address),port);
            }
            catch (System.FormatException e)
            {
            gsv.addStatusText("Looking up " + address + " ...");
            try {
                gameIP = new IPEndPoint(System.Net.Dns.GetHostByName(address).AddressList[0], port);
            }
            catch (Exception ex)
            {
                gsv.reenableConnectButton();
                gsv.addStatusText("Could not lookup " + address + " on port " + port + ": " + ex.Message);
                return;
            }

            } catch (Exception e) {
            gsv.reenableConnectButton();
            gsv.addStatusText("Unknown error parsing address " + address + " on port " + port + ": " + e.Message);
            return;
            }

            try
            {
            gameSocket = new Socket(System.Net.Sockets.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp);
            gameSocket.Connect(gameIP);
            gameConnection = new Connection(gameSocket);
            }
            catch (Exception e)
            {
            gsv.reenableConnectButton();
            gsv.addStatusText("Could not connect to " + address + " on port " + port + ": " + e.Message);
            return;
            }

            listenData = new Thread(new ThreadStart(listenForData));
            listenData.Start();

            gsv.addStatusText("Connected.");

            DataPacket pkt = new DataPacket("Connected", null);
            gameConnection.sendObject(pkt);

            participants = new ArrayList();
        }