public void Run() { Player player = game.Player2; NetworkStream ns = null; TcpClient client = null; try { listener = new TcpListener(IPAddress.Any, 12000); listener.Start(1); client = listener.AcceptTcpClient(); view.AddToLog(String.Format("Подключен клиент {0}\nИграем за белых", client.Client.RemoteEndPoint.ToString())); ns = client.GetStream(); string playername = ReadString(ns); int lose, win; win = ReadInt(ns); lose = ReadInt(ns); player.Name = playername; player.SetStatistic(win, lose); // send player info player = game.Player1; WriteString(ns, player.Name); WriteInt(ns, player.GetWin()); WriteInt(ns, player.GetLose()); view.HideServerBanner(); CommandLoop(ns, view, game); client.Close(); } catch (Exception e) { view.Message(e.Message); } finally { if (ns != null) { ns.Close(); } if (client != null) { client.Close(); } if (listener != null) { // Остановим его listener.Stop(); } } }
public void Run() { TcpClient client = null; NetworkStream ns = null; Player player = game.Player2; try { client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse(server), port)); ns = client.GetStream(); view.AddToLog(String.Format("Подключен к серверу {0}:{1}\nИграем за черных", server, port)); WriteString(ns, player.Name); WriteInt(ns, player.GetWin()); WriteInt(ns, player.GetLose()); // get first player player = game.Player1; string playername = ReadString(ns); int lose, win; win = ReadInt(ns); lose = ReadInt(ns); player.Name = playername; player.SetStatistic(win, lose); view.Invoke(new Action(() => game.ClientGameView())); CommandLoop(ns, view, game); } catch (Exception e) { view.Message(e.Message); } finally { if (ns != null) { ns.Close(); } if (client != null) { client.Close(); } } }