質問編集履歴

2

codeの変更をしました

2019/11/11 01:09

投稿

TNKa
TNKa

スコア5

test CHANGED
File without changes
test CHANGED
@@ -6,13 +6,11 @@
6
6
 
7
7
 
8
8
 
9
- public class UDPEchoClientTimeout {
9
+ public class UDPEchoServer {
10
10
 
11
11
 
12
12
 
13
- private static final int TIMEOUT = 3000; // Resend timeout (milliseconds)
14
-
15
- private static final int MAXTRIES = 5; // Maximum retransmissions
13
+ private static final int ECHOMAX = 255; // Maximum size of echo datagram
16
14
 
17
15
 
18
16
 
@@ -20,93 +18,47 @@
20
18
 
21
19
 
22
20
 
23
- if ((args.length < 2) || (args.length > 3)) // Test for correct # of args
21
+ if (args.length != 1) // Test for correct argument list
24
22
 
25
- throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]");
23
+ throw new IllegalArgumentException("Parameter(s): <Port>");
26
24
 
27
25
 
28
26
 
29
- System.out.println("UDPでDatagramをります");
27
+ System.out.println("Datagramを UTP で受け取ります");
28
+
29
+ System.out.println("UTP: Unshielded Twist Pair ケーブル");
30
30
 
31
31
 
32
32
 
33
- InetAddress serverAddress = InetAddress.getByName(args[0]); // Server address
34
-
35
- // Convert input String to bytes using the default character encoding
36
-
37
- byte[] bytesToSend = args[1].getBytes();
38
-
39
- System.out.println("Sending : " + args[1]); // ++appended for test running
40
-
41
- int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7;
33
+ int servPort = Integer.parseInt(args[0]);
42
34
 
43
35
 
44
36
 
45
- DatagramSocket socket = new DatagramSocket();
37
+ DatagramSocket socket = new DatagramSocket(servPort);
38
+
39
+ DatagramPacket packet = new DatagramPacket(new byte[ECHOMAX], ECHOMAX);
46
40
 
47
41
 
48
42
 
49
- socket.setSoTimeout(TIMEOUT); // Maximum receive blocking time (milliseconds)
43
+ for (;;) { // Run forever, receiving and echoing datagrams
50
44
 
51
- // Sending packet
45
+ socket.receive(packet); // Receive packet from client
52
46
 
53
- DatagramPacket sendPacket = new DatagramPacket(bytesToSend,
47
+ System.out.println("Handling client at " +
54
48
 
55
- bytesToSend.length, serverAddress, servPort);
49
+ packet.getAddress().getHostAddress() + " on port " + packet.getPort());
56
50
 
57
- // Receiving packet
51
+ System.out.println("get data : " +
58
52
 
59
- DatagramPacket receivePacket =
53
+ new String(packet.getData()));
60
54
 
61
- new DatagramPacket(new byte[bytesToSend.length], bytesToSend.length);
55
+ socket.send(packet); // Send back the same message to client
62
56
 
57
+ packet.setLength(ECHOMAX); // Reset length to avoid shrinking buffer
63
58
 
59
+ }
64
60
 
65
- int tries = 0; // Packets may be lost, so we have to keep trying
66
-
67
- boolean receivedResponse = false;
61
+ /* NOT REACHED */
68
-
69
- do {
70
-
71
- socket.send(sendPacket); // Send the echo string
72
-
73
- try {
74
-
75
- socket.receive(receivePacket); // Attempt echo reply reception
76
-
77
-
78
-
79
- if (!receivePacket.getAddress().equals(serverAddress)) // Check source
80
-
81
- throw new IOException("Received packet from an unknown source");
82
-
83
-
84
-
85
- receivedResponse = true;
86
-
87
- } catch (InterruptedIOException e) { // We did not get anything
88
-
89
- tries += 1;
90
-
91
- System.out.println("Timed out, " + (MAXTRIES-tries) + " more tries...");
92
-
93
- }
94
-
95
- } while ((!receivedResponse) && (tries < MAXTRIES));
96
-
97
-
98
-
99
- if (receivedResponse)
100
-
101
- System.out.println("Received: " + new String(receivePacket.getData()));
102
-
103
- else
104
-
105
- System.out.println("No response -- giving up.");
106
-
107
-
108
-
109
- socket.close();
110
62
 
111
63
  }
112
64
 

1

違ったコードを挿入していたので新しくしました。

2019/11/11 01:09

投稿

TNKa
TNKa

スコア5

test CHANGED
File without changes
test CHANGED
@@ -114,6 +114,8 @@
114
114
 
115
115
 
116
116
 
117
+
118
+
117
119
  ```
118
120
 
119
121
  ### 実現したいこと