Advertisement
Guest User

Untitled

a guest
May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Net.Sockets;
  5. using System.Net;
  6. using System;
  7. using System.Threading;
  8. using UnityEngine.UI;
  9.  
  10. public class Network : MonoBehaviour {
  11.  
  12. UdpClient udpClient = new UdpClient();
  13. //NetConnection con = new NetConnection("127.0.0.1",20000);
  14.  
  15. // Use this for initialization
  16. void Start () {
  17. //ConnectTCP("127.0.0.1",80,"GET /response.php HTTP/1.1\r\nHost: localhost:80\r\n\r\n");
  18. //ConnectUDP("127.0.0.1",20000,"Hello UDP");
  19. NetConnection con = new NetConnection("127.0.0.1",20000);
  20. con.Send("Hello");
  21. }
  22.  
  23. // Update is called once per frame
  24. void Update () {
  25.  
  26. }
  27.  
  28. void ConnectTCP(String server,Int32 port,String message)
  29. {
  30. try
  31. {
  32. TcpClient client = new TcpClient(server,port);
  33.  
  34. Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
  35.  
  36. NetworkStream stream = client.GetStream();
  37. stream.Write(data,0,data.Length);
  38.  
  39. data = new Byte[1024];
  40.  
  41. //String response; // = String.Empty;
  42. Int32 numbytes = stream.Read(data,0,data.Length);
  43.  
  44. String response = System.Text.Encoding.ASCII.GetString(data,0,numbytes);
  45. Debug.Log("Received: "+response);
  46.  
  47. int Result = response.IndexOf("\r\n\r\n");
  48. if (Result!=-1) Debug.Log("Data: "+response.Substring(Result+4));
  49.  
  50. stream.Close();
  51. client.Close();
  52. }
  53. catch(ArgumentNullException e)
  54. {
  55. Debug.Log("NullException: "+e);
  56. }
  57. catch(SocketException e)
  58. {
  59. Debug.Log("SocketException: "+e);
  60. }
  61. }
  62.  
  63. void ConnectUDP(String Server,Int32 port,String message)
  64. {
  65. try
  66. {
  67. udpClient.Connect(Server,port);
  68.  
  69. Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
  70.  
  71. udpClient.Send(data,data.Length);
  72.  
  73. IPEndPoint endPoint = new IPEndPoint(IPAddress.Any,0);
  74.  
  75. Byte[] receivedBytes = udpClient.Receive(ref endPoint);
  76. String response = System.Text.Encoding.ASCII.GetString(receivedBytes);
  77.  
  78. Debug.Log(response);
  79.  
  80. udpClient.Close();
  81.  
  82. }
  83. catch(Exception e)
  84. {
  85. Console.WriteLine(e.ToString());
  86. }
  87. }
  88.  
  89. public class Receiver
  90. {
  91. public UdpClient udpSocket;
  92. IPEndPoint endPoint;
  93. public Thread thread;
  94. public Boolean running = true;
  95.  
  96. public Receiver(UdpClient udpClient)
  97. {
  98. udpSocket = udpClient;
  99. endPoint = new IPEndPoint(IPAddress.Any,0);
  100.  
  101. thread = new Thread(Thread);
  102. thread.Start();
  103. }
  104.  
  105. public void Thread()
  106. {
  107. while(running)
  108. {
  109. try {
  110. Byte[] receivedBytes = udpSocket.Receive(ref endPoint);
  111. String response = System.Text.Encoding.ASCII.GetString(receivedBytes);
  112.  
  113. Debug.Log(response);
  114. }
  115. catch(Exception e)
  116. {
  117. Console.WriteLine(e.ToString());
  118. }
  119. }
  120. }
  121. }
  122.  
  123. public class NetConnection
  124. {
  125. UdpClient Client = new UdpClient();
  126. Receiver recv;
  127.  
  128. public NetConnection(String server,Int32 port)
  129. {
  130. Client.Connect(server,port);
  131. recv = new Receiver(Client);
  132. }
  133.  
  134. ~NetConnection()
  135. {
  136. recv.udpSocket.Close();
  137. Client.Close();
  138. recv.running = false;
  139. recv.thread.Abort();
  140. recv.thread.Join();
  141. Debug.Log("terminated");
  142. }
  143.  
  144. public void Send(String message)
  145. {
  146. Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
  147.  
  148. Client.Send(data,data.Length);
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement