Java Snippets

Hamburger icon

Datagram Sockets

This folder contains a group of programs that communicate through datagram sockets to send and receive bits of data. Each datagram socket can act as a server or as a client.

Steps for a datagram socket connection:

DatagramReceptor and DatagramEmitter

This is a simple implementation of a server and a client that exchange a string message using datagram sockets, following the previous points. Compile them and then run each in a separate terminal. Make sure the receptor is listening before you run the emitter.

Here, one datagram socket is acting as the server, and the other as the client. The client connects to the server and sends a message. When the server receives the message, it prints it in its standard output.

Go to code.

DatagramReceiver and DatagramSender

Here, both receiver and sender send and receive a token, but one has to be listening first; that's the receiver. Both programs are acting as server and as client to each other: the receiver first starts acting as a server, listening until it receives the token from the sender, when this happens, it sends a message with the token, acting as a client. On the other side, the sender first sends a token, and then receives it back from the receiver.

Both programs can be run on the same computer, on different terminals. Be sure to run the receiver first. They listen on localhost:5555.

If you don't want to run them in two terminals manually, you can use SRManager.java, that works as CSManager.java in the stream sockets, just introducing a small delay of half a second between both programs to make sure that the receiver is first.

try {
	Thread.sleep(500);
} catch(InterruptedException ex) {
	Thread.currentThread().interrupt();
}

It also adds a new printing function.

Go to code.

SocketRing and RingManager

This is a pair of programs that implement a ring of datagram sockets. The SocketRing class has two arguments: the size of the ring (how many sockets on it) and the position of the current socket in the ring.

Each socket waits until it receives the message from the previous socket in the ring, and then sends it to the next socket in the ring (like the scissors game). The first socket in the ring creates the message and receives it from the last socket in the ring. All the sockets are created from the same program, SocketRing, but are run with different input arguments.

The first socket is at position 0, and the minimum number of sockets is two. Each socket listens in a different port and they use ports from 5550 on.

The SocketRing class first checks the input parameters, then assigns the port numbers:

// Assigning ports
int startPort = 5550;
int thisPort  = startPort + position;
int nextPort  = thisPort + 1;
if (position == nOfProcs - 1)
	nextPort  = startPort;

From the second socket on, they first receive and then send. Using conditionals, we make the first socket send first and then receive. Before sending the token to the next position, a delay of one second is introduced, to make sure that all the sockets are listening before we start sending messages.

if (position == 0) {
	message = "token".getBytes();
	try {
		Thread.sleep(1000);
	} catch(InterruptedException ex) {
		Thread.currentThread().interrupt();
	}
}

Just like in previous snippets, the different gnome terminals are launched with the help of RingManager.java. It implements a loop through the number of sockets in the ring. In each loop, it launches a terminal to run the command $ java SocketRing nOfProcs position:

int nOfProcs = 3;
for(int i=0; i<nOfProcs; i++) {
	String[] cmdarray = {"gnome-terminal", "--geometry=100x15+1000+" + i*300,  "-x", "sh", "-c", "java SocketRing " + nOfProcs + " " + i + "; exec $SHELL"};
	csm.launch(cmdarray, envp, dir);
}

To run the snippet, simply call the RingManager:

$ java RingManager

Go to code.

TimeServer and TimeClient

This is an example of a time server using datagram sockets. The client sends a message specifying the date format, and the server sends the date back.

View Datagram Sockets' repo