Exemple #1
0
 /// <summary>
 /// Cuando el cliente se conecto finalmente con el server
 /// </summary>
 internal void clientConnectedToServer(TgcSocketServerInfo serverInfo, int playerId)
 {
     buttonJoinServer.Enabled       = false;
     buttonDisconnect.Enabled       = true;
     textBoxCurrentServer.Text      = serverInfo.Name;
     textBoxCurrentServer.BackColor = Color.Green;
     textBoxPlayerId.Text           = playerId.ToString();
     textBoxServerIp.Text           = modifier.Client.ServerInfo.Address.ToString();
 }
 /// <summary>
 /// Cuando el cliente se conecto finalmente con el server
 /// </summary>
 internal void clientConnectedToServer(TgcSocketServerInfo serverInfo, int playerId)
 {
     buttonJoinServer.Enabled = false;
     buttonDisconnect.Enabled = true;
     textBoxCurrentServer.Text = serverInfo.Name;
     textBoxCurrentServer.BackColor = Color.Green;
     textBoxPlayerId.Text = playerId.ToString();
     textBoxServerIp.Text = modifier.Client.ServerInfo.Address.ToString();
 }
        /// <summary>
        /// Se conecta a un nuevo servidor. Pero todavía no lo toma como definitivo hasta
        /// que no se hace la selección final.
        /// </summary>
        /// <param name="ip">IP del server</param>
        /// <param name="port">Puerto del server</param>
        /// <returns>True si todo salio bien</returns>
        public bool connect(string ip, int port)
        {
            try
            {
                //Conectar con el server, en forma no bloqueante
                IPAddress  address = IPAddress.Parse(ip);
                IPEndPoint Ipep    = new IPEndPoint(address, port);
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(Ipep);
                clientSocket.Blocking = false;

                serverInfo = new TgcSocketServerInfo(address, port);

                //Enviar mensaje de Handshake
                clientSocket.Send(Encoding.ASCII.GetBytes(CLIENT_HANDSHAKE));
                status = TgcSocketClientInfo.ClientStatus.HandshakePending;

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        /// <summary>
        /// Se conecta a un nuevo servidor. Pero todavía no lo toma como definitivo hasta
        /// que no se hace la selección final.
        /// </summary>
        /// <param name="ip">IP del server</param>
        /// <param name="port">Puerto del server</param>
        /// <returns>True si todo salio bien</returns>
        public bool connect(string ip, int port)
        {
            try
            {
                //Conectar con el server, en forma no bloqueante
                IPAddress address = IPAddress.Parse(ip);
                IPEndPoint Ipep = new IPEndPoint(address, port);
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(Ipep);
                clientSocket.Blocking = false;

                serverInfo = new TgcSocketServerInfo(address, port);

                //Enviar mensaje de Handshake
                clientSocket.Send(Encoding.ASCII.GetBytes(CLIENT_HANDSHAKE));
                status = TgcSocketClientInfo.ClientStatus.HandshakePending;

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }