Checking if a port is listening

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 19 11:24:38 PDT 2016


On Saturday, 19 March 2016 at 09:55:13 UTC, Lucien wrote:
>     const int MAX = 64;
>     Socket[] sockets = new Socket[MAX];
>     string ipb = "192.168.0.";
>
>     for (int i = 1; i < MAX; i++) {

Here's the reason for your SEGV: You need to start at 0, because 
otherwise `sockets[0]` is `null`. When you add that to the 
SocketSet, it will trigger the segfault. I guess you want to skip 
the 0 because it represents the subnet address; in that case, you 
simply mustn't add `sockets[0]` to the set.

But then there is another problems: You're using `select()` the 
wrong way. The point of using select() is that you can check 
things asynchronously. Your code should be structured like this 
(pseudo code):

auto ss = new SocketSet();
for(i; 1 .. MAX) {
     auto s = new Socket(...);
     s.blocking = false;
     s.connect(...);
     ss.add(s);
}

while(ss.count > 0) {
     auto write_ss = ss.dup;
     auto status = Socket.select(null /* read */, write_ss /* 
write */, null /* error */, 500.msecs);
     // for a connect()ing socket, writeability means connected
     if(status < 0)
         writeln("interrupted, retrying");
     else if(status == 0)
         writeln("timeout, retrying");
     else {
         writeln(status, " socket(s) changed state");
         for(fd; 0 .. write_ss.maxfd+1) {
             // check whether this socket has changed
             if(!write_ss.isSet(fd)) continue;
             // if yes, remove it from the original SocketSet
             ss.remove(fd);
             writeln("successfully connected to 192.168.0.", fd+1);
         }
     }
}


More information about the Digitalmars-d-learn mailing list