public client() { bool conn = true; TcpClient client = new TcpClient(); Console.WriteLine("skriv porten. Example: 5001"); int port = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("skriv ip'en. \n example: 127.0.0.1"); IPAddress ip = IPAddress.Parse(Console.ReadLine()); IPEndPoint endPoint = new IPEndPoint(ip, port); client.Connect(endPoint); NetworkStream stream = client.GetStream(); Smethods smethod = new Smethods(); smethod.ReceiveMessages(stream); Console.WriteLine("Du kan skrive beskeder nu"); while (conn) { smethod.cmessage(stream); } client.Close(); }
void ClientServer() { string ipInput = "f**k"; bool bol = true; int port = 0; IPAddress ip = IPAddress.Any; IPEndPoint endpoint = new IPEndPoint(ip, 5001); TcpListener listener = new TcpListener(endpoint); listener.Start(); while (bol) { Smethods funktioner = new Smethods(); if (Console.ReadKey(true).Key == ConsoleKey.Enter) { if (ipInput == "f**k") { Console.WriteLine("skriv den ip du vil sende til\n example: 127.0.0.1"); ipInput = Console.ReadLine(); Console.WriteLine("Skriv hvilken port du vil sende det til\n example 5001"); port = Convert.ToInt32(Console.ReadLine()); } TcpClient clients = new TcpClient(); IPAddress ipp = IPAddress.Parse(ipInput); IPEndPoint remoteEndpoint = new IPEndPoint(ipp, port); clients.Connect(remoteEndpoint); NetworkStream streams = clients.GetStream(); string message = Console.ReadLine(); byte[] buffersixe = Encoding.UTF8.GetBytes(message); streams.Write(buffersixe, 0, message.Length); clients.Close(); } TcpClient client = listener.AcceptTcpClient(); NetworkStream stream = client.GetStream(); funktioner.ReceiveMessages(stream); } listener.Stop(); }
public server() { bool conn = true; int port = 5001; Smethods smethod = new Smethods(); smethod.serverInfo(port); IPAddress ip = IPAddress.Any; IPEndPoint localendpoint = new IPEndPoint(ip, port); TcpListener listener = new TcpListener(localendpoint); listener.Start(); Console.WriteLine("leder efter potientel klient"); smethod.acceptconn(listener); Console.WriteLine("Skriv din besked"); while (conn) { smethod.sMessage(); } }
/// <summary> /// denne funtktion tager imod en command som den får sendt igennem hvis en skriver det til den. Denne her compare den så med alle ekisterende commands /// </summary> /// <param name="command"></param> public async void dictionayList(TcpClient requestingUser, string command) { NetworkStream requester = requestingUser.GetStream(); Smethods calling = new Smethods(); if (command == "/?") { string coices = DLCH(); } else if (command == "/wisper") { string text = "hvem vil du gerne skrive til?"; calling.send(requester, text); string choiceOfUser = Console.ReadLine(); foreach (users brugernavn in calling.userlist) { if (choiceOfUser == brugernavn.navne) { text = "Hvad vil du gerne sende som besked?"; calling.send(requester, text); text = Console.ReadLine(); NetworkStream wisperbruger = brugernavn.brugere.GetStream(); calling.send(wisperbruger, text); } } } else if (command == "/list") { foreach (users brugernavn in calling.userlist) { byte[] liste = Encoding.UTF8.GetBytes(brugernavn.navne + "\n"); requestingUser.GetStream().Write(liste, 0, liste.Length); } } }
/// <summary> /// denne funtktion tager imod en command som den får sendt igennem hvis en skriver det til den. Denne her compare den så med alle ekisterende commands /// </summary> /// <param name="command"></param> public void dictionayList(TcpClient requestingUser, string command) { int guess = 0; NetworkStream requester = requestingUser.GetStream(); Smethods calling = new Smethods(); if (command == "/?") { string coices = DLCH(); calling.send(requester, coices); } else if (command == "/wisper") { string text = "hvem vil du gerne skrive til?"; calling.send(requester, text); string choiceOfUser = calling.recivingString(requester); foreach (users brugernavn in Smethods.userlist) { if (choiceOfUser == brugernavn.navne) { text = "Hvad vil du gerne sende som besked?"; calling.send(requester, text); text = calling.recivingString(requester); NetworkStream wisperbruger = brugernavn.brugere.GetStream(); calling.send(wisperbruger, text); } } } else if (command == "/list") { foreach (users brugernavn in Smethods.userlist) { calling.send(requester, brugernavn.navne + "\n"); } } else if (command == "/game") { int RandomNumber(int min, int max) { Random random = new Random(); return(random.Next(min, max)); } bool conn = true; while (conn) { if (guess == 0) { guess = RandomNumber(0, 100); } string Text = "gæt på et nummer \n"; calling.send(requester, Text); string theGuess = calling.recivingString(requester); Console.WriteLine(guess); if (int.TryParse(theGuess, out int value)) { if (Convert.ToInt32(theGuess) == guess) { Text = "du gættede rigtigt \n"; calling.send(requester, Text); conn = false; guess = 0; } else if (Convert.ToInt32(theGuess) > guess) { Text = "dit gæt var for højt \n"; calling.send(requester, Text); } else if (Convert.ToInt32(theGuess) < guess) { Text = "Dit gæt var for lavt \n"; calling.send(requester, Text); } } else if (theGuess == "stop") { conn = false; } else { Text = "skriv et tal din spade \n" + "eller skriv 'stop' for at lukke"; calling.send(requester, Text); } } } }