public Server() { consoleColor consoleColor = new consoleColor(); Console.Clear(); IPAddress ip = IPAddress.Any; int port = portCheckServer(); TcpListener listener = new TcpListener(ip, port); listener.Start(); Console.WriteLine("Opretter server vent venligst..."); Console.Clear(); consoleColor.green(); Console.WriteLine("Server oprettet på port: " + port); consoleColor.gray(); Console.WriteLine("Skriv den første besked....."); Console.WriteLine("<--------------->-|-|-<--------------->"); AcceptClients(listener); bool isRunning = true; while (isRunning) { Console.Write("You: "); string text = Console.ReadLine(); byte[] buffer = Encoding.UTF8.GetBytes(text); foreach (TcpClient client in clients) { try { client.GetStream().Write(buffer, 0, buffer.Length); } catch (System.IO.IOException) { break; } catch (System.InvalidOperationException) { break; } } } }
public ClientProgram() { consoleColor consoleColor = new consoleColor(); TcpClient client = new TcpClient(); bool isRunning = true; //Console.Clear(); int port = portCheckClient(); Console.Clear(); IPAddress ip = ipCheckClient(); IPEndPoint endPoint = new IPEndPoint(ip, port); Console.Clear(); Console.WriteLine("Forbinder til server..."); try { client.Connect(endPoint); } catch (SocketException) { Console.Clear(); consoleColor.red(); Console.WriteLine("Kunne ikke forbinde til serveren tjek om du har brugt den rigtige port og ip."); consoleColor.gray(); Console.Write("Tryk ennter for at fortsætte..."); Console.ReadLine(); Console.Clear(); return; } Console.Clear(); consoleColor.green(); Console.WriteLine("Forbinelse oprettet til server."); Console.WriteLine("Port: " + port); Console.WriteLine("Ip: " + ip); consoleColor.gray(); Console.WriteLine("Skriv din første besked....."); Console.WriteLine("<--------------->-|-|-<--------------->"); while (isRunning) { NetworkStream stream = client.GetStream(); ReceiveMessage(stream); Console.Write("You: "); string text = Console.ReadLine(); byte[] buffer = Encoding.UTF8.GetBytes(text); stream.Write(buffer, 0, buffer.Length); if (text == "/ouit") { isRunning = false; client.Close(); Console.WriteLine("<--------------->-|-|-<--------------->"); Console.WriteLine("Du har forladt samtalen..."); Console.WriteLine("Tryk enter for at gå videre..."); Console.ReadLine(); Console.Clear(); return; } } client.Close(); }