private void GoalKeeper(Shot shoot) { try { //init TcpClient tcpClient = new TcpClient(); tcpClient.Connect(IPAddress.Loopback, 8001); Stream GoalKeeperStream = tcpClient.GetStream(); //send byte[] MessageSend = Encoding.Default.GetBytes(JsonSerializer.Serialize(shoot)); GoalKeeperStream.Write(MessageSend, 0, MessageSend.Length); //recive byte[] MessageRecive = new byte[1000]; GoalKeeperStream.Read(MessageRecive, 0, MessageRecive.Length); string MessageReciveString = Encoding.Default.GetString(MessageRecive).Split("\0").FirstOrDefault(); GoalkeeperRequestedPosition goalkeeperRequestedPosition = JsonSerializer.Deserialize <GoalkeeperRequestedPosition>(MessageReciveString); _switchGoalKeeperSideMoving = goalkeeperRequestedPosition.X; //close GoalKeeperStream.Close(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
public static void Main() { //init IPAddress ipAd = IPAddress.Loopback; TcpListener myList = new TcpListener(ipAd, 8001); while (true) { try { myList.Start(); Socket s = myList.AcceptSocket(); Console.WriteLine("Connection accepted from " + s.RemoteEndPoint); //recive byte[] MessageRecive = new byte[1000]; s.Receive(MessageRecive); string MessageReciveString = Encoding.Default.GetString(MessageRecive).Split("\0") .FirstOrDefault(); Shot shot = JsonSerializer.Deserialize <Shot>(MessageReciveString); //calc var shotPixelRangeReal = CalculatePixelCoordinate(shot); //send GoalkeeperRequestedPosition MessageSend = new GoalkeeperRequestedPosition { X = (int)shotPixelRangeReal }; s.Send(Encoding.Default.GetBytes(JsonSerializer.Serialize(MessageSend))); Console.WriteLine("end"); s.Close(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); myList.Stop(); } } //close myList.Stop(); }