-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUDPClient.java
More file actions
141 lines (122 loc) · 4.28 KB
/
UDPClient.java
File metadata and controls
141 lines (122 loc) · 4.28 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* UDPClient.java -- Simple UDP client
*
* $Id: UDPClient.java,v 1.2 2003/10/14 14:25:30 kangasha Exp $
*
*/
import java.net.*;
import java.util.*;
public class UDPClient extends UDPPinger implements Runnable {
/** Host to ping */
String remoteHost;
/** Port number of remote host */
int remotePort;
/** How many pings to send */
static final int NUM_PINGS = 10;
/** How many reply pings have we received */
int numReplies = 0;
/** Array for holding replies and RTTs */
static boolean[] replies = new boolean[NUM_PINGS];
static long[] rtt = new long[NUM_PINGS];
/*
* Send our own pings at least once per second. If no replies received
* within 5 seconds, assume ping was lost.
*/
/** 1 second timeout for waiting replies */
static final int TIMEOUT = 1000;
/** 5 second timeout for collecting pings at the end */
static final int REPLY_TIMEOUT = 5000;
/** Create UDPClient object. */
public UDPClient(String host, int port) {
remoteHost = host;
remotePort = port;
}
/**
* Main Function.
*/
public static void main(String args[]) {
String host = "127.0.0.1";
int port = 8000;
System.out.println("Contacting host " + host + " at port " + port);
UDPClient client = new UDPClient(host, port);
client.run();
}
/** Main code for pinger client thread. */
public void run() {
/* Create socket. We do not care which local port we use. */
createSocket();
try {
socket.setSoTimeout(TIMEOUT);
} catch (SocketException e) {
System.out.println("Error setting timeout TIMEOUT: " + e);
}
for (int i = 0; i < NUM_PINGS; i++) {
/*
* Message we want to send. Add space at the end for easy parsing of
* replies.
*/
Date now = new Date();
String message = "PING " + i + " " + now.getTime() + " ";
replies[i] = false;
rtt[i] = 1000000;
PingMessage ping = null;
/* Send ping to recipient */
try {
ping = new PingMessage(InetAddress.getByName(remoteHost),
remotePort, message);
} catch (UnknownHostException e) {
System.out.println("Cannot find host: " + e);
}
sendPing(ping);
/* Read reply */
try {
PingMessage reply = receivePing();
handleReply(reply.getContents());
} catch (SocketTimeoutException e) {
/*
* Reply did not arrive. Do nothing for now. Figure out lost
* pings later.
*/
}
}
/*
* We sent all our pings. Now check if there are still missing replies.
* Wait for a reply, if nothing comes, then assume it was lost. If a
* reply arrives, keep looking until nothing comes within a reasonable
* timeout.
*/
try {
socket.setSoTimeout(REPLY_TIMEOUT);
} catch (SocketException e) {
System.out.println("Error setting timeout REPLY_TIMEOUT: " + e);
}
while (numReplies < NUM_PINGS) {
try {
PingMessage reply = receivePing();
handleReply(reply.getContents());
} catch (SocketTimeoutException e) {
/* Nothing coming our way apparently. Exit loop. */
numReplies = NUM_PINGS;
}
}
/* Print statistics */
for (int i = 0; i < NUM_PINGS; i++) {
System.out.println("PING " + i + ": " + replies[i] + " RTT: "
+ ((rtt[i] > 0) ? Long.toString(rtt[i]):"< 1")+ " ms");
}
}
/**
* Handle the incoming ping message. For now, just count it as having been
* correctly received.
*/
private void handleReply(String reply) {
String[] tmp = reply.split(" ");
int pingNumber = Integer.parseInt(tmp[1]);
long then = Long.parseLong(tmp[2]);
replies[pingNumber] = true;
/* Calculate RTT and store it in the rtt-array. */
Date now = new Date();
rtt[pingNumber] = now.getTime() - then;
numReplies++;
}
}