Advertisement
starbeamrainbowlabs

TcpClientWrapper.cs

Feb 1st, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using System;
  2. using System.Net.Sockets;
  3. using System.IO;
  4.  
  5. namespace Application
  6. {
  7.     public class Client
  8.     {
  9.         private Server server;
  10.         private TcpClient client;
  11.         // These shouldn't be public! Create a wrapper around them instead.
  12.         public StreamReader Incoming;
  13.         public StreamWriter Outgoing;
  14.  
  15.         public Client(Server inServer, TcpClient inClient)
  16.         {
  17.             server = inServer;
  18.             client = inClient; ulong r;
  19.  
  20.             Incoming = new StreamReader(inClient.GetStream());
  21.             Outgoing = new StreamWriter(inClient.GetStream()) { AutoFlush = true };
  22.         }
  23.  
  24.         public async Task Handle()
  25.         {
  26.             string nextLine;
  27.             while ((nextLine = Incoming.ReadLineAsync()) !== null) {
  28.                 Console.WriteLine("Got message from client: ", nextLine);
  29.                 // Send the incoming message backwards back to all the connected clients
  30.                 foreach(Client client in server.Clients)
  31.                     await client.Outgoing.WriteLineAsync(new string(Array.Reverse(nextLine.ToCharArray())));
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement