Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using UnityEngine;
  4. using System.Net.Sockets;
  5. using System.Text;
  6.  
  7. public class SocketServer : MonoBehaviour
  8. {
  9. [Header("Socket Configuration")]
  10. public ProtocolType Protocol;
  11. public int Port = 9000;
  12.  
  13. System.Threading.Thread SocketThread;
  14. volatile bool keepReading = false;
  15.  
  16. // Use this for initialization
  17. void Start()
  18. {
  19. Application.runInBackground = true;
  20. startServer();
  21. }
  22.  
  23. void startServer()
  24. {
  25. SocketThread = new System.Threading.Thread(networkCode);
  26. SocketThread.IsBackground = true;
  27. SocketThread.Start();
  28. }
  29.  
  30. private string getIPAddress()
  31. {
  32. IPHostEntry host;
  33. string localIP = "";
  34. host = Dns.GetHostEntry(Dns.GetHostName());
  35. foreach (IPAddress ip in host.AddressList)
  36. {
  37. if (ip.AddressFamily == AddressFamily.InterNetwork)
  38. {
  39. localIP = ip.ToString();
  40. }
  41.  
  42. }
  43. return localIP;
  44. }
  45.  
  46.  
  47. Socket listener;
  48. Socket handler;
  49.  
  50. void networkCode()
  51. {
  52. string data;
  53.  
  54. // Data buffer for incoming data.
  55. byte[] bytes = new Byte[1024];
  56.  
  57. // host running the application.
  58. Debug.Log("Ip " + getIPAddress().ToString());
  59. IPAddress[] ipArray = Dns.GetHostAddresses(getIPAddress());
  60. IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], Port);
  61.  
  62. // Create a TCP/IP socket.
  63. listener = new Socket(ipArray[0].AddressFamily,
  64. SocketType.Stream, Protocol);
  65.  
  66. // Bind the socket to the local endpoint and
  67. // listen for incoming connections.
  68.  
  69. try
  70. {
  71. listener.Bind(localEndPoint);
  72. listener.Listen(10);
  73.  
  74. // Start listening for connections.
  75. while (true)
  76. {
  77. keepReading = true;
  78.  
  79. // Program is suspended while waiting for an incoming connection.
  80. Debug.Log("Waiting for Connection"); //It works
  81.  
  82. handler = listener.Accept();
  83. Debug.Log("Client Connected"); //It doesn't work
  84. data = null;
  85.  
  86. // An incoming connection needs to be processed.
  87. while (keepReading)
  88. {
  89. bytes = new byte[1024];
  90. int bytesRec = handler.Receive(bytes);
  91. Debug.Log("Received from Server");
  92.  
  93. if (bytesRec <= 0)
  94. {
  95. keepReading = false;
  96. handler.Disconnect(true);
  97. break;
  98. }
  99.  
  100. data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
  101. Debug.Log("Data: " + data);
  102. if (data.IndexOf("<EOF>") > -1)
  103. {
  104. break;
  105. }
  106.  
  107. System.Threading.Thread.Sleep(1);
  108. }
  109.  
  110. System.Threading.Thread.Sleep(1);
  111. }
  112. }
  113. catch (Exception e)
  114. {
  115. Debug.Log(e.ToString());
  116. }
  117. }
  118.  
  119. void stopServer()
  120. {
  121. keepReading = false;
  122.  
  123. //stop thread
  124. if (SocketThread != null)
  125. {
  126. SocketThread.Abort();
  127. }
  128.  
  129. if (handler != null && handler.Connected)
  130. {
  131. handler.Disconnect(false);
  132. Debug.Log("Disconnected!");
  133. }
  134. }
  135.  
  136. void OnDisable()
  137. {
  138. stopServer();
  139. Debug.Log("Stoped");
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement