Hello,
I decided to exercise my skills in D a bit and wrote an implementation
for an echo server using D. I figure before I post it to the site, I'd
post it here for comments as I'd like to get the best possible version
on the site vs. something that's half-assed. Considering I'm very new
to socket programming, I'm guessing this may not be the best possible
version.
Attached are two files: a client and a server. Let me know what you think.
Casey
import std.socket;
import std.stdio;
import std.string;
void main()
{
Socket sock = new TcpSocket(new InternetAddress("localhost", 12321));
string[4] messages = ["Hello world.", "Hello\nworld.", "\tHello world.",
"Goodby cruel world..."];
ubyte buff[1];
int bytesRead;
foreach (msg; messages)
{
sock.send(msg);
for (int i = 0; i < msg.length; i++)
{
bytesRead = sock.receive(buff);
write(cast(char)buff[0]);
}
/*
string temp = cast(string)buff;
writeln(temp, "\n");
*/
write("\n");
}
sock.close();
}
import std.socket;
import std.array;
void main()
{
Socket listener = new TcpSocket;
assert(listener.isAlive);
listener.bind(new InternetAddress(12321));
listener.listen(10);
Socket currSock;
uint bytesRead;
ubyte buff[1];
while(1)
{
currSock = listener.accept();
while ((bytesRead = currSock.receive(buff)) > 0)
{
currSock.send(buff);
}
currSock.close();
buff.clear();
}
}