Advertisement
nevachana

Untitled

Jun 19th, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Server
  11. {
  12.     class tcpConexion
  13.     {
  14.         private TcpClient client;
  15.         private TcpListener listener;
  16.         private Server s = new Server();
  17.         private List<Tuple<TcpClient, int>> clientList = new List<Tuple<TcpClient, int>>();
  18.         public void startServer()
  19.         {
  20.             new Thread(() => this.Listener()).Start();
  21.         }
  22.         private void Listener()
  23.         {
  24.             this.listener = new TcpListener(IPAddress.Any, 3030);
  25.             this.listener.Start();
  26.             while (true)
  27.             {
  28.                 this.client = this.listener.AcceptTcpClient();
  29.                 handleClient.newClient(this.client);
  30.             }
  31.         }
  32.         public void startReading(TcpClient client, int clientID)
  33.         {
  34.             this.clientList.Add(new Tuple<TcpClient, int>(client, clientID));
  35.             this.s.Write(string.Format("{0} Client has connected!", clientID));
  36.             s.updateConsoleUsers(this.clientList.Count);
  37.             NetworkStream stream = client.GetStream();
  38.             Byte[] buffer = new Byte[client.Available];
  39.             while (client.Connected)
  40.             {
  41.                 try
  42.                 {
  43.                     stream.Read(buffer, 0, buffer.Length);
  44.                     this.s.Write(string.Format("client: {0} has sent: {1}", clientID, Encoding.Default.GetString(buffer)));
  45.                     stream.Flush();
  46.                 }
  47.                 catch
  48.                 {
  49.                     this.clientList.Remove(new Tuple<TcpClient, int>(client, clientID));
  50.                     s.updateConsoleUsers(this.clientList.Count);
  51.                     stream.Flush();
  52.                     stream.Close();
  53.                     client.Close();
  54.                     this.s.Write(string.Format("{0} has disconnected", clientID));
  55.                 }
  56.             }
  57.         }
  58.         public void handleData(int packetID, string body)
  59.         {
  60.             switch (packetID)
  61.             {
  62.                 case 0:
  63.                     break;
  64.             }
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement