Advertisement
SkyFlash21

Server-TCP

Jul 14th, 2021 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5.  
  6. namespace OpenComputer
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. TcpListener server = new TcpListener(IPAddress.Any, 3001);
  13. // we set our IP address as server's address, and we also set the port: 9999
  14.  
  15. server.Start(); // this will start the server
  16.  
  17. Console.WriteLine("Server Started");
  18. Socket client = server.AcceptSocket();
  19. while (true)
  20. {
  21.  
  22. Console.WriteLine("Connection accepted from " + client.RemoteEndPoint);
  23. while (true)
  24. {
  25. string command = Console.ReadLine();
  26. byte[] byData = System.Text.Encoding.ASCII.GetBytes(command + "\n");
  27. client.Send(byData);
  28. byte[] b = new byte[100];
  29. int k = client.Receive(b);
  30. for (int i = 0; i < k; i++)
  31. Console.Write(Convert.ToChar(b[i]));
  32. Console.WriteLine("");
  33. }
  34.  
  35. }
  36. }
  37.  
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement