Guest User

Untitled

a guest
Aug 25th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. using System.Data;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. namespace Server
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17.  
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. // Own thread for accepting connections
  21.  
  22. Main ConnectionLoop = new Main(10);
  23. Thread thread = new Thread(new ThreadStart(ConnectionLoop.PrepareThread));
  24. thread.IsBackground = true;
  25. thread.Start();
  26. }
  27. }
  28. }
  29.  
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Net;
  34. using System.Net.Sockets;
  35. using System.Text;
  36. using System.Threading;
  37. using System.Threading.Tasks;
  38.  
  39. namespace Server
  40. {
  41. public class Main
  42. {
  43. private int m_backLog = 0;
  44.  
  45. // constructor
  46. public Main(int Backlog)
  47. {
  48. m_backLog = Backlog;
  49. Console.WriteLine("Main class created, backlog: {0}", Backlog);
  50. }
  51.  
  52. public void PrepareThread()
  53. {
  54. Console.WriteLine("Thread created");
  55. StartAccepting(CancellationToken.None).Wait();
  56. }
  57.  
  58. private async Task StartAccepting(CancellationToken token)
  59. {
  60. Console.WriteLine("Started listening..");
  61. CancellationTokenSource cts = new CancellationTokenSource();
  62. TcpListener listener = new TcpListener(IPAddress.Any, 6112);
  63. listener.Start();
  64. await AcceptClientsAsync(listener, cts.Token);
  65. // Thread.Sleep(600000);
  66. }
  67.  
  68. private async Task AcceptClientsAsync(TcpListener listener, CancellationToken token)
  69. {
  70. var clientCounter = 0;
  71. while (!token.IsCancellationRequested)
  72. {
  73. TcpClient client = await listener.AcceptTcpClientAsync().ConfigureAwait(false);
  74. clientCounter++;
  75. Console.WriteLine("Client {0} accepted!", clientCounter);
  76. await EchoAsync(client, clientCounter, token);
  77. }
  78. }
  79.  
  80. private async Task EchoAsync(TcpClient client, int clientCounter, CancellationToken token)
  81. {
  82. using (client)
  83. {
  84. var buf = new byte[4096]; // buffer for stream
  85. var stream = client.GetStream(); // stream itself
  86.  
  87. while (!token.IsCancellationRequested)
  88. {
  89. // some conditions we don't know is client connected, lets have timeout
  90. var timeoutTask = Task.Delay(TimeSpan.FromSeconds(15));
  91. var amountReadTask = stream.ReadAsync(buf, 0, buf.Length, token);
  92. var completedTask = await Task.WhenAny(timeoutTask, amountReadTask).ConfigureAwait(false);
  93.  
  94. if (completedTask == timeoutTask)
  95. {
  96. var msg = Encoding.ASCII.GetBytes("Client timed out");
  97. await stream.WriteAsync(msg, 0, msg.Length);
  98. break;
  99. }
  100.  
  101. var amountRead = amountReadTask.Result;
  102. if (amountRead == 0) break; // end of stream
  103. await stream.WriteAsync(buf, 0, amountRead, token).ConfigureAwait(false);
  104. }
  105. }
  106. Console.WriteLine("Client {0} disconnected", clientCounter);
  107. }
  108. }
  109. }
Add Comment
Please, Sign In to add comment