Sockets
Sockets
You are encouraged to solve this task according to the task description, using any language you may know.
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
Compiler: Gnat 3.15p this example is specific to the Gnat Compiler.
with Ada.Text_IO; use Ada.Text_IO; 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; done : boolean :=false; 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;
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(); } }
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);
Python
Interpreter: Python
import socket sock = socket.socket(AF_INET, SOCK_STREAM) sock.connect(("localhost", 256)) sock.send("hello socket world") sock.close()
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