Guest User

Untitled

a guest
Apr 18th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Collections.Generic;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             TcpListener serverSocket = new TcpListener(8888);
  14.             TcpClient clientSocket = default(TcpClient);
  15.             int counter = 0;
  16.  
  17.             serverSocket.Start();
  18.             Console.WriteLine("Szerver elindítva!");
  19.  
  20.             counter = 0;
  21.             while (true)
  22.             {
  23.                 counter += 1;
  24.                 clientSocket = serverSocket.AcceptTcpClient();
  25.                 Console.WriteLine("Kliens(" + Convert.ToString(counter) + ") elindítva!");
  26.                 handleClinet client = new handleClinet();
  27.                 client.startClient(clientSocket, counter.ToString());
  28.             }
  29.  
  30.             clientSocket.Close();
  31.             serverSocket.Stop();
  32.             Console.WriteLine(" >> " + "exit");
  33.             Console.ReadLine();
  34.         }
  35.     }
  36.  
  37.     //Class to handle each client request separatly
  38.     public class handleClinet
  39.     {
  40.         List<TcpClient> clientSocket = new List<TcpClient>();
  41.         TcpClient jelenlegiSocket = new TcpClient();
  42.         string clNo;
  43.         public void startClient(TcpClient inClientSocket, string clineNo)
  44.         {
  45.             this.clientSocket.Add(inClientSocket);
  46.             jelenlegiSocket = inClientSocket;
  47.             this.clNo = clineNo;
  48.             Thread ctThread = new Thread(doChat);
  49.             ctThread.Start();
  50.         }
  51.         private void doChat()
  52.         {
  53.             byte[] bytesFrom = new byte[10025];
  54.             string bejovo_uzenet = null;
  55.             Byte[] sendBytes = null;
  56.             string serverResponse = null;
  57.  
  58.             while ((true))
  59.             {
  60.                 try
  61.                 {
  62.                     NetworkStream networkStream = jelenlegiSocket.GetStream();
  63.                     networkStream.Read(bytesFrom, 0, (int)jelenlegiSocket.ReceiveBufferSize);
  64.                     bejovo_uzenet = System.Text.Encoding.ASCII.GetString(bytesFrom);
  65.                     bejovo_uzenet = bejovo_uzenet.Substring(0, bejovo_uzenet.IndexOf("$"));
  66.                     Console.WriteLine(bejovo_uzenet);
  67.  
  68.                     foreach (TcpClient tcp in clientSocket)
  69.                     {
  70.                         serverResponse = bejovo_uzenet;
  71.                         sendBytes = Encoding.ASCII.GetBytes(serverResponse);
  72.                         tcp.GetStream().Write(sendBytes, 0, sendBytes.Length);
  73.                         tcp.GetStream().Flush();
  74.                     }
  75.                 }
  76.  
  77.                 catch (Exception ex)
  78.                 {
  79.                     Console.WriteLine(" >> " + ex.ToString());
  80.                 }
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment