Created
August 30, 2011 21:05
-
-
Save jpf91/1182041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.socket, std.exception, std.stdio, std.random, std.range; | |
void main(string args[]) | |
{ | |
enforce(args.length == 2); | |
if(args[1] == "--server") | |
{ | |
auto sock = new Socket(AddressFamily.INET, SocketType.STREAM); | |
sock.bind(new InternetAddress("localhost", 9870)); | |
sock.listen(0); | |
auto sock2 = sock.accept(); | |
writeln("Server running; client connected; Press Enter"); | |
readln(); | |
writeln("OK, reading data"); | |
auto data = sock2.receive(new ubyte[2048]); | |
writefln("Read %s bytes", data); | |
writeln("Bye"); | |
} | |
else | |
{ | |
auto sock = new Socket(AddressFamily.INET, SocketType.STREAM); | |
sock.connect(new InternetAddress("localhost", 9870)); | |
auto numbers = array(take(rndGen(), 256)); | |
ubyte[] data = cast(ubyte[])numbers; | |
writeln("Connected; Press Enter"); | |
readln(); | |
writefln("Sending %s bytes", data.length); | |
sock.send(data); | |
writeln("Sending done, Press Enter to exit"); | |
readln(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment