Java UDP programming

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

  • Learn the differences between TCP and UDP and reliability
  • Learn what an UDP datagram is
  • Learn the different ways to send a UDP datagram to one or multiple clients
  • Learn UDP in the Socket API
  • How UDP can be used for service discovery
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Explore the code examples

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

Explore the code examples

Individually, or in pair/group, take 10 minutes to explore and discuss the code examples.

Answer the questions available in the course material:

  • How do the code examples work?
  • What are the main takeaways of the code examples?
  • What are the main differences between the code examples?

If needed, use the theoretical content to help you.

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

UDP

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

UDP

  • A transport layer protocol just like TCP
  • Connectionless protocol - does not require to establish a connection before sending data
  • Unreliable protocol - does not guarantee delivery but is fast
  • Analogy: sending postcards through the postal service
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Differences between TCP and UDP

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

Differences between TCP and UDP

  • TCP
    • Connection-oriented
    • Reliable
    • Stream protocol
    • Unicast
    • Request-response
    • Used for FTP, HTTP, SMTP, SSH, etc.
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
  • UDP
    • Connectionless
    • Unreliable
    • Datagram protocol
    • Unicast, broadcast and multicast
    • Fire-and-forget, request-response (manual)
    • Service discovery protocols
    • Used for DNS, streaming, gaming, etc.
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

UDP datagrams

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

UDP datagrams

  • Datagrams = discrete chunks of data (packets) sent over the network
  • Sent individually and independently
  • Contain a header (source and destination ports, length, checksum, etc.) and a payload (data)
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Reliability

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

Reliability

  • UDP is unreliable (no guarantee of delivery, no guarantee of order)
  • The application must implement its own reliability mechanism
  • In some cases, reliability is not needed (e.g. streaming)
  • Handling reliability is complex - not covered in this course
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

UDP in 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

UDP in the Socket API

  • DatagramSocket is used to send and receive datagrams
  • A datagram is created with the DatagramPacket class
  • A multicast socket is created with the MulticastSocket class.
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Unicast, broadcast and multicast

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

Unicast, broadcast and multicast

  • Unicast, broadcast and multicast are ways to send data over the network
  • TCP is unicast only
  • UDP can be unicast, broadcast or multicast
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Unicast

  • One-to-one communication
  • One sender and one receiver
  • To send a datagram, the sender must know:
    • The IP address of the receiver
    • The port of the receiver

Think of it as a private conversation between two people

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

Broadcast

  • One-to-all communication
  • One sender and multiple receivers
  • To send a datagram, the sender must know:
    • The subnet
    • The port
  • 255.255.255.255 for all hosts

Think of it as a public announcement.

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

Multicast

  • One-to-many communication
  • One sender and some receivers
  • To send a datagram, the sender must know:
    • The multicast address (between 239.0.0.0 and 239.255.255.255)
    • The port

Think of it as a group conversation.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
  • Just as with broadcast, it can be blocked by routers
  • Multicast is quite guaranteed not to work on the public Internet
  • Made for the local network
  • Multicast is a complex topic
  • Not covered in depth in this course
  • The course material contains some resources
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Messaging patterns

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

Messaging patterns

  • Fire-and-forget
    • One-way communication
    • No response
    • No guarantee of delivery
  • Request-response
    • Two-way communication
    • Response
    • Guarantee of delivery (manual)
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Service discovery protocols

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

Service discovery protocols

  • Discover services on the network
  • Two types of protocols
  • Service discovery protocol patterns:
    • Advertisement (passive)
    • Query (active)
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Questions

Do you have any questions?

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?

  • Update your application protocol with the new knowledge you gained
  • Learn to use the debugger
  • Execute the code examples and run multiple emitters at the same time
  • Explore the Java UDP programming template
  • Implement the "Temperature monitoring" application (optional)
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?

  • Learn the different ways to manage multiple clients at the same time with concurrency
  • Update the two network applications to handle multiple clients (optional):
    • The "Guess the number" game
    • The "Temperature monitoring" application
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Sources

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