コード例 #1
0
        /// <summary>
        /// Creates a NetworkClient by being a host
        /// </summary>
        /// <returns></returns>
        public static NetworkOpponentClient HostCreate(string localAddress, string myNickname)
        {
            IPAddress ipAddr = IPAddress.Parse(localAddress);

            TcpListener listener = new TcpListener(ipAddr, Port);

            listener.Start();

            Debug.WriteLine("Server running...");
            Debug.WriteLine("Local End point: " + listener.LocalEndpoint);

            Console.WriteLine("En attente du joueur adverse...");

            Socket socket = listener.AcceptSocket();

            Debug.WriteLine("Une connexion a été accepté depuis " + socket.RemoteEndPoint);

            listener.Stop(); // No needing to listen for others players anymore since we got it

            var nc = new NetworkOpponentClient(socket);

            nc.ExchangeInitInfos(myNickname);

            return(nc);
        }
コード例 #2
0
        private void CreateGame()
        {
            Player myPlayer = new Player(1, _nickname, new Pawn('O')
            {
                Color = ConsoleColor.Red
            });

            Console.Write("Adresse locale de partage : ");
#if RELEASE
            string ipAddrStr = Console.ReadLine();
#else
            string ipAddrStr = "127.0.0.1";
            Console.WriteLine();
#endif

            NetworkOpponentClient nc = NetworkOpponentClient.HostCreate(ipAddrStr);

            string opposingNickname = nc.ExchangeInitInfos(_nickname);

            //try
            //{
            //    IPAddress ipAddr = IPAddress.Parse(ipAddrStr);
            //    TcpListener listener = new TcpListener(ipAddr, Port);
            //    listener.Start();

            //    Debug.WriteLine("Server running...");
            //    Debug.WriteLine("Local End point: " + listener.LocalEndpoint);

            //    Console.WriteLine("En attente du joueur adverse...");

            //    Socket socket = listener.AcceptSocket();
            //    Debug.WriteLine("Une connexion a été accepté depuis " + socket.RemoteEndPoint);

            //    listener.Stop();

            //    _nstm = new NetworkStream(socket);

            //    // Getting opposing player object
            //    Player opposingPlayer = (Player)_formatter.Deserialize(_nstm);
            //    Console.WriteLine($"Vous êtes contre le joueur {opposingPlayer.Name}");

            //    // Sending my player object
            //    _formatter.Serialize(_nstm, myPlayer);

            //    Console.WriteLine("La partie va pouvoir commencer");

            //    PlayGame(myPlayer, opposingPlayer);

            //    socket.Close();
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine("Error: " + e.StackTrace);
            //}
        }
コード例 #3
0
        public static NetworkOpponentClient JoinHost(string hostAddress, string myNickname)
        {
            TcpClient client = new TcpClient();

            Console.WriteLine("Connexion en cours...");

            client.Connect(hostAddress, Port);
            Console.WriteLine("Connecté à l'adresse " + hostAddress);

            var nc = new NetworkOpponentClient(client.Client);

            nc.ExchangeInitInfos(myNickname);

            return(nc);
        }