Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10.  
  11. namespace RatSockServer
  12. {
  13. public class Program
  14. {
  15. //Both
  16. static string match = "test";
  17.  
  18. static int port = 1337;
  19.  
  20. //Server
  21. static IPAddress ipRecieve = IPAddress.Any;
  22.  
  23. static TcpListener server = new TcpListener(ipRecieve, port);
  24.  
  25. static List<Connection> connections = new List<Connection>();
  26.  
  27. static void Main(string[] args)
  28. {
  29. server.Start();
  30. Thread checkCon_Thread = new Thread(checkNewConnection);
  31. Thread returnList_Thread = new Thread(returnList);
  32. checkCon_Thread.Start();
  33. returnList_Thread.Start();
  34. }
  35.  
  36. static void checkNewConnection()
  37. {
  38. Connection connection = new Connection(null, server.AcceptTcpClient());
  39. if (connection.client != null)
  40. {
  41. Thread newCon_Thread = new Thread(()=>newConnection(connection));
  42. newCon_Thread.Start();
  43. }
  44. checkNewConnection();
  45. }
  46.  
  47. static void returnList()
  48. {
  49. if (connections.Count != 0)
  50. {
  51. foreach (var a_con in connections)
  52. {
  53. foreach (var a_str in a_con.connection)
  54. {
  55. Console.Write(a_str + " ");
  56. }
  57. Console.WriteLine();
  58. }
  59. }
  60. else
  61. {
  62. Console.WriteLine("Waiting for Connections");
  63. }
  64.  
  65. checkStates();
  66. Thread.Sleep(5000);
  67. Console.Clear();
  68. returnList();
  69. }
  70.  
  71. static void checkStates()
  72. {
  73. try
  74. {
  75. foreach (var a_con in connections)
  76. {
  77.  
  78. sendData(a_con.client, "check_state");
  79.  
  80. string r_raw = recieveData(a_con.client);
  81. string[] r_msg = null;
  82. if (r_raw != null)
  83. {
  84. r_msg = r_raw.Split('_');
  85. if (r_msg[0] == a_con.connection[1])
  86. {
  87. if (r_msg[1] != "alive")
  88. {
  89. connections.Remove(a_con);
  90. }
  91. }
  92. else
  93. {
  94. connections.Remove(a_con);
  95. }
  96. }
  97. else
  98. {
  99. connections.Remove(a_con);
  100. }
  101.  
  102.  
  103. }
  104. }
  105. catch (InvalidOperationException)
  106. {
  107.  
  108. }
  109. }
  110.  
  111. static void newConnection(Connection connection)
  112. {
  113. string r_Match = recieveData(connection.client);
  114. if (r_Match == match)
  115. {
  116. sendData(connection.client, "accept");
  117. addToList(connection);
  118. }
  119. }
  120.  
  121. static void addToList(Connection connection)
  122. {
  123. string[] t_connection = new string[3];
  124. string t_data = connections.Count+1 + "_" + recieveData(connection.client);
  125. t_connection = t_data.Split('_');
  126. connection.connection = t_connection;
  127. connections.Add(connection);
  128. }
  129.  
  130. static string recieveData(TcpClient client)
  131. {
  132. Byte[] bytes = new Byte[256];
  133. string data = null;
  134.  
  135. // Get a stream object for reading and writing
  136. NetworkStream stream = client.GetStream();
  137.  
  138. try
  139. {
  140. int i = stream.Read(bytes, 0, bytes.Length);
  141. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  142. }
  143. catch (IOException e)
  144. {
  145. data = null;
  146. }
  147.  
  148. return data;
  149. }
  150.  
  151. static void sendData(TcpClient client, string message)
  152. {
  153. // Translate the passed message into ASCII and store it as a Byte array.
  154. Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
  155.  
  156. NetworkStream stream = client.GetStream();
  157. stream.Write(data, 0, data.Length);
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement