Created
September 24, 2020 11:24
-
-
Save aferust/7579b520814f82b3203c969924491459 to your computer and use it in GitHub Desktop.
dlang tcp server
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.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