Skip to content

About

A small Java example of communication over UDP (User Datagram Protocol). A command-line client sends text datagrams to a server. The server prints each received message, including the sender's IP address and port, and sends an acknowledgment datagram back to that sender.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Java UDP Client and Server

This project is a small Java example of communication over UDP (User Datagram Protocol). A command-line client sends text datagrams to a server. The server prints each received message, including the sender's IP address and port, and sends an acknowledgment datagram back to that sender.

UDP is connectionless: the client and server exchange individual datagrams without establishing a persistent connection. Delivery and ordering are not guaranteed by UDP; the acknowledgment in this example is an application-level response, not a delivery guarantee.

Project Files

  • UDPClient.java contains the client implementation. Its declared class is udpcliente.UDPCliente.
  • UDPServer.java contains the server implementation. Its declared class is udpserver.UDPServer.

The source filenames do not match their public class names, which Java requires when compiling. The commands below copy the sources to correctly named files in a temporary build directory before compiling.

Requirements

  • Java Development Kit (JDK), with javac and java available on your PATH.

Build and Run

From the project directory, prepare correctly named source files and compile them:

mkdir -p build/src/udpcliente build/src/udpserver build/classes
cp UDPClient.java build/src/udpcliente/UDPCliente.java
cp UDPServer.java build/src/udpserver/UDPServer.java
javac -d build/classes build/src/udpcliente/UDPCliente.java build/src/udpserver/UDPServer.java

Start the server in one terminal, using a UDP port from 1 to 65535:

java -cp build/classes udpserver.UDPServer 5000

Start the client in another terminal, specifying the server host and the same port:

java -cp build/classes udpcliente.UDPCliente 127.0.0.1 5000

Enter a message in the client terminal to send it. Type exit to send that text and then leave the client's input loop. The server continues running until it is stopped manually.

Message Flow

  1. The client reads a line from standard input and sends it as a UTF-8 UDP datagram to the configured host and port.
  2. The server displays the datagram's sender address, sender port, payload length, and text.
  3. The server sends Message successfully received by server. to the datagram's source address and port.

Current Limitation

The client currently passes its outgoing datagram to receive() instead of passing the packet allocated for the server response. As a result, the response is not read into the buffer used for display, so the acknowledgment may not appear correctly. The server-side receive and acknowledgment behavior is implemented, but the client response path needs correction for the full round trip to work reliably.

About

A small Java example of communication over UDP (User Datagram Protocol). A command-line client sends text datagrams to a server. The server prints each received message, including the sender's IP address and port, and sends an acknowledgment datagram back to that sender.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages