Commit 3341853
committed
feat(udp): add DatagramClient and Server with explicit port binding and string reversal response
What
- Added two classes demonstrating UDP communication:
- DatagramClient:
- Creates a DatagramSocket bound to port 2001 (client-side port explicitly set).
- Sends "Hello Server:" as a UDP datagram to server on localhost:2000.
- Allocates a 1024-byte buffer for response, receives datagram from server, trims, and prints result.
- Closes socket after exchange.
- Server:
- Opens a DatagramSocket bound to port 2000 (server port).
- Waits for a datagram from a client, reads content into a String.
- Reverses the received string using StringBuilder.reverse().
- Sends reversed string back to the client at localhost:2001.
- Closes socket after reply.
Why
- Demonstrates core concepts of UDP in Java:
- DatagramSockets for connectionless communication.
- DatagramPackets for message encapsulation.
- Explicit port binding (server on 2000, client on 2001) to illustrate bidirectional communication.
- Helps understand message boundaries, client-server request/response pattern, and string manipulation in network programming.
- Serves as a foundation for building lightweight, low-latency communication systems.
How
- Client side:
- Binds socket to a fixed port (2001) → useful for ensuring server knows where to reply.
- Constructs DatagramPacket with msg.getBytes(), message length, server address (InetAddress.getByName("localhost")), and server port (2000).
- Sends the packet via ds.send(dp).
- Prepares buffer (byte[1024]), wraps in a DatagramPacket, blocks on receive().
- Converts bytes into String using new String(dp.getData()).trim() (trimming trailing nulls).
- Prints "From Server Hello: <response>" and closes socket.
- Server side:
- Creates DatagramSocket on port 2000 to listen for packets.
- Receives packet into a fixed buffer, extracts String with new String(dp.getData()).trim().
- Uses StringBuilder.reverse() to reverse the message.
- Constructs reply packet with reversed message, sets destination to localhost:2001 (client port).
- Sends reply, closes socket.
Key considerations / notes
- Explicit port binding:
- Client uses port 2001 → ensures server knows where to send reply.
- In contrast to ephemeral port allocation, this approach is deterministic.
- Buffer management:
- new String(dp.getData()) reads entire buffer, but .trim() removes trailing whitespace. Better practice: new String(dp.getData(), 0, dp.getLength()).
- Character encoding:
- Currently uses platform default encoding via getBytes() and new String(byte[]). For portability, specify StandardCharsets.UTF_8.
- Single exchange:
- Server handles only one packet then exits. To build a persistent service, wrap receive() in a loop.
- Blocking I/O:
- Both client and server block until data arrives. For timeout handling, configure ds.setSoTimeout(ms) and catch SocketTimeoutException.
- Threading:
- Current implementation is single-threaded; production-grade servers often handle multiple clients concurrently.
- Resource management:
- Uses manual socket closing (ds.close()). For safety, prefer try-with-resources or explicit shutdown logic.
How to test
1. Run Server (binds to port 2000).
java Server
Expected output:
From Client: Hello Server:
2. Run DatagramClient (binds to port 2001, sends message).
java DatagramClient
Expected client output:
From Server Hello: :revreS olleH
Server additionally prints reversed message was sent.
Real-life applications
- Simple request/response protocols over UDP (e.g., DNS-style queries).
- Quick-and-lightweight text-based message exchange for demos.
- Learning environment to understand connectionless datagram transport.
Future improvements
- Replace .trim() with new String(dp.getData(), 0, dp.getLength(), UTF_8).
- Convert server to loop for continuous service.
- Add logging, configurable host/port, and better error handling.
- Extend to handle multiple message types or structured payloads.
- Implement retry/ACK mechanism for reliability in lossy networks.
Files added
- DatagramClient.java — sends and receives UDP packets via explicit client port.
- Server.java — listens on fixed server port, reverses client message, replies.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 3cddd8f commit 3341853
File tree
1 file changed
+13
-10
lines changed- Section27NetworkProgramming/DataGramReverseEchoServer/src
1 file changed
+13
-10
lines changedLines changed: 13 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
37 | | - | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
45 | | - | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
50 | | - | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | | - | |
| 55 | + | |
| 56 | + | |
54 | 57 | | |
55 | | - | |
| 58 | + | |
56 | 59 | | |
57 | 60 | | |
58 | 61 | | |
59 | 62 | | |
60 | | - | |
| 63 | + | |
0 commit comments