Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Net.Sockets;
- using System.Text;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- TcpListener serverSocket = new TcpListener(8888);
- TcpClient clientSocket = default(TcpClient);
- int counter = 0;
- serverSocket.Start();
- Console.WriteLine("Szerver elindítva!");
- counter = 0;
- while (true)
- {
- counter += 1;
- clientSocket = serverSocket.AcceptTcpClient();
- Console.WriteLine("Kliens(" + Convert.ToString(counter) + ") elindítva!");
- handleClinet client = new handleClinet();
- client.startClient(clientSocket, counter.ToString());
- }
- clientSocket.Close();
- serverSocket.Stop();
- Console.WriteLine(" >> " + "exit");
- Console.ReadLine();
- }
- }
- //Class to handle each client request separatly
- public class handleClinet
- {
- List<TcpClient> clientSocket = new List<TcpClient>();
- TcpClient jelenlegiSocket = new TcpClient();
- string clNo;
- public void startClient(TcpClient inClientSocket, string clineNo)
- {
- this.clientSocket.Add(inClientSocket);
- jelenlegiSocket = inClientSocket;
- this.clNo = clineNo;
- Thread ctThread = new Thread(doChat);
- ctThread.Start();
- }
- private void doChat()
- {
- byte[] bytesFrom = new byte[10025];
- string bejovo_uzenet = null;
- Byte[] sendBytes = null;
- string serverResponse = null;
- while ((true))
- {
- try
- {
- NetworkStream networkStream = jelenlegiSocket.GetStream();
- networkStream.Read(bytesFrom, 0, (int)jelenlegiSocket.ReceiveBufferSize);
- bejovo_uzenet = System.Text.Encoding.ASCII.GetString(bytesFrom);
- bejovo_uzenet = bejovo_uzenet.Substring(0, bejovo_uzenet.IndexOf("$"));
- Console.WriteLine(bejovo_uzenet);
- foreach (TcpClient tcp in clientSocket)
- {
- serverResponse = bejovo_uzenet;
- sendBytes = Encoding.ASCII.GetBytes(serverResponse);
- tcp.GetStream().Write(sendBytes, 0, sendBytes.Length);
- tcp.GetStream().Flush();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(" >> " + ex.ToString());
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment