Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Google.Protobuf;
  10. using Google.Protobuf.Examples.AddressBook;
  11.  
  12. namespace App
  13. {
  14.     public class TcpServer
  15.     {
  16.         private readonly TcpListener listener;
  17.         private readonly List<TcpClient> pool;
  18.  
  19.         public TcpServer(IPAddress ip, int port)
  20.         {
  21.             if (ip == null)
  22.                 throw new NullReferenceException();
  23.  
  24.             if (port < 0)
  25.                 throw new Exception();
  26.  
  27.             listener = new TcpListener(ip, port);
  28.             pool = new List<TcpClient>();
  29.         }
  30.  
  31.         public async Task<TcpClient> Start(int? backlog = null)
  32.         {
  33.             if (backlog.HasValue)
  34.                 listener.Start(backlog.Value);
  35.             else
  36.                 listener.Start();
  37.  
  38.             Console.WriteLine("Server started. Listening to the TCP clients...");
  39.             return await listener.AcceptTcpClientAsync();
  40.         }
  41.  
  42.         public async Task HandleNewConnection()
  43.         {
  44.             pool.Add(await Start());
  45.             Console.WriteLine("Client has been connected. Waiting for the input data...");
  46.  
  47.             Task.Factory.StartNew(async() => await HandleInputData(pool[pool.Count - 1]));
  48.             await HandleNewConnection();
  49.         }
  50.  
  51.         private async Task HandleInputData(TcpClient client)
  52.         {
  53.             var data = new byte[4096];
  54.             var cts  = new CancellationTokenSource();
  55.  
  56.             var john = new Person
  57.             {
  58.                 Id = 1234,
  59.                 Name = "John Doe",
  60.                 Email = "jdoe@example.com",
  61.                 Phones = { new Person.Types.PhoneNumber { Number = "555-4321", Type = Person.Types.PhoneType.Home } }
  62.             };
  63.  
  64.             var ms = new MemoryStream();
  65.             var os = new CodedOutputStream(ms);
  66.             john.WriteTo(os);
  67.             os.Flush();
  68.             var rawData = ms.ToArray();
  69.             var str = Encoding.UTF8.GetString(rawData);
  70.  
  71.             var networkStream = client.GetStream();
  72.             var hwText = Encoding.UTF8.GetBytes(Convert.ToBase64String(rawData));
  73.  
  74.             await networkStream.WriteAsync(hwText, 0, hwText.Length, cts.Token);
  75.             var bytesRead = await networkStream.ReadAsync(data, 0, data.Length, cts.Token);
  76.  
  77.             if (bytesRead > 0)
  78.             {
  79.                 var text = Encoding.UTF8.GetString(data, 0, bytesRead);
  80.                 Console.WriteLine($"Response is: {text}");
  81.             }
  82.  
  83.             await HandleInputData(client);
  84.         }
  85.     }
  86.  
  87.     class Program
  88.     {
  89.         private const string ip = "127.0.0.1";
  90.         private const int port = 8080;
  91.  
  92.         [MTAThread]
  93.         public static void Main()
  94.         {
  95.             Task.Factory.StartNew(async() =>
  96.             {
  97.                 var server = new TcpServer(IPAddress.Parse(ip), port);
  98.                 await server.HandleNewConnection();
  99.             });
  100.  
  101.             Console.ReadLine();
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement