Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Sockets;
  7. using System.Net;
  8.  
  9. namespace ConsoleApp1
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. TcpListener server = null;
  16. try
  17. {
  18. int Zmey1direction = 2;
  19. int Zmey2direction = 3;
  20. int Zmey3direction = 4;
  21. int Zmey4direction = 1;
  22. int Start = 0;
  23. int BerryX = 15;
  24. int BerryY = 15;
  25. int port = 8888;
  26. IPAddress localAddr = IPAddress.Parse("127.0.0.1");
  27. server = new TcpListener(localAddr, port);
  28. server.Start();
  29. while (true)
  30. {
  31. string mess = "";
  32. Random rnd = new Random();
  33. TcpClient client = server.AcceptTcpClient();
  34. NetworkStream stream = client.GetStream();
  35. byte[] data = new byte[256];
  36. do
  37. {
  38. int bytes = stream.Read(data, 0, data.Length);
  39. mess += Encoding.UTF8.GetString(data, 0, bytes);
  40. } while (stream.DataAvailable);
  41. if (mess[0] != '?')
  42. {
  43. Console.WriteLine(mess);
  44. }
  45. if (mess == "Zmey1 1")
  46. Zmey1direction = 1;
  47. else if (mess == "Zmey1 2")
  48. Zmey1direction = 2;
  49. else if (mess == "Zmey1 3")
  50. Zmey1direction = 3;
  51. else if (mess == "Zmey1 4")
  52. Zmey1direction = 4;
  53.  
  54. else if (mess == "Zmey2 1")
  55. Zmey1direction = 1;
  56. else if (mess == "Zmey2 2")
  57. Zmey1direction = 2;
  58. else if (mess == "Zmey2 3")
  59. Zmey1direction = 3;
  60. else if (mess == "Zmey2 4")
  61. Zmey1direction = 4;
  62.  
  63. else if (mess == "Zmey3 1")
  64. Zmey1direction = 1;
  65. else if (mess == "Zmey3 2")
  66. Zmey1direction = 2;
  67. else if (mess == "Zmey3 3")
  68. Zmey1direction = 3;
  69. else if (mess == "Zmey3 4")
  70. Zmey1direction = 4;
  71.  
  72. else if (mess == "Zmey4 1")
  73. Zmey1direction = 1;
  74. else if (mess == "Zmey4 2")
  75. Zmey1direction = 2;
  76. else if (mess == "Zmey4 3")
  77. Zmey1direction = 3;
  78. else if (mess == "Zmey4 4")
  79. Zmey1direction = 4;
  80.  
  81. else if (mess == "Pause")
  82. {
  83. if (Start == 1)
  84. Start = 0;
  85. else
  86. Start = 1;
  87. }
  88. else if (mess[0] == '?')
  89. {
  90. string response = Zmey1direction.ToString() + Zmey2direction.ToString() + Zmey3direction.ToString() + Zmey4direction.ToString() + Start.ToString();
  91. data = Encoding.UTF8.GetBytes(response);
  92. stream.Write(data, 0, data.Length);
  93. }
  94.  
  95.  
  96. stream.Close();
  97. client.Close();
  98. }
  99. }
  100. catch (Exception ee)
  101. {
  102. Console.WriteLine(ee.Message);
  103. }
  104. finally
  105. {
  106. if (server != null) server.Stop();
  107. }
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement