Advertisement
Garey

AlliedMods Answer

Oct 24th, 2019
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. Well, what's the problem then? You have TcpListener and TcpClient in C#. You have a port onto where to listen for what you want and you can send back some data. Let me give you an example and be warned, I'm not a C# expert, I just have some knowledge of it.
  2.  
  3. Here we have the server code:
  4. [PHP]
  5. namespace Server
  6. {
  7. class Program
  8. {
  9. const int PORT = 5000;
  10. const string HOST = "127.0.0.1";
  11.  
  12. static void Main(string[] args)
  13. {
  14. /*! Listen at the specified host and port number */
  15. IPAddress hostAddress = IPAddress.Parse(HOST);
  16. TcpListener listener = new TcpListener(hostAddress, PORT);
  17. Console.WriteLine("Listening...");
  18. listener.Start();
  19.  
  20. /*! Accept incoming client *//
  21. TcpClient client = listener.AcceptTcpClient();
  22.  
  23. /*! Get the incoming data through a network stream */
  24. NetworkStream networkStream = client.GetStream();
  25. byte[] buffer = new byte[client.ReceiveBufferSize];
  26.  
  27. /*! Read the data from the network stream */
  28. int bytesRead = networkStream.Read(buffer, 0, client.ReceiveBufferSize);
  29.  
  30. /*! Convert the received data into string and print it out */
  31. string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
  32. Console.WriteLine("Received : " + dataReceived);
  33.  
  34. /*! Return a response to the client */
  35. Console.WriteLine("Sending back: " + dataReceived);
  36. networkStream.Write(buffer, 0, bytesRead);
  37. client.Close();
  38. listener.Stop();
  39. Console.ReadLine();
  40. }
  41. }
  42. }
  43. [/PHP]
  44.  
  45. And we have a client (again in C#) just so you are able get the idea how the transmission of data through network actually works:
  46.  
  47. [PHP]
  48. namespace Client
  49. {
  50. class Program
  51. {
  52. const int PORT = 5000;
  53. const string HOST = "127.0.0.1";
  54. static void Main(string[] args)
  55. {
  56. /*! Data to send to the server */
  57. string message = DateTime.Now.ToString();
  58.  
  59. /*! Create a TCPClient object to connect to the host with port*/
  60. TcpClient client = new TcpClient(HOST, PORT);
  61. NetworkStream networkStream= client.GetStream();
  62. byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(message);
  63.  
  64. /*! Send the message */
  65. Console.WriteLine("Sending: " + message);
  66. networkStream.Write(bytesToSend, 0, bytesToSend.Length);
  67.  
  68. //---read back the text---
  69. byte[] bytesToRead = new byte[client.ReceiveBufferSize];
  70. int bytesRead = networkStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
  71. Console.WriteLine("Received: " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
  72. Console.ReadLine();
  73. client.Close();
  74. }
  75. }
  76. }
  77. [/PHP]
  78.  
  79. You can learn much more onto how the HTTP servers and listeners work overall at the RFC2616 HTTP/1.1 - https://www.ietf.org/rfc/rfc2616.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement