Sockets: Difference between revisions

From Rosetta Code
Content added Content deleted
(New page: For this excersise 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 require...)
 
m (Changed over to headers and added a task tag.)
Line 1: Line 1:
{{task}}
For this excersise 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
For this excersise 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


=Creating a outgoing socket=
----
----


==Ada==
=={{header|Ada}}==
'''Compiler:''' Gnat 3.15p
'''Compiler:''' Gnat 3.15p
this example is specific to the Gnat Compiler.
this example is specific to the Gnat Compiler.
Line 29: Line 29:
end;
end;


==java==
=={{header|Java}}==
import java.net.*;
import java.net.*;
public class SocketSend
public class SocketSend

Revision as of 22:52, 26 November 2007

Task
Sockets
You are encouraged to solve this task according to the task description, using any language you may know.

For this excersise 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("127.0.0.1","Hello Socket World"); }
 public static void sendData(String ip, String msg) throws java.io.IOException
    {
    Socket sock = new Socket( ip , 256 );
    sock.getOutputStream().write(msg.getBytes());
    sock.getOutputStream().flush();
    sock.close();   
    }
 }