private void InitializeReceiverConnection() { while (true) { IpPortAddress dlg = new IpPortAddress("Слушатель (Any)"); if (receiverSocket != null) { receiverSocket.Close(); } receiverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); if ((bool)dlg.ShowDialog()) { try { port = Int32.Parse(dlg.Port); switch (dlg.IP) { case "default": EndPoint = new IPEndPoint(IPAddress.Loopback, port); break; case "any": EndPoint = new IPEndPoint(IPAddress.Any, port); break; default: EndPoint = new IPEndPoint(IPAddress.Parse(dlg.IP), port); break; } receiverSocket.Bind(localEndPoint); Task.Run(() => { while (receiverSocket.IsBound) { byte[] buff = new byte[1024]; EndPoint var = new IPEndPoint(IPAddress.Any, 10001); int len = receiverSocket.ReceiveFrom(buff, ref var); GetMessage(Encoding.Unicode.GetString(buff, 0, len), var.ToString()); } }); break; } catch (Exception ex) { MessageBox.Show(ex.Message); }; } else { EndPoint = new IPEndPoint(IPAddress.Any, 10001); break; } } }
private void InitializeSenderConnection() { while (true) { IpPortAddress dlg = new IpPortAddress("Удаленный сервер", "10.2.124.255"); if (senderSocket != null) { senderSocket.Close(); } senderSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); senderSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); if ((bool)dlg.ShowDialog()) { try { port = Int32.Parse(dlg.Port); switch (dlg.IP) { case "default": BroadcastEndPoint = new IPEndPoint(IPAddress.Loopback, port); break; case "any": BroadcastEndPoint = new IPEndPoint(IPAddress.Any, port); break; default: BroadcastEndPoint = new IPEndPoint(IPAddress.Parse(dlg.IP), port); break; } break; } catch (Exception ex) { MessageBox.Show(ex.Message); }; } else { BroadcastEndPoint = new IPEndPoint(IPAddress.Any, 10001); break; } } }