Lab 1 Retele
Lab 1 Retele
Retele de calculatoare
Socket
A socket is an abstraction of a communication channel end-point (either network communication or local inter-process
communication), that allows us to exchange data between the peers.
In order to work with sockets we need an API. This API was initially introduced by Berkeley university in 1983, long
before the Internet -- TCP/IP -- was born. The API was initially only available for C programming language, but it was
quickly adopted by all languages as libraries, and it is today a de facto standard for network application interfaces.
The most important operations that are available to us are:
receiving data;
sending data;
closing the socket;
These operations can be executed only when the socket is in a certain state; states which are determined by the life cycle
of the socket:
TCP sockets
The most common use for sockets is to write TCP applications, which are split in two categories:
servers:
applications that usually run on dedicated hardware and software -- on a server machine;
these applications offer a well defined service to any client that communicates with it;
clients:
applications that usually run on the users computer -- a workstation, desktop, laptop or even smart-phone
and PDAs;
these applications connect to a specific server, send requests, and wait for responses;
As a consequence the sockets are also split in two types:
server sockets -- used on the server side;
client sockets -- used on the client side;
As noted previously a client needs to connect to a server, resulting that the two sockets -- the one from the server and the
one from the client -- are related to each other -- they are connected. In order to establish a connection the following main
conditions must be met:
the server has to listen on a specific IP address and port -- this is the server's socket local address;
the client has to connect on to that particular IP address and port -- this is the client's socket remote address;
Java API
java.net:
Socket:
bind(SocketAddress) -- used to establish the socket's local address;
connect(SocketAddress) -- used to establish the socket's remote address, and try to establish a
connection;
shutdownInput();
shutdownOutput();
close() -- used to terminate the connection;
getInputStream() -- used to obtain the input -- incoming -- byte stream;
getOutputStream() -- used to obtain the output -- outgoing -- byte stream;
getLocalSocketAddress() -- used to obtain the local socket address;
getRemoteSocketAddress() -- used to obtain the remote socket address -- who is on the other side;
Examples
Creating the client socket:
Socket socket = new Socket ();
Java API
java.net:
ServerSocket:
bind(SocketAddress) -- this establishes the socket's local address -- the one that the client must be
aware of; this also puts the socket in a listening state;
accept() -- waits for an incoming connection and returns a (client) socket representing that
connection;
close() -- stops the acceptance of new connections and releases all the socket resources;
getLocalSocketAddress() -- used to retrieve the local socket address;
Examples
Creating the server socket:
ServerSocket socket = new ServerSocket ();
Binding to the local socket address -- this is the one the clients should be connecting to:
InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
int localIpPort = 80;
SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
socket.bind (localSocketAddress);
Integer:
parseInt(String) -- used to parse (obtain) an integer from a string;
Complete example
The following two applications are a simple client and server that allow a user to make the following queries:
send hello, and receive hello;
send get-time, and receive the server's time in number of milliseconds elapsed from 1970;
send get-random, and receive a random number;
The protocol is pretty simple: send one line -- the request -- receive one line -- the response;
The client is started as (where bin is a folder where the byte-compiled classes are found):
java -classpath ./bin Client <host> <port> <request>
java -classpath ./bin Client 127.0.0.1 7654 hello
Client
import
import
import
import
import
import
import
java.io.BufferedReader;
java.io.BufferedWriter;
java.io.InputStreamReader;
java.io.OutputStreamWriter;
java.net.InetAddress;
java.net.InetSocketAddress;
java.net.Socket;
Server
import
import
import
import
import
import
import
import
java.io.BufferedReader;
java.io.BufferedWriter;
java.io.InputStreamReader;
java.io.OutputStreamWriter;
java.net.InetAddress;
java.net.InetSocketAddress;
java.net.ServerSocket;
java.net.Socket;
}
}
}
} .start ();