Echo server: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: Add code using Socket.tcp_server_loop from Ruby 1.9.2. Keep old code for old Ruby versions.)
(whitespace)
Line 256: Line 256:
=={{header|C}}==
=={{header|C}}==
{{works with|POSIX}}
{{works with|POSIX}}

This is a rather standard code (details apart); the reference guide for such a code is the [http://beej.us/guide/bgnet Beej's Guide to Network programming]. The dependency from POSIX is mainly in the use of the <tt>read</tt> and <tt>write</tt> functions, (using the socket as a file descriptor sometimes make things simpler).
This is a rather standard code (details apart); the reference guide for such a code is the [http://beej.us/guide/bgnet Beej's Guide to Network programming]. The dependency from POSIX is mainly in the use of the <tt>read</tt> and <tt>write</tt> functions, (using the socket as a file descriptor sometimes make things simpler).


Line 379: Line 378:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>
<lang csharp>using System.Net.Sockets;
using System.Net.Sockets;
using System.Threading;
using System.Threading;


Line 442: Line 440:
}
}
}
}
}</lang>
}
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==

<lang lisp>(use '[clojure.contrib.server-socket :only (create-server)])
<lang lisp>(use '[clojure.contrib.server-socket :only (create-server)])
(use '[clojure.contrib.duck-streams :only (read-lines write-lines)])
(use '[clojure.contrib.duck-streams :only (read-lines write-lines)])
Line 458: Line 454:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==

{{improve|Common Lisp|There should be a http://common-lisp.net/project/usocket/ example.}}
{{improve|Common Lisp|There should be a http://common-lisp.net/project/usocket/ example.}}

Sockets is not a standard part of Common Lisp but many implementations have support for this. The following example {{works with|CLISP}}
Sockets is not a standard part of Common Lisp but many implementations have support for this. The following example {{works with|CLISP}}
<lang lisp>(defvar *clients* '()
<lang lisp>(defvar *clients* '()
Line 511: Line 505:


=={{header|D}}==
=={{header|D}}==

This is a very basic server that processes the buffers one character at a time. In a real-world application, the buffers would be larger. More seriously, it processes one listener at a time. If the <code>currSock.receive()</code> blocks, the loop will not process other clients. This opens the door for a trivial denial-of-service attack. A realistic echo service must multiplex clients.
This is a very basic server that processes the buffers one character at a time. In a real-world application, the buffers would be larger. More seriously, it processes one listener at a time. If the <code>currSock.receive()</code> blocks, the loop will not process other clients. This opens the door for a trivial denial-of-service attack. A realistic echo service must multiplex clients.
<lang d>// Tested using DMD 2.048

<lang d>
// Tested using DMD 2.048
import std.socket;
import std.socket;
import std.array;
import std.array;
Line 603: Line 594:


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>-module(echo).

<lang erlang>
-module(echo).
-export([start/0]).
-export([start/0]).


Line 627: Line 616:
{tcp_closed, Conn} ->
{tcp_closed, Conn} ->
io:format("Connection closed: ~p~n", [Conn])
io:format("Connection closed: ~p~n", [Conn])
end.
end.</lang>
</lang>


=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
Line 660: Line 648:
let main _ =
let main _ =
EchoService
EchoService
0
0</lang>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Line 746: Line 733:
go echo(s, i)
go echo(s, i)
}
}
}</lang>
}
</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 778: Line 764:


=={{header|Java}}==
=={{header|Java}}==

<lang java>import java.io.BufferedReader;
<lang java>import java.io.BufferedReader;
import java.io.IOException;
import java.io.IOException;
Line 1,261: Line 1,246:
{{omit from|PARI/GP}}
{{omit from|PARI/GP}}
{{omit from|Retro|No concurrency support}}
{{omit from|Retro|No concurrency support}}
{{omit from|SNUSP|No networking.}}
{{omit from|Unlambda|Does not have network access.}}
{{omit from|Unlambda|Does not have network access.}}