Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Networking_server
- {
- class Program
- {
- public static string password = "";
- public static string userName = "";
- public static string ipAddress = "";
- public static string serverAddress = "";
- static void Main(string[] args)
- {
- CreateUser();
- Server myServer = new Server();
- Thread serverThread = new Thread(myServer.Run);
- serverThread.Start();
- serverThread.Join();
- }
- private static void CreateUser()
- {
- Console.Write("Server IP: ");
- serverAddress = Console.ReadLine();
- Console.Write("Username: ");
- userName = Console.ReadLine();
- Console.Write("Password: ");
- password = Console.ReadLine();
- IPHostEntry host;
- host = Dns.GetHostEntry(Dns.GetHostName());
- foreach (IPAddress ip in host.AddressList)
- {
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- {
- ipAddress = ip.ToString();
- }
- }
- User user = new User { "rawr", "rawr", "rawr" };
- }
- }
- public class Server
- {
- List<ClientHandler> clients = new List<ClientHandler>();
- List<ClientHandler> clients2 = new List<ClientHandler>();
- public void Run()
- {
- TcpListener listener = new TcpListener(IPAddress.Any, 5000);
- Console.WriteLine("Server up and running, waiting for messages...");
- try
- {
- listener.Start();
- while (true)
- {
- try
- {
- TcpClient c = listener.AcceptTcpClient();
- ClientHandler newClient = new ClientHandler(c, this);
- foreach (var client in clients)
- {
- if (client != newClient)
- {
- clients2.Add(newClient);
- }
- }
- clients = clients2;
- clients2.Clear();
- Thread clientThread = new Thread(newClient.Run);
- clientThread.Start();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message + "sörvör run exception");
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message + "sörvör run exception");
- }
- finally
- {
- if (listener != null)
- listener.Stop();
- }
- }
- public void Broadcast(ClientHandler client, string message)
- {
- foreach (var item in clients)
- {
- if (item.tcpclient.Connected)
- {
- clients2.Add(item);
- }
- }
- foreach (ClientHandler tmpClient in clients2)
- {
- if (tmpClient != client)
- {
- NetworkStream n = tmpClient.tcpclient.GetStream();
- BinaryWriter w = new BinaryWriter(n);
- w.Write(message);
- w.Flush();
- }
- else if (clients.Count() == 1)
- {
- NetworkStream n = tmpClient.tcpclient.GetStream();
- BinaryWriter w = new BinaryWriter(n);
- w.Write("Sorry, you are alone...");
- w.Flush();
- }
- }
- }
- public void DisconnectClient(ClientHandler client)
- {
- clients.Remove(client);
- Console.WriteLine("Client X has left the building...");
- Broadcast(client, "Client X has left the building...");
- }
- }
- public class ClientHandler
- {
- public TcpClient tcpclient;
- private Server myServer;
- private string UserName;
- public ClientHandler(TcpClient c, Server server)
- {
- tcpclient = c;
- this.myServer = server;
- }
- public void Run()
- {
- try
- {
- string message = "";
- while (!message.Equals("quit"))
- {
- NetworkStream n = tcpclient.GetStream();
- message = new BinaryReader(n).ReadString();
- myServer.Broadcast(this, message);
- Console.WriteLine(message);
- }
- myServer.DisconnectClient(this);
- tcpclient.Close();
- }
- catch (Exception ex)
- {
- myServer.DisconnectClient(this);
- tcpclient.Close();
- Console.WriteLine(ex.Message + "röv");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement