2009-01-29 00:18:34 +08:00
|
|
|
module socketserver;
|
|
|
|
|
|
|
|
import dsfml.system.all;
|
|
|
|
import dsfml.network.all;
|
|
|
|
|
|
|
|
version (Tango)
|
|
|
|
{
|
2010-03-04 10:23:27 +08:00
|
|
|
import tango.io.Console;
|
|
|
|
import tango.io.Stdout;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-04 10:23:27 +08:00
|
|
|
import std.stdio;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2010-03-04 10:23:27 +08:00
|
|
|
//We create a TCP socket for listening incomming client
|
|
|
|
SocketTCP listener = new SocketTCP();
|
|
|
|
|
|
|
|
//Set a random port for the listener
|
|
|
|
if (!listener.listen(9000))
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
//Creation of TCP socket
|
|
|
|
SocketTCP client = new SocketTCP();
|
|
|
|
IPAddress ipClient;
|
|
|
|
|
|
|
|
display("Waiting for client."w);
|
|
|
|
|
|
|
|
if (listener.accept(client, ipClient) == SocketStatus.DONE) //This call blocks until client connection
|
|
|
|
{
|
|
|
|
display("New client connected."w);
|
|
|
|
//The packet for retrieving the client message
|
|
|
|
Packet p = new Packet();
|
|
|
|
display("Waiting for data"w);
|
|
|
|
if (client.receive(p) != SocketStatus.DONE) //Assert on reception error
|
|
|
|
assert(0);
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
//Display the string send by the client
|
|
|
|
wchar[] c;
|
|
|
|
p.get(c);
|
|
|
|
display("Packet received : "w ~ c);
|
|
|
|
|
|
|
|
//Clear the packet (We could use a new one)
|
|
|
|
p.clear();
|
|
|
|
|
|
|
|
//and send response to client
|
|
|
|
client.send(p.set("Hello from the server !"w));
|
|
|
|
}
|
|
|
|
read();
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-04 10:23:27 +08:00
|
|
|
* Multilib string display
|
2009-01-29 00:18:34 +08:00
|
|
|
*/
|
|
|
|
void display(wchar[] c)
|
|
|
|
{
|
2010-03-04 10:23:27 +08:00
|
|
|
version (Tango)
|
|
|
|
{
|
|
|
|
Stdout(c).newline;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writefln("%s", c);
|
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-04 10:23:27 +08:00
|
|
|
* Dummy function to prevent console closing on windows
|
2009-01-29 00:18:34 +08:00
|
|
|
*/
|
|
|
|
void read()
|
|
|
|
{
|
2010-03-04 10:23:27 +08:00
|
|
|
version (Tango)
|
|
|
|
{
|
|
|
|
Cin.get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
readln();
|
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|