Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. // ExecuteClient() Method
  2. static void ExecuteClient(string message)
  3. {
  4.  
  5. try
  6. {
  7.  
  8. // Establish the remote endpoint
  9. // for the socket. This example
  10. // uses port 11111 on the local
  11. // computer.
  12. IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
  13. Console.WriteLine(ipHost); //System.Net.IPHostEntry
  14. IPAddress ipAddr = ipHost.AddressList[0];
  15. Console.WriteLine(ipHost.AddressList.ToString()); //System.Net.IPAddress[]
  16. Console.WriteLine(ipAddr); //gives back an ip v6 address
  17. IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 11111);
  18. Console.WriteLine(localEndPoint);
  19. // Creation TCP/IP Socket using
  20. // Socket Class Costructor
  21. Console.WriteLine("AddressFamily: " + ipAddr.AddressFamily.ToString()); //InterNetworkV6
  22. Socket sender = new Socket(ipAddr.AddressFamily,
  23. SocketType.Stream, ProtocolType.Tcp);
  24.  
  25. try
  26. {
  27.  
  28. // Connect Socket to the remote
  29. // endpoint using method Connect()
  30. sender.Connect(localEndPoint);
  31.  
  32. // We print EndPoint information
  33. // that we are connected
  34. Console.WriteLine("Socket connected to -> {0} ",
  35. sender.RemoteEndPoint.ToString());
  36.  
  37. // Creation of messagge that
  38. // we will send to Server
  39. byte[] messageSent = Encoding.ASCII.GetBytes("<EOF> " + message);
  40. int byteSent = sender.Send(messageSent);
  41.  
  42. // Data buffer
  43. byte[] messageReceived = new byte[1024];
  44.  
  45. // We receive the messagge using
  46. // the method Receive(). This
  47. // method returns number of bytes
  48. // received, that we'll use to
  49. // convert them to string
  50. int byteRecv = sender.Receive(messageReceived);
  51. Console.WriteLine("Message from Server -> {0}",
  52. Encoding.ASCII.GetString(messageReceived,
  53. 0, byteRecv));
  54.  
  55. // Close Socket using
  56. // the method Close()
  57. sender.Shutdown(SocketShutdown.Both);
  58. sender.Close();
  59. }
  60.  
  61. // Manage of Socket's Exceptions
  62. catch (ArgumentNullException ane)
  63. {
  64.  
  65. Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
  66. }
  67.  
  68. catch (SocketException se)
  69. {
  70.  
  71. Console.WriteLine("SocketException : {0}", se.ToString());
  72. }
  73.  
  74. catch (Exception e)
  75. {
  76. Console.WriteLine("Unexpected exception : {0}", e.ToString());
  77. }
  78. }
  79.  
  80. catch (Exception e)
  81. {
  82.  
  83. Console.WriteLine(e.ToString());
  84. }
  85. }
  86.  
  87.  
  88. //ExecuteServer method
  89. public static void ExecuteServer()
  90. {
  91. // Establish the local endpoint
  92. // for the socket. Dns.GetHostName
  93. // returns the name of the host
  94. // running the application.
  95. IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
  96. IPAddress ipAddr = ipHost.AddressList[0];
  97. IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 11111);
  98.  
  99. // Creation TCP/IP Socket using
  100. // Socket Class Costructor
  101. Socket listener = new Socket(ipAddr.AddressFamily,
  102. SocketType.Stream, ProtocolType.Tcp);
  103.  
  104. try
  105. {
  106. // Using Bind() method we associate a
  107. // network address to the Server Socket
  108. // All client that will connect to this
  109. // Server Socket must know this network
  110. // Address
  111. listener.Bind(localEndPoint);
  112.  
  113. // Using Listen() method we create
  114. // the Client list that will want
  115. // to connect to Server
  116. listener.Listen(10);
  117. while (true)
  118. {
  119.  
  120. //Console.WriteLine("Waiting connection ... ");
  121.  
  122. // Suspend while waiting for
  123. // incoming connection Using
  124. // Accept() method the server
  125. // will accept connection of client
  126. Socket clientSocket = listener.Accept();
  127.  
  128. // Data buffer
  129. byte[] bytes = new Byte[1024];
  130. string data = null;
  131.  
  132. while (true)
  133. {
  134.  
  135. int numByte = clientSocket.Receive(bytes);
  136.  
  137. data += Encoding.ASCII.GetString(bytes,
  138. 0, numByte);
  139.  
  140. if (data.IndexOf("<EOF>") > -1)
  141. break;
  142. }
  143.  
  144. Console.WriteLine("Text received -> {0} ", data);
  145. if(data == "<EOF> " + "kill")
  146. {
  147. Application.Exit();
  148. } else if (data == "<EOF> " + "test")
  149. {
  150. Console.Writeline("It works!");
  151. } else
  152. {
  153. byte[] message = Encoding.ASCII.GetBytes("Error 404 message not found!");
  154. // Send a message to Client
  155. // using Send() method
  156. clientSocket.Send(message);
  157. Messagebox1();
  158. }
  159. // Close client Socket using the
  160. // Close() method. After closing,
  161. // we can use the closed Socket
  162. // for a new Client Connection
  163. clientSocket.Shutdown(SocketShutdown.Both);
  164. clientSocket.Close();
  165. }
  166. }
  167.  
  168. catch (Exception e)
  169. {
  170. //Console.WriteLine(e.ToString());
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement