Exemplo n.º 1
0
        /// <summary>
        /// Start a new thread for each client that is connected.
        /// Then adds that client to the ClientSocket list
        /// </summary>
        private void ListenForClients()
        {
            try
            {
                while(IsRunning)
                {
                    // Check if the listener is pending so we can stop it later
                    if (!listener.Pending())
                    {
                        Thread.Sleep(500);
                        continue;
                    }

                    Socket ConnectedClientSocket = listener.AcceptSocket();
                    // When the tcpListener gets a new connection inject the socket into a new ClientSocket object
                    // and start a new thread for that ClientSocket.
                    ClientSocket client = new ClientSocket(ConnectedClientSocket);
                    ClientThread = new Thread(new ThreadStart(client.ReadSocket));
                    ClientThread.IsBackground = true;
                    ClientThread.Start();
                    ClientSockets.Add(client);
                    // Make sure the ClientSocket has all a list of all the other connected sockets.
                    UpdateAllClientLists();
                    Console.WriteLine("Client Connected: " + ConnectedClientSocket.RemoteEndPoint);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("server: " + e.Message);
            }
        }
Exemplo n.º 2
0
        private void SendGameInviteToOpponent()
        {
            ClientSocket Opponent = FindOpponent();

            Opponent.WriteToSocket(PacketParser.MakePackageString(new Package("newgame", GamePackage.From, GamePackage.To, GamePackage.Data)));
        }