Sockets: Difference between revisions
(added haskell; not tested) |
|||
Line 41: | Line 41: | ||
socket.close() ; |
socket.close() ; |
||
}</d> |
}</d> |
||
=={{header|Haskell}}== |
|||
import Network |
|||
⚫ | |||
main = withSocketsDo $ sendTo "localhost" (PortNumber $ toEnum 256) "hello socket world" |
|||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
Line 55: | Line 60: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<java>import java.net.*; |
|||
public class SocketSend { |
|||
public static void main(String args[]) throws java.io.IOException { |
|||
sendData("localhost", "Hello Socket World"); |
|||
} |
|||
⚫ | |||
public static void sendData(String host, String msg) throws java.io.IOException{ |
|||
Socket sock = new Socket( host, 256 ); |
|||
sock.getOutputStream().write(msg.getBytes()); |
|||
sock.getOutputStream().flush(); |
|||
sock.close(); |
|||
} |
|||
}</java> |
|||
} |
|||
Encapsulating the Socket's OutputStream in a PrintStream (for data) or PrintWriter (for text) may be easier in more complex programs for their auto-flush abilities and their overloaded print and println methods. The write method from the original OutputStream will still be available. |
Encapsulating the Socket's OutputStream in a PrintStream (for data) or PrintWriter (for text) may be easier in more complex programs for their auto-flush abilities and their overloaded print and println methods. The write method from the original OutputStream will still be available. |
||
Line 90: | Line 95: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<perl>use Socket; |
|||
$host = gethostbyname('localhost'); |
|||
$in = sockaddr_in(256, $host); |
|||
$proto = getprotobyname('tcp'); |
|||
socket(Socket_Handle, AF_INET, SOCK_STREAM, $proto); |
|||
connect(Socket_Handle, $in); |
|||
send(Socket_Handle, 'Hello socket world', 0, $in); |
|||
close(Socket_Handle);</perl> |
|||
Object oriented version. |
Object oriented version. |
||
<perl>use Socket::Class; |
|||
$sock = Socket::Class->new( |
|||
'remote_port' => 256, |
|||
) || die Socket::Class->error; |
|||
$sock->send('Hello socket world'); |
|||
$sock->free;</perl> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
<python>import socket |
|||
sock = socket.socket(AF_INET, SOCK_STREAM) |
|||
sock.connect(("localhost", 256)) |
|||
sock.sendall("hello socket world") |
|||
sock.close()</python> |
|||
=={{header|Rhope}}== |
=={{header|Rhope}}== |
Revision as of 01:26, 24 November 2008
You are encouraged to solve this task according to the task description, using any language you may know.
For this exercise a program is open a socket to localhost on port 256 and send the message "hello socket world" before closing the socket. Catching any exceptions or errors is not required.
Ada
<ada> with GNAT.Sockets; use GNAT.Sockets;
procedure SocketSend is
procedure sendData (IP : String; Msg : String) is Client : Socket_Type; Address : Sock_Addr_Type; Channel : Stream_Access; begin Create_Socket (Client); Address.Addr := Inet_Addr(ip); Address.Port := 256; Connect_Socket (Client, Address); Channel := Stream (Client); String'Write (Channel, Msg); Close_Socket (Client); end;
begin
Initialize; sendData ("127.0.0.1","Hello Socket World");
end; </ada>
D
<d>module socket ; import std.stdio ; import std.socket ; version(Win32) {
// For Win32 systems, need to link with ws2_32.lib. pragma(lib, "ws2_32.lib") ;
} void main() {
auto socket = new Socket(AddressFamily.INET, SocketType.STREAM) ; socket.connect(new InternetAddress("localhost",256)) ; writefln(socket.send(cast(void[])"Hello socket world"), " bytes sent.") ; socket.close() ;
}</d>
Haskell
import Network main = withSocketsDo $ sendTo "localhost" (PortNumber $ toEnum 256) "hello socket world"
IDL
socket, unit, 'localhost',256,/get_lun
printf,unit,"Hello socket world"
close, unit
"Well-known" port numbers (under 1024 -- such as 256) can also be specified by name (in this case 'RAP').
If there is no listener on this port, this will hang for a while before timing out.
Java
<java>import java.net.*; public class SocketSend {
public static void main(String args[]) throws java.io.IOException { sendData("localhost", "Hello Socket World"); }
public static void sendData(String host, String msg) throws java.io.IOException{ Socket sock = new Socket( host, 256 ); sock.getOutputStream().write(msg.getBytes()); sock.getOutputStream().flush(); sock.close(); }
}</java> Encapsulating the Socket's OutputStream in a PrintStream (for data) or PrintWriter (for text) may be easier in more complex programs for their auto-flush abilities and their overloaded print and println methods. The write method from the original OutputStream will still be available.
OCaml
<ocaml>open Unix
let init_socket addr port =
let inet_addr = (gethostbyname addr).h_addr_list.(0) in let sockaddr = ADDR_INET (inet_addr, port) in let sock = socket PF_INET SOCK_STREAM 0 in connect sock sockaddr; (* convert the file descriptor into high-level channels: *) let outchan = out_channel_of_descr sock in let inchan = in_channel_of_descr sock in (inchan, outchan)</ocaml>
<ocaml>let () =
let ic, oc = init_socket "localhost" 256 in output_string oc "Hello Socket World";
- </ocaml>
Perl
<perl>use Socket;
$host = gethostbyname('localhost'); $in = sockaddr_in(256, $host); $proto = getprotobyname('tcp'); socket(Socket_Handle, AF_INET, SOCK_STREAM, $proto); connect(Socket_Handle, $in); send(Socket_Handle, 'Hello socket world', 0, $in); close(Socket_Handle);</perl>
Object oriented version. <perl>use Socket::Class;
$sock = Socket::Class->new(
'remote_port' => 256,
) || die Socket::Class->error; $sock->send('Hello socket world'); $sock->free;</perl>
Python
<python>import socket sock = socket.socket(AF_INET, SOCK_STREAM) sock.connect(("localhost", 256)) sock.sendall("hello socket world") sock.close()</python>
Rhope
Socket Send(0,0) |: [New@Net Client["localhost",256]]Put String["hello socket world"] :|
The connection is automatically closed when the object is freed.
Seed7
$ include "seed7_05.s7i"; include "socket.s7i"; const proc: main is func local var file: sock is STD_NULL; begin sock := openInetSocket(256); writeln(sock, "hello socket world"); close(sock); end func;
Tcl
set io [socket 127.0.0.1 256]
puts $io "Hello socket world"
close $io
Toka
needs sockets #! A simple abstraction layer that makes writing trivial servers easy value| server.socket server.connection server.action | [ ( n- ) pBind to server.socket ] is server.setSocket [ ( - ) server.socket pAccept to server.connection ] is server.acceptConnection [ ( - ) server.connection pClose drop ] is server.closeConnection [ ( $- ) >r server.connection r> string.getLength pWrite drop ] is server.send [ ( an- ) server.connection -rot pRead drop ] is server.recieve [ ( qn- ) swap to server.action server.setSocket [ server.acceptConnection server.action invoke server.closeConnection TRUE ] whileTrue ] is server.start #! The actual server [ " hello socket world" server.send ] 256 server.start
UnixPipes
(echo "Hello World" | nc localhost 256 | exit 0)