Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Net.Sockets;
- using System.IO;
- namespace MultiServeris
- {
- class Multiserveris
- {
- static void Main(string[] args)
- {
- TcpListener ServerSocket = new TcpListener(1000); // listens on port number 1000
- ServerSocket.Start(); // start listener
- List<TcpClient> clients = new List<TcpClient>();
- Console.WriteLine("Serveris paleistas. Laukiama klientu...");
- while (true)
- {
- // creates clients list
- TcpClient clientSocket = ServerSocket.AcceptTcpClient(); // wait for client to make request
- clients.Add(clientSocket); // Adds client to list
- handleClient client = new handleClient(); // handle each client
- client.startClient(clientSocket);
- }
- }
- }
- public class handleClient
- {
- TcpClient clientSocket; // client socket
- List<TcpClient> clients;
- public void startClient(TcpClient inClientSocket)
- {
- this.clientSocket = inClientSocket;
- Thread ctThread = new Thread(Chat); // start thread for each client
- ctThread.Start();
- }
- private void Chat()
- {
- byte[] msg = new byte[1024];
- while (true)
- {
- NetworkStream ns = clientSocket.GetStream();
- ns.Read(msg, 0, 100);
- ns.Flush();
- Broadcast(msg);
- }
- }
- private void Broadcast(byte[] msg)
- {
- foreach (var client in clients)
- {
- NetworkStream ns = client.GetStream();
- ns.Write(msg, 0, 100);
- ns.Flush();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement