UDP Client

First UDP Client

In this exercise, we want to write a UDP client NetcatUDP allowing to send packets to a server. The packets will contain the bytes of string encoded in a given charset. The program takes as arguments the IP address and port number of the server and the charset name.
For instance, we can call the program is follows:

$ java fr.uge.net.udp.NetcatUDP gaspard.univ-mlv.fr 7 UTF-8

The program will behave as follows in a loop:

Starting from the template NetcatUDP.java, write the program NetcatUDP.

Tests

To test your client, you can use a server for the Echo protocol(RFC 862) which is usualy on port 7. This protocol is very simple : for every packet received, the server sent back a packet containing the same data to the sender.

Test 1
If you are on a university computer:

You can use the Echo server on the machine gaspard.univ-mlv.fr. You should obtain:

  
$ java fr.uge.net.udp.NetcatUDP gaspard.univ-mlv.fr 7 UTF-8
C'est beau l'€
Received 16 bytes from /193.55.61.20:7
String: C'est beau l'€
As the packet returned are identical to the packet send, it is normal that you end up printing the same string that you sent.

If you are on your laptop:

You do not have access to the machine gaspard.univ-mlv.fr from your laptop or from home. To test, you need to start a Echo server locally. Download ServerEchoUDP.jar and in a terminal, run:

$ java -jar ServerEchoUDP.jar 7777
which starts a Echo server on the port 7777 of your machine. You test your client with:
  
$ java fr.uge.net.udp.NetcatUDP localhost 7777 UTF-8
C'est beau l'€
Received 16 bytes from /127.0.0.1:7777
String: C'est beau l'€
As the packet returned are identical to the packet send, it is normal that you end up printing the same string that you sent.

Test 2

We will now test your client with a different server. Download ServerUpperCaseUDP.jar and start it on the port 4545 of your machine:

$ java -jar ServerUpperCaseUDP.jar 4545 UTF-8
The server decodes all packets in its starting charset (UTF-8 here) and sends back the bytes in the charset of the uppercased string. You should obtain the following behaviour:
$ java fr.uge.net.udp.NetcatUDP localhost 4545 UTF-8
Autant arrêter le Java si c'est pour ça!
Received 42 bytes from /127.0.0.1:4545
String: AUTANT ARRÊTER LE JAVA SI C'EST POUR ÇA!
ah l'€
Received 8 bytes from /127.0.0.1:4545
String: AH L'€

Start your client and server with two different charsets. For example, in two distinct terminals, you can run:

$ java -jar ServerUpperCaseUDP.jar 4545 Latin1
$ java fr.uge.net.udp.NetcatUDP 4545 UTF-8

What happens if you send the string "Ah l'€!"?

Better Uppercase Protocole

The server ServerBetterUpperCaseUDP.jar uses a different protocol in which the packets contain both the charset name and the bytes of the strings. The precise protocol is described bellow:

BetterUpperCase Protocol

To send a string, the client sends a packet with the following format:
  • An int (4 bytes) in BigEndian corresponding to the size (in bytes) of the charset name encoded in ASCII
  • The charset name encoded in ASCII
  • The bytes of the string encoded in this charset
The server sends back its answer using the same packet format.
Warning: the charset used for the answer is not necessarily the charset of the query.
Packets cannot be larger than 1024 bytes.

For example, the string "élo" in the charset Latin1 will be sent as:

00 00 00 06 4c 61 74 69 6e 31 e9 6c 6f
which can be decomposed as follows:
00 00 00 06  --> 6 as a BigEndian int = the number of bytes for "Latin1" encoded in ASCII
4c 61 74 69 6e 31 --> "Latin1" encoded in ASCII
e9 6c 6f --> "élo" encoded in Latin1

Starting with the template ClientBetterUpperCaseUDP.java, write the methods decodeMessage et encodeMessage while respecting the conventions written in the documentation of these methods.

Tests

Test 1

Use the JUnit 5 tests ClientBetterUpperCaseUDPTest.java to test your code.

Test 2

Start the server with:

$ java -jar ServerBetterUpperCaseUDP.jar 7778
We expect a to have:
$ java fr.uge.net.udp.ClientBetterUpperCaseUDP localhost 7778 Latin1
au boulot!
AU BOULOT!
c'est à finir pour la prochaine séance!
C'EST À FINIR POUR LA PROCHAINE SÉANCE!