Exemple #1
0
        public void Connect()
        {
            client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                client_socket.Connect(new IPEndPoint(IPAddress.Parse(IP), port));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not connect to server!");

                lobby.Invoke(() =>
                {
                    lobby.Close();
                });
                return;
            }

            if (client_socket.Connected)
            {
                Packet pack = new Packet();
                pack.Type         = JoinMessage;
                pack.PlayerStatus = new player();

                pack.PlayerStatus.Name = lobby.player_name;

                SendPacket(pack);
            }

            while (client_socket.IsConnected())
            {
                process_packet.ReadData(client_socket);
            }

            lobby.Invoke(() =>
            {
                lobby.Close();
            });
        }
Exemple #2
0
        public void Listen()
        {
            this.sock_listen = true;

            listener_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listener_sock.Bind(new IPEndPoint(IPAddress.Any, host_port));
            listener_sock.Listen(16);


            while (lobby_form.Visible)
            {
                List <Socket> ReadList  = new List <Socket>(ConnectedSockets);
                List <Socket> ErrorList = new List <Socket>(ConnectedSockets);
                ReadList.Add(listener_sock);


                Socket.Select(ReadList, null, ErrorList, 1000);



                foreach (Socket client_sock in ReadList)
                {
                    if (client_sock == listener_sock)
                    {
                        ConnectedSockets.Add(client_sock.Accept());
                    }
                    else
                    {
                        if (client_sock.Available > 0)
                        {
                            process_packet.ReadData(client_sock);
                        }
                        else
                        {
                            ErrorList.Add(client_sock);
                        }
                    }
                }

                foreach (Socket client_sock in ErrorList)
                {
                    ConnectedSockets.Remove(client_sock);
                    string PlayerName  = PlayerSocketMap[client_sock];
                    player player_pack = PlayerToPack[PlayerName];

                    PlayerSocketMap.Remove(client_sock);

                    lobby_form.Invoke(() =>
                    {
                        lobby_form.RemovePlayer(PlayerName, player_pack);
                    });
                }
            }

            foreach (Socket client in ConnectedSockets)
            {
                if (client.Connected)
                {
                    client.Close();
                }

                client.Dispose();
            }

            listener_sock.Close();
            listener_sock.Dispose();
        }