Last active
December 15, 2015 22:00
-
-
Save halida/5330197 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
import std.string; | |
import std.conv; | |
import std.random; | |
import std.outbuffer; | |
import core.thread; | |
int main() { | |
auto s = new TcpSocket(); | |
// s.blocking(true); | |
auto addr = new InternetAddress("localhost", 8111); | |
s.connect(addr); | |
// char [] data; | |
// long r = s.send("hello"); | |
// writefln("send byte: %d", r); | |
long r = s.send(cast(void[])"hello socket world"); | |
writefln("%d bytes sent.", r) ; | |
Thread.sleep( dur!("seconds")( 1 ) ); | |
char [] data; | |
r = s.receive(data); | |
writefln("receive byte: %d", r); | |
// writeln(data); | |
s.close(); | |
return 0; | |
} |
This file contains hidden or 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
require 'socket' | |
gs = TCPServer.open(8111) | |
socks = [gs] | |
addr = gs.addr | |
addr.shift | |
puts "server is on #{addr.join(':')}" | |
while true | |
nsock = select(socks) | |
next if nsock == nil | |
for s in nsock[0] | |
if s == gs | |
socks.push(s.accept) | |
puts "#{s} is accepted" | |
else | |
begin | |
if s.eof? | |
puts "#{s} is gone" | |
s.close | |
socks.delete(s) | |
else | |
str = s.gets | |
puts "received: #{str}" | |
s.write(str) | |
if str.strip == 'EOF' | |
s.close | |
socks.delete(s) | |
end | |
end | |
rescue Exception => e | |
puts "#{s} error: #{e.message}" | |
s.close | |
socks.delete(s) | |
end | |
end | |
end | |
end | |
This file contains hidden or 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
$ ruby echo_server.rb | |
server is on 8111:0.0.0.0:0.0.0.0 | |
#<TCPServer:0x007ff2c989e310> is accepted | |
received: hello socket world | |
#<TCPSocket:0x007ff2c989d528> error: Connection reset by peer | |
$ rdmd client.d 127.0.0.1 8111 | |
18 bytes sent. | |
receive byte: 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment