public static void ReceiveMessage() { var afunc = new AsyncFunctions(); while (!exit) { try{ afunc.Receive(sender); afunc.receiveDone.WaitOne(); if (afunc.message == null) { continue; } var testmsg = afunc.message; if (testmsg.Text == "Завершение работы сервера") { exit = true; } Console.Out.WriteLineAsync(testmsg.Text); afunc.message = null; } catch (Exception ex) { Console.Out.WriteLineAsync("exeption test"); Console.Out.WriteLineAsync(ex.ToString()); } } }
private static void ServerStart() { IPHostEntry ipHost = Dns.GetHostEntry("localhost"); IPAddress ipAddr = ipHost.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000); Socket sListener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); AsyncFunctions afunc = new AsyncFunctions(); try{ sListener.Bind(ipEndPoint); sListener.Listen(10); Console.Out.WriteLineAsync($"Ожидаем соединение через порт {ipEndPoint}"); while (_working) { afunc.Accept(sListener); afunc.aceptDone.WaitOne(); } } catch (Exception ex) { Console.Out.WriteLineAsync(ex.ToString()); } finally { Console.ReadLine(); } }
private static void Broadcast(string text) { var afunc = new AsyncFunctions(); foreach (var list in _conectionsList) { foreach (var client in list) { var protomsg = new Message() { Data = "", Sender = "Server", Text = text }; afunc.sendDone.Set(); afunc.Send(client, protomsg); } } }
public static void Send() { try { exit = false; byte[] bytes = new byte[1024]; var protomsg = new Message(); IPHostEntry ipHost = Dns.GetHostEntry("localhost"); IPAddress ipAddr = ipHost.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000); sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); var afunc = new AsyncFunctions(); afunc.Connect(ipEndPoint, sender); Thread receiveThread = new Thread(ReceiveMessage); receiveThread.Start(); protomsg = new Message() { Data = "Join", Sender = "Human", Text = "" }; afunc.Send(sender, protomsg); while (!exit) { var text = Console.ReadLine(); ClearLine(); protomsg = MessageCreator(text); afunc.sendDone.WaitOne(); afunc.Send(sender, protomsg); } sender.Shutdown(SocketShutdown.Both); sender.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally{ Console.ReadLine(); } }
private static void ClientHandlerOld(Object socket) { var afunc = new AsyncFunctions(); var handler = (Socket)socket; _clients.Add(handler); Console.WriteLine(); while (!_exit) { afunc.Receive(handler); afunc.receiveDone.WaitOne(); var testmsg = afunc.message; if (!MessageHandler(testmsg)) { break; } } _clients.Remove(handler); handler.Shutdown(SocketShutdown.Both); handler.Close(); }