Hello world/Web server: Difference between revisions
Content added Content deleted
m (→{{header|Sidef}}: changed the keyword "require" to function call) |
m (→{{header|Sidef}}: Replaced "Socket::*" with "Socket.*") |
||
Line 1,336: | Line 1,336: | ||
<lang ruby>var port = 8080; |
<lang ruby>var port = 8080; |
||
var protocol = Socket.getprotobyname("tcp"); |
var protocol = Socket.getprotobyname("tcp"); |
||
var sock = (Socket.open(Socket |
var sock = (Socket.open(Socket.PF_INET, Socket.SOCK_STREAM, protocol) || die "couldn't open a socket: #{$!}"); |
||
# PF_INET to indicate that this socket will connect to the internet domain |
# PF_INET to indicate that this socket will connect to the internet domain |
||
# SOCK_STREAM indicates a TCP stream, SOCK_DGRAM would indicate UDP communication |
# SOCK_STREAM indicates a TCP stream, SOCK_DGRAM would indicate UDP communication |
||
sock.setsockopt(Socket |
sock.setsockopt(Socket.SOL_SOCKET, Socket.SO_REUSEADDR, 1) || die "couldn't set socket options: #{$!}"; |
||
# SOL_SOCKET to indicate that we are setting an option on the socket instead of the protocol |
# SOL_SOCKET to indicate that we are setting an option on the socket instead of the protocol |
||
# mark the socket reusable |
# mark the socket reusable |
||
sock.bind(Socket.sockaddr_in(port, Socket |
sock.bind(Socket.sockaddr_in(port, Socket.INADDR_ANY)) || die "couldn't bind socket to port #{port}: #{$!}"; |
||
# bind our socket to $port, allowing any IP to connect |
# bind our socket to $port, allowing any IP to connect |
||
sock.listen(Socket |
sock.listen(Socket.SOMAXCONN) || die "couldn't listen to port #{port}: #{$!}"; |
||
# start listening for incoming connections |
# start listening for incoming connections |
||
while (var client = sock.accept) { |
while (var client = sock.accept) { |
||
client.print ("HTTP/1.1 200 OK\r\n" + |
client.print ("HTTP/1.1 200 OK\r\n" + |