コード例 #1
0
        private void Juego_Load(object sender, EventArgs e)
        {
            CargarGraficos();

            if (isMultiplayer == true && isServer == false)
            {
                try
                {
                    CheckForIllegalCrossThreadCalls = false;
                    strName      = player;
                    this.Text    = strName;
                    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                    IPAddress ipAddress = IPAddress.Parse(serverIP);
                    //Server is listening on port 1000
                    IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);

                    //Connect to the server
                    clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);

                    MessageBox.Show("Connected");

                    msgToSend            = new SocketFiles.Data();
                    msgToSend.cmdCommand = SocketFiles.Command.List;
                    msgToSend.strName    = strName;
                    msgToSend.strMessage = null;

                    byteData = msgToSend.ToByte();

                    clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                    byteData = new byte[1024];

                    //Start listening to the data asynchronously
                    clientSocket.BeginReceive(byteData,
                                              0,
                                              byteData.Length,
                                              SocketFlags.None,
                                              new AsyncCallback(OnReceive),
                                              null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
コード例 #2
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);

                msgReceived = new SocketFiles.Data(byteData);
                //Accordingly process the message received
                switch (msgReceived.cmdCommand)
                {
                case SocketFiles.Command.Login:
                    //lstChatters.Items.Add(msgReceived.strName);
                    break;

                case SocketFiles.Command.Logout:
                    //lstChatters.Items.Remove(msgReceived.strName);
                    break;

                case SocketFiles.Command.Message:
                    //TODO
                    break;

                case SocketFiles.Command.List:

                    //lstChatters.Items.AddRange(msgReceived.strMessage.Split('*'));
                    //lstChatters.Items.RemoveAt(lstChatters.Items.Count - 1);
                    //txtChatBox.Text += "<<<" + strName + " Ha entrado al Juego>>>\r\n";
                    break;
                }

                //if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.List)
                //    txtChatBox.Text += msgReceived.strMessage + "\r\n";

                byteData = new byte[1024];

                clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #3
0
        private void OnConnect(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndConnect(ar);

                //We are connected so we login into the server
                msgToSend            = new SocketFiles.Data();
                msgToSend.cmdCommand = SocketFiles.Command.Login;
                msgToSend.strName    = player;
                msgToSend.strMessage = null;

                byte[] b = msgToSend.ToByte();

                //Send the message to the server
                clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }