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
.
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.
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.
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 7777which 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.
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-8The 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
"Ah l'€!"
?
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:
int
(4 bytes) in BigEndian corresponding to the size (in bytes) of the charset name encoded in ASCII
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 6fwhich 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.
Use the JUnit 5 tests ClientBetterUpperCaseUDPTest.java to test your code.
Start the server with:
$ java -jar ServerBetterUpperCaseUDP.jar 7778We 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!