Bonjour,
Pour un TP de réseaux, on avait le code d'un serveur qui reçoit des paquets UDP, il nous est demandé d'écrire le code du client qui en envoie.
Voici le code des 2 :
Serveur :
Client :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 import java.io.*; import java.net.*; import java.util.*; /* * Server to process ping requests over UDP. */ public class PingServer { private static final double LOSS_RATE = 0.3; private static final int AVERAGE_DELAY = 100; // milliseconds public static void main(String[] args) throws Exception { // Get command line argument. if (args.length != 1) { System.out.println("Required arguments: port"); return; } int port = Integer.parseInt(args[0]); // Create random number generator for use in simulating // packet loss and network delay. Random random = new Random(); // Create a datagram socket for receiving and sending UDP packets // through the port specified on the command line. DatagramSocket socket = new DatagramSocket(port); // Processing loop. while (true) { // Create a datagram packet to hold incomming UDP packet. DatagramPacket request = new DatagramPacket(new byte[1024], 1024); // Block until the host receives a UDP packet. socket.receive(request); // Print the recieved data. printData(request); // Decide whether to reply, or simulate packet loss. if (random.nextDouble() < LOSS_RATE) { System.out.println(" Reply not sent."); continue; } // Simulate network delay. Thread.sleep((int) (random.nextDouble() * 2 * AVERAGE_DELAY)); // Send reply. InetAddress clientHost = request.getAddress(); int clientPort = request.getPort(); byte[] buf = request.getData(); DatagramPacket reply = new DatagramPacket(buf, buf.length, clientHost, clientPort); socket.send(reply); System.out.println(" Reply sent."); } } /* * Print ping data to the standard output stream. */ private static void printData(DatagramPacket request) throws Exception { // Obtain references to the packet's array of bytes. byte[] buf = request.getData(); // Wrap the bytes in a byte array input stream, // so that you can read the data as a stream of bytes. ByteArrayInputStream bais = new ByteArrayInputStream(buf); // Wrap the byte array output stream in an input stream reader, // so you can read the data as a stream of characters. InputStreamReader isr = new InputStreamReader(bais); // Wrap the input stream reader in a bufferred reader, // so you can read the character data a line at a time. // (A line is a sequence of chars terminated by any combination of \r and \n.) BufferedReader br = new BufferedReader(isr); // The message data is contained in a single line, so read this line. String line = br.readLine(); // Print host address and data received from it. System.out.println( "Received from " + request.getAddress().getHostAddress() + ": " + new String(line) ); } }
Mon problème se situe au niveau ru socket.receive(request); côté serveur. En effet, au lancement des 2 codes, lorsque le client envoie ses messages, le serveur reste bloqué à cette ligne de commande.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 import java.io.*; import java.net.*; import java.util.*; import java.util.Date; /* * Client to process ping requests over UDP. */ public class PingClient { static Date date = new Date(); public static void main(String args[]) throws Exception { //Get command line argument. if (args.length != 2) { System.out.println("Required arguments: host and port"); return; } InetAddress clientHost = InetAddress.getByName(args[0]); int clientPort = Integer.parseInt(args[1]); DatagramSocket socket = new DatagramSocket(clientPort); socket.setSoTimeout(1000); //Loop for sending the 10 different requests for(int i=0;i<10;i++){ // Create a datagram packet to send UDP packet byte[] packetContent = ("PING " + i + date.getTime() + "\r\n").getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(packetContent); InputStreamReader isr = new InputStreamReader(bais); BufferedReader br = new BufferedReader(isr); DatagramPacket request = new DatagramPacket(packetContent,packetContent.length, clientHost, clientPort); socket.send(request); System.out.println("Request sent for packet: " + i); //long RTTend = date.getTime(); //long RTT = RTTend - RTTstart; } } /* * Print ping data to the standard output stream. */ private static String printACK(DatagramPacket request) throws Exception { // Obtain references to the packet's array of bytes. byte[] buf = request.getData(); // Wrap the bytes in a byte array input stream, // so that you can read the data as a stream of bytes. ByteArrayInputStream bais = new ByteArrayInputStream(buf); // Wrap the byte array output stream in an input stream reader, // so you can read the data as a stream of characters. InputStreamReader isr = new InputStreamReader(bais); // Wrap the input stream reader in a bufferred reader, // so you can read the character data a line at a time. // (A line is a sequence of chars terminated by any combination of \r and \n.) BufferedReader br = new BufferedReader(isr); // The message data is contained in a single line, so read this line. String line = br.readLine(); // Print the response of the server. return line; } }
Mon paramètre pour le serveur est 1400 et ceux pour le client, l'IP de ma machine et 1401.
Sauriez-vous d'où vient le problème ?
Merci pour votre aide![]()
Partager