// Qt's asynchronous DNS resolution mechanism will call this method // when the DNS resolution for this peer's hostname is complete. // "Completion" might mean either success or failure: // on failure, we'll get a QHostInfo object with no IP addresses. public void lookupDone(QHostInfo hi) { hostinfo = hi; addrs = hi.addresses(); Console.WriteLine("Peer " + name + ": got " + addrs.size() + " addresses"); //Console.WriteLine(((QHostAddress)addrs.get(0)).ToString()); IPaddr = ((QHostAddress)addrs.get(0)).ToString(); }
public Net(ushort initPort, IList <string> initPeers) { // For each peer name in the command-line arguments, // create a Peer object to represent and track that peer. foreach (string peerName in initPeers) { peers.Add(new Peer(this, peerName)); //peerList.add(peerName); } //Console.WriteLine(peers.Count); for (int i = 0; i < peers.Count; i++) { peerList.add(peers[i].betterName); } // Bind our UDP socket to the specified (or default) port // allowing us to send and receive network datagrams. if (initPort == 0) { initPort = NetConf.defaultPort; } // Like println in Java Console.WriteLine("Binding to port " + initPort); sock.bind(new QHostAddress(QHostAddress.SpecialAddress.Any), initPort); sock.readyRead.connect(this, "receive()"); // Save some identifying information for printfs and such. myPort = initPort; myName = QHostInfo.localHostName() + ":" + myPort; // Create a timer to trigger repeating "pings" // we send out to to all our peers. QTimer timer = new QTimer(this); timer.timeout.connect(this, "pingAllPeers()"); timer.start(1000); // ping once every second }
public Peer(Net peerNet, String peerName) { net = peerNet; name = peerName; // Does the peerName include a colon and a port number? // If so, parse it and strip it off. int colonpos = peerName.IndexOf(':'); if (colonpos >= 0) // Explicit port number. { hostName = peerName.Substring(0, colonpos); port = ushort.Parse(peerName.Substring(colonpos + 1)); } else // Use default port. { hostName = peerName; port = NetConf.defaultPort; } // If the peer name is empty (aside from port number), // interpret it as a synonym for "localhost". if (hostName == "") { hostName = "localhost"; } // Start DNS-resolving this peer's hostname, // calling our lookupDone() method when finished. Console.WriteLine("Resolving hostname " + hostName); QHostInfo.lookupHost(hostName, this, "lookupDone"); betterName = hostName + ":" + port; }