How can I set Timeout of Socket?

FreeSlave freeslave93 at gmail.com
Sun Nov 15 04:24:58 UTC 2020


On Sunday, 15 November 2020 at 00:05:08 UTC, Marcone wrote:
> Socket s = new Socket(AddressFamily.INET, SocketType.STREAM);
> s.connect(new InternetAddress("domain.com", 80));
>
> I want that program raise an error if reach for example 30 
> seconds of timeout.

Perhaps using Socket.select and SocketSet?

import std.socket;
import std.stdio;
import core.time;

void main()
{
     Socket s = new Socket(AddressFamily.INET, SocketType.STREAM);
     s.blocking = false;
     auto set = new SocketSet(1);
     set.add(s);
     s.connect(new InternetAddress("dlang.org", 80));
     scope(exit) s.close();
     Socket.select(null, set, null, dur!"seconds"(10));
     if (set.isSet(s))
     {
         writeln("socket is ready");
     }
     else
     {
         writeln("could not connect");
     }
}


More information about the Digitalmars-d-learn mailing list