Laboratory 2: Information Packets
Laboratory 2: Information Packets
Broadcasting refers to transmitting a packet that will be received (conceptually) by every device on the network. In
practice, the scope of the broadcast is limited to a broadcast domain.
Multicast is... a need. Multicast is a network addressing method for the delivery of information to a group of
destinations simultaneously using the most efficient strategy to deliver the messages over each link of the network
only once, creating copies only when the links to the multiple destinations split.
UDP sockets
Like in the case of TCP sockets we usually split the applications in two main categories: servers and clients.
But in this situation we do not have UDP client and server sockets, we speak only of UDP sockets -- as UDP is a
connection-less protocol. Nonetheless in order for the communication to take place the same conditions -- like in the
case of TCP -- must be met:
the server should listen on a certain IP address and port -- again the local socket address;
the client should know in advance the exact IP address and port of the server -- the remote socket address;
the client must also have a local socket address, just like in the case of the server;
Life-cycle
Thus the generic life-cycle of an UDP socket would be:
As we can see there is nowhere any connection being made, and thus both the client and the server act the same way,
the only distinction is that usually the client is the one that initiates the dialog.
Data exchange
The data exchange step is split in two actions:
sending a datagram:
datagram creation -- we create a datagram object for which we establish:
the data buffer -- what we want to send;
the destination socket address -- where do we want to send the datagram; this is composed of an
IP and port of the other entity;
datagram sending -- we actually send the datagram over the network to the specified destination;
receiving:
datagram creation -- we create a datagram object for which we establish:
the buffer -- where we want to receive the data;
we wait for datagrams, and when one arrives we obtain it;
2
we must obtain the datagram length, in order to know how much of the buffer has been written;
we may also obtain the sender's socket address;
We could observe from the previous steps that the data exchange could be done with multiple entities, thus one client
could speak with more than one server (or one server with multiple clients) by using the same socket -- something
which in the case of TCP sockets is not possible.
Also we should observe that in the case of UDP sockets we must prepare in advance the buffer where we want to
receive the data, and in the case buffer is to small to hold the entire received datagram, usually an error will be raised,
and we can retry with a greater buffer.
Java API
java.net:
DatagramSocket:
constructor() -- creates a datagram socket and binds it to a random local port -- the IP address is
0.0.0.0;
constructor(SocketAddress) -- creates a datagram socket and binds it to the specified socket
address;
connect(SocketAddress) -- this method is used to establish a default remote socket address for
datagrams -- thus giving us the impression of a connection-oriented socket;
send(DatagramPacket) -- sends a datagram;
receive(DatagramPacket) -- waits and fills the datagram object; the buffer and remote socket
address will be set-up;
getLocalSocketAddress() -- used to obtain the local socket address;
getRemoteSocketAddress() -- used to obtain the remote socket address -- the one setup by
connect;
close() -- used to destroy the socket, and after this it will not be usable;
DatagramPacket:
constructor(byte, int, int) -- the constructor used to establish only the data buffer and positions; it
3
Examples
resolving the remote (destination) socket address (IP and port) -- this is usually done on the client side:
sending a datagram:
creating the datagram:
socket.send (requestDatagram);
receiving a datagram:
creating the datagram:
socket.receive (responseDatagram);
obtaining the remote (source) socket address -- this is usually done on the server side:
socket.close ();
Observation: implicit and explicit refers to the fact that the sender must know the exact address of the involved
destinations:
the local network broadcast address; for example if our IP is 10.10.10.8, and the netmask is 255.255.255.0, then
the local network broadcast address is 10.10.10.255;
the global network broadcast address: 255.255.255.255;
Observation: if a host makes a broadcast, the sent packet is tagged with the its (the sender) IP address, and thus any
receiving host can determine the source address.
Client
import
import
import
import
import
java.net.DatagramPacket;
java.net.DatagramSocket;
java.net.InetAddress;
java.net.InetSocketAddress;
java.net.SocketAddress;
Server
import
import
import
import
import
java.net.DatagramPacket;
java.net.DatagramSocket;
java.net.InetAddress;
java.net.InetSocketAddress;
java.net.SocketAddress;
Assignment
1)Write a TCP client application that:
takes zero or two arguments:
the first argument is the DNS name of the server;
the second argument is the port of the server;
in case there are no arguments the name server is assumed to be hephaistos.info.uvt.ro and the port
7391;
it connects by using a TCP socket to the given server;
it waits for commands from the server and -- when applicable -- will give an answer;
it simulates a simple arithmetic calculator that has a single register to which will operate according to the
received commands -- initially the register is 0;
it handles all exception cases, and for each exception will show an error message -- using System.err -- and will
exit gracefully;
in case of errors encountered when decoding the protocol an error message -- using System.err -- will be shown
and will exit gracefully;
The protocol is as follows:
each command, command argument and response are lines -- strings ended by LF, '\n';
the commands are sent by the server, and the client only sends back an answer when it should;
the commands are:
addition command: a line containing only the character '+', followed by a line containing a number; no
answer is sent by the client; the number is added to the register;
substraction command: the same as addition command, but the first line contains just the character '-';
the number is substracted from the register;
multiplication command: '*', see above;
division command: '/', see above;
result command: a line containing only the string '='; the client will send as answer a line containing the
current value of the register; and will wait for a line from the server which validates the answer (either a
line containing ok or nok depending on the fact that the client executed the operation correctly); after
this it resumes normal operation; please note that the register is not reseted to 0, it retains its value;
(please see the example);
exit command: a line containing only the string 'exit'; the client will close the connection and exit;
Observations:
Example dialog:
s->c
s->c
s->c
s->c
s->c
s<-c
s->c
s->c
s->c
s->c
s<-c
s->c
s->c
ok<LF>
-<LF>
10<LF>
=<LF>
2<LF>
ok<LF>
exit<LF>
s->c
s<-c
s->c
s->c
=<LF>
0<LF>
ok<LF>
exit<LF>
s->c exit<LF>
java.io.BufferedReader;
java.io.BufferedWriter;
java.io.InputStreamReader;
java.io.OutputStreamWriter;
java.net.InetAddress;
java.net.InetSocketAddress;
java.net.ServerSocket;
java.net.Socket;
java.util.Random;
10
break command_loop;
}
String status = (Integer.toString (register) .equals (response)) ? "ok" : "nok";
writer.write (status + "\n");
writer.flush ();
break;
}
case 5 : {
writer.write ("exit\n");
writer.flush ();
String response = reader.readLine ();
if (response != null)
System.err.println ("Protocol error!");
break command_loop;
}
default :
throw new Error ();
}
// Closing the client socket
client.close ();
} catch (Throwable error) {
}
} .start ();
}
2)
Write a UDP client that:
Important: When sending the PING-PONG message, be careful to send it on the socket address from where the
datagram came. See the method getSocketAddress from DatagramPacket class.
11
Example dialog
client->broadcast PING
server->client PONG
client->server PING-PONG
Server Source
import
import
import
import
java.net.DatagramPacket;
java.net.DatagramSocket;
java.net.InetAddress;
java.net.InetSocketAddress;
} else if (request.startsWith("PING-PONG")) {
System.out.println("ESTABLISHED <-> " + clientAddr.getHostAddress());
}
}
}
12