Hello world/Web server: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 139: | Line 139: | ||
int main() |
int main() |
||
{ |
{ |
||
int one = 1, client_fd; |
|||
struct sockaddr_in svr_addr, cli_addr; |
|||
socklen_t sin_len = sizeof(cli_addr); |
|||
int sock = socket(AF_INET, SOCK_STREAM, 0); |
|||
if (sock < 0) |
|||
err(1, "can't open socket"); |
|||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int)); |
|||
int port = 8080; |
|||
svr_addr.sin_family = AF_INET; |
|||
svr_addr.sin_addr.s_addr = INADDR_ANY; |
|||
svr_addr.sin_port = htons(port); |
|||
if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) { |
|||
close(sock); |
|||
err(1, "Can't bind"); |
|||
} |
|||
listen(sock, 5); |
|||
while (1) { |
|||
client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len); |
|||
printf("got connection\n"); |
|||
if (client_fd == -1) { |
|||
perror("Can't accept"); |
|||
continue; |
|||
} |
|||
} |
|||
write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/ |
|||
close(client_fd); |
|||
} |
|||
}</lang> |
}</lang> |
||
Line 242: | Line 242: | ||
(let ((connections (list (socket-listen "127.0.0.1" port :reuse-address t)))) |
(let ((connections (list (socket-listen "127.0.0.1" port :reuse-address t)))) |
||
(unwind-protect |
(unwind-protect |
||
(loop (loop for ready in (wait-for-input connections :ready-only t) |
|||
do (if (typep ready 'stream-server-usocket) |
|||
(push (socket-accept ready) connections) |
|||
(let* ((stream (socket-stream ready))) |
|||
(read-all stream) |
|||
(format log-stream "Got message...~%") |
|||
(mapc (lambda (line) (ln line stream)) |
|||
(list "HTTP/1.1 200 OK" |
|||
"Content-Type: text/plain; charset=UTF-8" |
|||
"" |
|||
"Hello world!")) |
|||
(socket-close ready) |
|||
(setf connections (remove ready connections)))))) |
|||
(loop for c in connections do (loop while (socket-close c)))))) |
(loop for c in connections do (loop while (socket-close c)))))) |
||
Line 268: | Line 268: | ||
void main() { |
void main() { |
||
Socket listener = new TcpSocket; |
|||
listener.bind(new InternetAddress(port)); |
|||
listener.listen(10); |
|||
Socket currSock; |
|||
while(cast(bool)(currSock = listener.accept())) { |
|||
currSock.sendTo(replace(q"EOF |
|||
HTTP/1.1 200 OK |
HTTP/1.1 200 OK |
||
Content-Type: text/html; charset=UTF-8 |
Content-Type: text/html; charset=UTF-8 |
||
<html> |
<html> |
||
<head><title>Hello, world!</title></head> |
|||
<body>Hello, world!</body> |
|||
</html> |
</html> |
||
EOF", "\n", "\r\n")); |
EOF", "\n", "\r\n")); |
||
currSock.close(); |
|||
} |
|||
} |
} |
||
</lang> |
</lang> |
||
Line 390: | Line 390: | ||
do( _Data ) -> |
do( _Data ) -> |
||
{proceed, [{response,{200,"Goodbye, World!"}}]}. |
|||
httpd_start( Port, Module ) -> |
httpd_start( Port, Module ) -> |
||
Arguments = [{bind_address, "localhost"}, {port, Port}, {ipfamily, inet}, |
|||
{modules, [Module]}, |
|||
{server_name,erlang:atom_to_list(Module)}, {server_root,"."}, {document_root,"."}], |
|||
{ok, Pid} = inets:start( httpd, Arguments, stand_alone ), |
|||
Pid. |
|||
httpd_stop( Pid ) -> |
httpd_stop( Pid ) -> |
||
inets:stop( stand_alone, Pid ). |
|||
task() -> |
task() -> |
||
Pid = httpd_start( 8080, ?MODULE ), |
|||
timer:sleep( 30000 ), |
|||
httpd_stop( Pid ). |
|||
</lang> |
</lang> |
||
Line 439: | Line 439: | ||
import ( |
import ( |
||
"fmt" |
|||
"log" |
|||
"net/http" |
|||
) |
) |
||
func main() { |
func main() { |
||
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { |
|||
fmt.Fprintln(w, "Goodbye, World!") |
|||
}) |
|||
log.Fatal(http.ListenAndServe(":8080", nil)) |
|||
} |
} |
||
</lang> |
</lang> |
||
Line 572: | Line 572: | ||
public class HelloWorld{ |
public class HelloWorld{ |
||
public static void main(String[] args) throws IOException{ |
|||
ServerSocket listener = new ServerSocket(8080); |
|||
while(true){ |
|||
Socket sock = listener.accept(); |
|||
new PrintWriter(sock.getOutputStream(), true). |
|||
println("Goodbye, World!"); |
|||
sock.close(); |
|||
} |
|||
} |
|||
} |
|||
}</lang> |
}</lang> |
||
Line 606: | Line 606: | ||
with no errors. |
with no errors. |
||
=={{header|Lasso}}== |
|||
While Lasso has a built-in webserver you can use, here's how you can create a basic multi-threaded webserver of your own to complete this request: |
|||
<lang lasso>local(server) = net_tcp |
|||
handle => { #server->close } |
|||
#server->bind(8080) & listen & forEachAccept => { |
|||
local(con) = #1 |
|||
split_thread => { |
|||
handle => { #con->close } |
|||
local(request) = '' |
|||
// Read in the request in chunks until you have it all |
|||
{ |
|||
#request->append(#con->readSomeBytes(8096)) |
|||
not #request->contains('\r\n\r\n')? currentCapture->restart |
|||
}() |
|||
local(response) = 'HTTP/1.1 200 OK\r\n\ |
|||
Content-Type: text/html; charset=UTF-8\r\n\r\n\ |
|||
Goodbye, World!' |
|||
#con->writeBytes(bytes(#response)) |
|||
} |
|||
}</lang> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
Line 798: | Line 821: | ||
Using Perl's glue power, provide a suicide note with visitor counter via netcat: |
Using Perl's glue power, provide a suicide note with visitor counter via netcat: |
||
<lang Perl>while (++(our $vn)) { |
<lang Perl>while (++(our $vn)) { |
||
open NC, "|-", qw(nc -l -p 8080 -q 1); |
|||
print NC "HTTP/1.0 200 OK\xd\xa", |
|||
"Content-type: text/plain; charset=utf-8\xd\xa\xd\xa", |
|||
"Goodbye, World! (hello, visitor No. $vn!)\xd\xa"; |
|||
}</lang> |
}</lang> |
||
Here's another solution using Plack (may be found on CPAN): |
Here's another solution using Plack (may be found on CPAN): |
||
Line 807: | Line 830: | ||
my $app = sub { |
my $app = sub { |
||
return [ 200, |
return [ 200, |
||
[ 'Content-Type' => 'text/html; charset=UTF-8' ], |
|||
[ '<html><head><title>Goodbye, world!</title></head><body>Goodbye, world!</body></html>' ] |
|||
] |
|||
}; |
}; |
||
my $runner = Plack::Runner->new; |
my $runner = Plack::Runner->new; |
||
Line 856: | Line 879: | ||
% wrapped in <h1></h1> tags, "Goodbye, World!". |
% wrapped in <h1></h1> tags, "Goodbye, World!". |
||
say_goodbye(_Request) :- reply_html_page([title('Howdy')], |
say_goodbye(_Request) :- reply_html_page([title('Howdy')], |
||
[h1('Goodbye, World!')]).</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<lang PureBasic>If InitNetwork() = 0 |
<lang PureBasic>If InitNetwork() = 0 |
||
MessageRequester("Error", "Can't initialize the network !") |
|||
End |
|||
EndIf |
EndIf |
||
Line 867: | Line 890: | ||
If CreateNetworkServer(0, Port) |
If CreateNetworkServer(0, Port) |
||
Repeat |
|||
Delay(1) |
|||
SEvent = NetworkServerEvent() |
|||
If SEvent |
|||
ClientID = EventClient() |
|||
Select SEvent |
|||
Case #PB_NetworkEvent_Data |
|||
SendNetworkData(ClientID,@"Goodbye, World!",Len("Goodbye, World!")) |
|||
CloseNetworkConnection(ClientID) |
|||
EndSelect |
|||
EndIf |
|||
ForEver |
|||
Else |
Else |
||
MessageRequester("Error", "Can't create the server (port in use ?).") |
|||
EndIf</lang> |
EndIf</lang> |
||