Advertisement
nevachana

Multi client server FORM c#

Jun 16th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Net;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Text.RegularExpressions;
  11.  
  12. namespace Fserver
  13. {
  14.     class Conexion
  15.     {
  16.         private TcpClient client;
  17.         private TcpListener listener;
  18.         private Dictionary<TcpClient, int> list = new Dictionary<TcpClient, int>();
  19.         private int initId = 0;
  20.  
  21.         private Label updateUsers = new Label();
  22.         private ListBox updateUserList = new ListBox();
  23.         private RichTextBox updateLogs = new RichTextBox();
  24.  
  25.         public bool allowConexions = true;
  26.        
  27.         public void Start(Label lb , ListBox list,RichTextBox txt)
  28.         {
  29.             this.listener = new TcpListener(IPAddress.Any, 4000);
  30.             this.listener.Start();
  31.             this.updateUsers = lb;
  32.             this.updateUserList = list;
  33.             this.updateLogs = txt;
  34.             while (allowConexions && this.list.Count < 100)
  35.             {
  36.                 this.client = this.listener.AcceptTcpClient();
  37.                 this.handleClient(this.client, initId);            
  38.             }
  39.             txt.Text += "\n Conexion denied";
  40.         }
  41.         private void handleClient(TcpClient c,int id)
  42.         {
  43.             this.initId++;
  44.             this.updateUsers.Text = this.list.Count().ToString();
  45.             this.updateUserList.Items.Add("ClientId:" + this.initId.ToString());
  46.             this.list.Add(c, id);
  47.             new Thread(() => startRead(c,id)).Start();
  48.  
  49.         }
  50.         private void startRead(TcpClient client,int id)
  51.         {
  52.             NetworkStream stream = client.GetStream();
  53.             while (client.Connected)
  54.             {
  55.                 try
  56.                 {
  57.                     byte[] buffer = new byte[client.Available];
  58.                     stream.Read(buffer, 0, buffer.Length);
  59.                     this.handleData(int.Parse(this.parsePacket(0, buffer)), this.parsePacket(1, buffer));
  60.                     this.updateLogs.Text += "\n Client sent: " + Encoding.Default.GetString(buffer);
  61.                     stream.Flush();
  62.                 }
  63.                 catch
  64.                 {
  65.                     this.updateLogs.Text += "\n Error reading client,closing..";
  66.                     stream.Flush();
  67.                     stream.Close();
  68.                     client.Close();
  69.                 }
  70.                 Thread.Sleep(1);
  71.             }
  72.             this.initId--;
  73.             this.updateUserList.Items.Remove("ClientId:" + id.ToString());
  74.             this.list.Remove(client);
  75.             this.updateUsers.Text = this.initId.ToString();
  76.             this.updateLogs.Text += "\n Client closed";
  77.         }
  78.         private void handleData(int id, string body)
  79.         {
  80.             switch (id)
  81.             {
  82.                 case 0:
  83.                     break;
  84.             }
  85.         }
  86.         private string parsePacket(int param, byte[] buffer)
  87.         {
  88.             return Regex.Split(Encoding.Default.GetString(buffer), "|")[param];
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement