Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Server
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- // Own thread for accepting connections
- Main ConnectionLoop = new Main(10);
- Thread thread = new Thread(new ThreadStart(ConnectionLoop.PrepareThread));
- thread.IsBackground = true;
- thread.Start();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Server
- {
- public class Main
- {
- private int m_backLog = 0;
- // constructor
- public Main(int Backlog)
- {
- m_backLog = Backlog;
- Console.WriteLine("Main class created, backlog: {0}", Backlog);
- }
- public void PrepareThread()
- {
- Console.WriteLine("Thread created");
- StartAccepting(CancellationToken.None).Wait();
- }
- private async Task StartAccepting(CancellationToken token)
- {
- Console.WriteLine("Started listening..");
- CancellationTokenSource cts = new CancellationTokenSource();
- TcpListener listener = new TcpListener(IPAddress.Any, 6112);
- listener.Start();
- await AcceptClientsAsync(listener, cts.Token);
- // Thread.Sleep(600000);
- }
- private async Task AcceptClientsAsync(TcpListener listener, CancellationToken token)
- {
- var clientCounter = 0;
- while (!token.IsCancellationRequested)
- {
- TcpClient client = await listener.AcceptTcpClientAsync().ConfigureAwait(false);
- clientCounter++;
- Console.WriteLine("Client {0} accepted!", clientCounter);
- await EchoAsync(client, clientCounter, token);
- }
- }
- private async Task EchoAsync(TcpClient client, int clientCounter, CancellationToken token)
- {
- using (client)
- {
- var buf = new byte[4096]; // buffer for stream
- var stream = client.GetStream(); // stream itself
- while (!token.IsCancellationRequested)
- {
- // some conditions we don't know is client connected, lets have timeout
- var timeoutTask = Task.Delay(TimeSpan.FromSeconds(15));
- var amountReadTask = stream.ReadAsync(buf, 0, buf.Length, token);
- var completedTask = await Task.WhenAny(timeoutTask, amountReadTask).ConfigureAwait(false);
- if (completedTask == timeoutTask)
- {
- var msg = Encoding.ASCII.GetBytes("Client timed out");
- await stream.WriteAsync(msg, 0, msg.Length);
- break;
- }
- var amountRead = amountReadTask.Result;
- if (amountRead == 0) break; // end of stream
- await stream.WriteAsync(buf, 0, amountRead, token).ConfigureAwait(false);
- }
- }
- Console.WriteLine("Client {0} disconnected", clientCounter);
- }
- }
- }
Add Comment
Please, Sign In to add comment