//Listen incoming connections. private void listenThread() { Socket serverSock = null; try { int recvNum; byte[] data = new byte[10240]; IPEndPoint localIp = null; foreach (IPAddress ipa in IpEntry.AddressList) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { localIp = new IPEndPoint(ipa, bindPort); break; } } IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), serverPort); if (MainWindow.activicePort.Contains(bindPort)) { MessageBox.Show("Port has been used."); throw new Exception(); } serverSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); serverSock.Bind(localIp); Success = true; MainWindow.activicePort.Add(bindPort); sockets.Add(serverSock); EndPoint server = serverEndPoint; EndPoint remote = new IPEndPoint(IPAddress.Any, 0); int port = 23000; while (true) { ArrayList denies = dt.read(); if (port > 23500) { port = 23000; } recvNum = serverSock.ReceiveFrom(data, ref remote); Lt.write("********************************"); Lt.write("Time: " + DateTime.Now.ToString()); Lt.write("Connect IP: " + (remote as IPEndPoint).Address.ToString()); Lt.write("Connect port: " + (remote as IPEndPoint).Port.ToString()); Lt.write("Destination Port: " + bindPort); Lt.write("Protocol: UDP"); if (denies.Contains((remote as IPEndPoint).Address.ToString())) { Lt.write("Operation: Reject"); Lt.write("********************************"); Console.WriteLine("Message received from " + remote.ToString() + "\t reject."); continue; } Lt.write("Operation: Accept"); Lt.write("********************************"); if (!remote.ToString().Equals(server.ToString())) { while (proxyTable.ContainsKey(port)) { port++; } IPEndPoint IPRemote = remote as IPEndPoint; string ip = IPRemote.Address.ToString(); int tarPort = IPRemote.Port; proxyTable.Add(port, IPRemote); Thread proxy = new Thread(proxyThread); object[] param = new object[5] { port, data, server, tarPort, recvNum }; port++; proxy.Start(param); } } } catch (Exception ex) { } finally { if (serverSock != null) { serverSock.Close(); } Success = false; MainWindow.activicePort.Remove(bindPort); stop(); } }