Java Snippets

Hamburger icon

Stream Sockets

This folder contains a group of programs that communicate through stream sockets to send and receive bits of data. For this we create a server class and a client class.

In the server class: In the client class:

ServerSocketStream and ClientSocketStream

This is a simple implementation of a server and a client that exchange a string message using stream sockets, following the previous points. Compile them and then run each in a separate terminal. Make sure the server is listening before you run 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.

FileServerSS and FileClientSS

This pair implement a server/client connection to send files using Stream Sockets (SS). The client sends a file (file.txt) to the server, and the server receives it and prints its contents. To read the file, a BufferedReader is used by the client with an object of the FileReader class. The file is in the same folder as the programs.

The two programs can run in the same computer, in different terminals. The IP used is localhost and the port is 5555.

To read the contents of the file and print them in the stdout, we manipulate the InputStream object with a Scanner:

InputStream in      = newSocket.getInputStream();
String filecontents = "";
Scanner s           = new Scanner(in, "UTF-8").useDelimiter("\\A | \\n");
while (s.hasNext())
	filecontents   +=;

For the lazy: If you don't want to open two terminals manually, just compile and run, which will do that for you. Running $ java CSManager is equivalent to running the server in another terminal with:

$ gnome-terminal -x sh -c "java FileServerSS; exec $SHELL"

and then typing $ java FileClientSS in the current terminal. But, is not needed, you can start the server manually by typing java FileServerSS and then type java FileClientSS in another terminal as many times as you need it, so long as the server is listening.

The CSManager class uses the method exec() from class Runtime, and passes the command, environment and working directory:

Runtime rt   = Runtime.getRuntime();
Process proc = rt.exec(cmdarray, envp, dir);

Go to code.

CalcServer and CalcClient

This is an example of multi-thread server when the order in which messages are sent and received is fixed beforehand. It implements a calculator where the client sents operations for the server to perform.

First, the client sends several messages in order, and the server receives them as they come. Then, the client is set to listen for the result that the server will send.

The server extends the class Thread, and reads and writes in its run() method. It uses DataInputStream and DataOutputStream to send and receive doubles.

In the main() method, it creates a thread for each socket that connects to it:

CalcServer thread = new CalcServer(newSocket);

Go to code.

FileServer and FileClient

This is another file exchange server, but implementing multi-threading. The server stores text files in the Files folder, which is in the same directory as the program. The client asks for a file, specifying the file name, without extension:

$ java FileClient filename

If the file is in the server, it is sent to the client, character by character. The client receives the file and prints it to its stdout. To read the file, we use again a BufferedReader with a FileReader object.

// Sending file
if(file.exists() && !file.isDirectory()) {
	br = new BufferedReader(new FileReader(filepath));
	String line  = null;
	while ((line = br.readLine()) != null) {
		for(int i=0; i<line.length(); i++) {
// Receiving file
int character;
while ((character = != -1) {
	char ch = (char) character;

The programs can be launched each in a different terminal, in the same computer. The server listens at localhost:5555

View Stream Sockets' repo