Java network concurrency

https://github.com/heig-vd-dai-course

Web · PDF

L. Delafontaine and H. Louis, with the help of GitHub Copilot.

Based on the original course by O. Liechti and J. Ehrensberger.

This work is licensed under the CC BY-SA 4.0 license.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Objectives

  • Program your own TCP client/server applications in Java with the Socket API
  • Understand how to handle multiple clients at the same time
  • Understand how to process data from streams

Your applications will be able to communicate over the network!

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

TCP

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

TCP

TCP is a transport protocol that is similar to a phone call:

  1. A connection is established between two parties
  2. Data sent is guaranteed to arrive in the same order
  3. Data can be sent again
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

The Socket API

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

The Socket API

  • Originally developed by Berkeley University
  • Ported to Java and many other languages
  • Provides a simple API to use TCP and UDP
  • A socket is a connection between two parties using a protocol and a port
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Client/server common functions

Operation Description
socket() Creates a new socket
getInputStream() Gets the input stream of a socket
getOutputStream() Gets the output stream of a socket
close() Closes a socket
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Client structure and functions

  1. Create a Socket
  2. Connect the socket to an IP address and a port number
  3. Read and write data from/to the socket
  4. Flush and close the socket
Operation Description
connect() Connects a socket to an IP address and a port number
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Server structure and functions

  1. Create a ServerSocket
  2. Bind the socket to an IP address and a port number
  3. Listen for incoming connections
  4. Loop
    1. Accept an incoming connection - creates a new Socket on a port
    2. Read and write data from/to the socket
    3. Flush and close the socket
  5. Close the ServerSocket
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
Operation Description
bind() Binds a socket to an IP address and a port number
listen() Listens for incoming connections
accept() Accepts an incoming connection

To make it simple, a socket is just like a file that you can open, read from, write to and close. To exchange data, sockets on both sides must be connected.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Processing data from streams

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Processing data from streams

  • Sockets use data streams to send and receive data, just like files
  • Get an input stream to read data from a socket
  • Get an output stream to write data to a socket
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Variable length data

Data sent can have a variable length. Manage this using one of the two methods:

  • Use a delimiter
  • Communicate a fixed length

This must be defined by your application protocol!

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Using a delimiter:

// End of transmission character
String EOT = "\u0004";

// Read data until the delimiter is found
String line;
while ((line = in.readLine()) != null && !line.equals(EOT)) {
  System.out.println(
    "[Server " + SERVER_ID + "] received data from client: " + line
  );
}
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Communicating a fixed length:

// Send the length of the data
out.write("DATA_LENGTH " + data.length() + "\n");

// Send the data
out.write(data);
// Read the length of the data
String[] parts = in.readLine().split(" ");
int dataLength = Integer.parseInt(parts[1]);

// Read the data
for (int i = 0; i < dataLength; i++) {
  System.out.print((char) in.read());
}
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Handling one client at a time

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Handling one client at a time

  1. Create a socket to listen to incoming connections
  2. Create a new socket for the client
  3. Handle the connection

Analogy: a restaurant with one table

➡️ Simple but quite useless...

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Handling multiple clients at the same time

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Handling multiple clients at the same time

Handle multiple clients at the same time is called concurrency.

Concurrency can be achieved with:

  • Multi-processing
  • Multi-threading
  • Asynchronous programming
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Multi-processing

  • Create an entirely new process for each client
  • Heavyweight and slow
  • Not recommended
  • Analogy: a new restaurant for each customer, including the kitchen, waiters, etc.
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Multi-threading

  • Create a new thread for each client
  • More lightweight and faster than multi-processing
  • Recommended with thread pool to limit resource usage
  • Not enough or too many threads can slow down the system
  • Analogy: a new or limited number of tables for each customer
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Asynchronous programming

  • Handle multiple clients with a single thread
  • Very performant!
  • Analogy: a food truck - you get a ticket, wait, get your food and leave
  • Out of scope for this course but interesting to know! Node.js is a good example for this approach
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Practical content

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

What will you do?

  • Send an email using a SMTP client written in Java with the Socket API
  • Run full client/server examples and understand how concurrent clients are handled
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Find the practical content

You can find the practical content for this chapter on GitHub.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Finished? Was it easy? Was it hard?

Can you let us know what was easy and what was difficult for you during this chapter?

This will help us to improve the course and adapt the content to your needs. If we notice some difficulties, we will come back to you to help you.

➡️ GitHub Discussions

You can use reactions to express your opinion on a comment!

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

What will you do next?

You will start the practical work!

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Sources

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0