Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.d
- void main(string[] args) {
- try {
- import server;
- handleServer();
- }
- catch { // being lazy, so when it can't bind, connect as client ...
- import client;
- handleClient();
- }
- import std.stdio : readln;
- readln();
- }
- // server.d
- module server;
- import std.socket;
- import core.thread;
- void handleServer() {
- Socket ssoc = new TcpSocket();
- ssoc.bind(new InternetAddress("127.0.0.1", 8989));
- ssoc.listen(100);
- auto csoc = ssoc.accept();
- // if we sleep here it will be a success too
- // I assume that's because the time-out isn't expected till one or more bytes have been received
- // by the client.
- // It's safe to assume that time-outs are not related to disconnection as it will
- // most likely assume the connection to still be established ???
- ubyte[] buf = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- csoc.send(buf[0 .. $/2]); // Sending first portion of 5 bytes (client is expecting 10)
- Thread.sleep(dur!("seconds")(10)); // waits 10 seconds between sending the last portion
- csoc.send(buf[$/2 .. $]); // Sending last portion of 5 bytes (the client should have a success by this time, so this is ignored completely)
- }
- // client.d
- module client;
- import std.stdio : writefln;
- import std.socket;
- import std.datetime;
- import core.thread;
- void handleClient() {
- Socket csoc = new TcpSocket();
- csoc.setOption(SocketOptionLevel.SOCKET,SocketOption.RCVTIMEO, dur!("seconds")(5));
- StopWatch sw;
- csoc.connect(new InternetAddress("127.0.0.1", 8989));
- writefln("Connected...");
- sw.start(); // starts a stop watch to track time
- auto buf = new ubyte[10]; // The buffer for receiving 10 byte
- csoc.receive(buf); // Receives the buffer (however only the portion of 5 bytes should be received. No time-out either
- sw.stop(); // Stops the stop watch
- // Gathers information
- writefln("Socket Error Text: '%s'", csoc.getErrorText);
- writefln("Time: '%s'", sw.peek().msecs);
- writefln("Buffer: %s", buf); // Only the first portion of 5 bytes is displaying.
- // I assume time-out's are triggered by the socket core and not the program itself by the server sleeping etc.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement