Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ChatServer;
- using System;
- using System.Threading;
- namespace ChatTCPServer
- {
- class Program
- {
- static ServerObject server; // сервер
- static Thread listenThread; // потока для прослушивания
- static void Main(string[] args)
- {
- try
- {
- server = new ServerObject();
- listenThread = new Thread(new ThreadStart(server.listen));
- listenThread.Start(); //старт потока
- }
- catch (Exception ex)
- {
- server.disconnect();
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
- public class ServerObject
- {
- static TcpListener tcpListener; // сервер для прослушивания
- List<ClientObject> clients = new List<ClientObject>(); // все подключения
- int port = 12000;
- protected internal void addConnection(ClientObject clientObject)
- {
- clients.Add(clientObject);
- }
- protected internal void removeConnection(string id)
- {
- // получаем по id закрытое подключение
- ClientObject client = clients.FirstOrDefault(c => c.Id == id);
- // и удаляем его из списка подключений
- if (client != null)
- clients.Remove(client);
- }
- // прослушивание входящих подключений
- protected internal void listen()
- {
- try
- {
- tcpListener = new TcpListener(IPAddress.Any, port);
- tcpListener.Start();
- Console.WriteLine("Сервер запущен. Ожидание подключений...");
- while (true)
- {
- TcpClient tcpClient = tcpListener.AcceptTcpClient();
- ClientObject clientObject = new ClientObject(tcpClient, this);
- Thread clientThread = new Thread(new ThreadStart(clientObject.process));
- clientThread.Start();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- disconnect();
- }
- }
- // трансляция сообщения подключенным клиентам
- protected internal void broadcastMessage(string message, string id)
- {
- byte[] data = Encoding.Unicode.GetBytes(message);
- for (int i = 0; i < clients.Count; i++)
- {
- if (clients[i].Id != id) // если id клиента не равно id отправляющего
- {
- clients[i].Stream.Write(data, 0, data.Length); //передача данных
- }
- }
- }
- // отключение всех клиентов
- protected internal void disconnect()
- {
- tcpListener.Stop(); //остановка сервера
- for (int i = 0; i < clients.Count; i++)
- {
- clients[i].close(); //отключение клиента
- }
- Environment.Exit(0); //завершение процесса
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Net;
- using System.Text;
- using System.Threading;
- namespace ChatServer
- {
- public class ServerObject
- {
- static TcpListener tcpListener; // сервер для прослушивания
- List<ClientObject> clients = new List<ClientObject>(); // все подключения
- int port = 12000;
- protected internal void addConnection(ClientObject clientObject)
- {
- clients.Add(clientObject);
- }
- protected internal void removeConnection(string id)
- {
- // получаем по id закрытое подключение
- ClientObject client = clients.FirstOrDefault(c => c.Id == id);
- // и удаляем его из списка подключений
- if (client != null)
- clients.Remove(client);
- }
- // прослушивание входящих подключений
- protected internal void listen()
- {
- try
- {
- tcpListener = new TcpListener(IPAddress.Any, port);
- tcpListener.Start();
- Console.WriteLine("Сервер запущен. Ожидание подключений...");
- while (true)
- {
- TcpClient tcpClient = tcpListener.AcceptTcpClient();
- ClientObject clientObject = new ClientObject(tcpClient, this);
- Thread clientThread = new Thread(new ThreadStart(clientObject.process));
- clientThread.Start();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- disconnect();
- }
- }
- // трансляция сообщения подключенным клиентам
- protected internal void broadcastMessage(string message, string id)
- {
- byte[] data = Encoding.Unicode.GetBytes(message);
- for (int i = 0; i < clients.Count; i++)
- {
- if (clients[i].Id != id) // если id клиента не равно id отправляющего
- {
- clients[i].Stream.Write(data, 0, data.Length); //передача данных
- }
- }
- }
- // отключение всех клиентов
- protected internal void disconnect()
- {
- tcpListener.Stop(); //остановка сервера
- for (int i = 0; i < clients.Count; i++)
- {
- clients[i].close(); //отключение клиента
- }
- Environment.Exit(0); //завершение процесса
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment