Guest User

Untitled

a guest
Jul 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5.  
  6. namespace test
  7. {
  8. class Program
  9. {
  10. static TcpListener listener;
  11. static void Main(string[] args)
  12. {
  13. listener = new TcpListener(new IPEndPoint(IPAddress.Any, 3349));
  14. listener.Start();
  15. listener.BeginAcceptSocket(Accept, null);
  16. Console.WriteLine("Started!");
  17.  
  18. // Simulate a random other client connecting, nc localhost 3349 does the same thing
  19. var client = new TcpClient();
  20. client.Connect("localhost", 3349);
  21.  
  22. Thread.Sleep(2000);
  23. listener.Stop();
  24.  
  25. Console.WriteLine("Done!");
  26. }
  27.  
  28. static void Accept(IAsyncResult result)
  29. {
  30. using(var socket = listener.EndAcceptSocket(result))
  31. {
  32. Console.WriteLine("Accepted socket");
  33. Thread.Sleep(500);
  34. socket.Shutdown(SocketShutdown.Both);
  35. }
  36.  
  37. Console.WriteLine("Socket fully closed");
  38. }
  39. }
  40. }
  41.  
  42. dotnet --version
  43. 2.1.103
  44.  
  45. lsb_release -a
  46. No LSB modules are available.
  47. Distributor ID: Ubuntu
  48. Description: Ubuntu 16.04.4 LTS
  49. Release: 16.04
  50. Codename: xenial
Add Comment
Please, Sign In to add comment