private void listen() { try { IPEndPoint localIP = new IPEndPoint(IPAddress.Any, PORT); udpServer = new UdpClient(localIP); } catch (Exception) { udpServer = null; thread = null; infoWindow.OnInfoUpdated(providerName, "PORT BOUND FAILED"); return; } while (continue_listening) { try { IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0); byte[] buffer = udpServer.Receive(ref ip); string str = Encoding.UTF8.GetString(buffer); string ipaddress = "" + ip.Address.GetAddressBytes()[0]; ipaddress += "." + ip.Address.GetAddressBytes()[1]; ipaddress += "." + ip.Address.GetAddressBytes()[2]; ipaddress += "." + ip.Address.GetAddressBytes()[3]; if (continue_listening) { String text; if (!ipaddress.Equals("127.0.0.1")) { text = "IP: " + ipaddress + "\nMessage: " + str; } else { text = str; } if (infoWindow != null) { infoWindow.OnInfoUpdated(providerName, text); } } } catch (Exception) { //Console.WriteLine("closed"); } } //udpServer.Close(); }
void InfoProvider.bindInfoWindow(InfoWindow infoWindow) { this.infoWindow = infoWindow; thread = new Thread(listen); thread.Start(); infoWindow.OnInfoUpdated(providerName, "Start collecting"); }