Skip to content

Instantly share code, notes, and snippets.

@aferust
Created September 24, 2020 11:24
Show Gist options
  • Save aferust/7579b520814f82b3203c969924491459 to your computer and use it in GitHub Desktop.
Save aferust/7579b520814f82b3203c969924491459 to your computer and use it in GitHub Desktop.
dlang tcp server
import std.stdio;
import std.socket;
class TCPServer{
string host;
ushort port;
@disable this();
this(string host, ushort port){
this.host = host;
this.port = port;
}
void start(){
auto tcps = new TcpSocket(AddressFamily.INET);
auto addr = new InternetAddress(host, port);
tcps.bind(addr);
tcps.listen(10);
writeln("Listening at ", addr);
while(true){
auto s = tcps.accept();
writeln(s.localAddress);
char[1024] data;
auto nbytes = s.receive(data);
writeln(nbytes, "-", data[0..nbytes]);
s.send(data);
s.close();
}
}
}
int main(){
auto ts = new TCPServer("127.0.0.1", 8888);
ts.start();
return 0;
}
/+ client
module client;
import std.socket, std.conv;
int main(){
auto tcps = new TcpSocket(AddressFamily.INET);
auto addr = new InternetAddress("127.0.0.1", 8888);
tcps.connect(addr);
tcps.sendTo("Selam şğüçö");
tcps.close();
return 0;
}
+/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment