-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
42 lines (38 loc) · 1.21 KB
/
Client.java
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
package Assignment3;
import java.net.BindException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Random;
public abstract class Client {
final int SERVER_PORT = 2711;
final String HOST_NAME = "localhost";
InetAddress address;
DatagramSocket socket;
double bid = 0;
int port;
public void findGoodPort() {
DatagramSocket testSocket;
boolean goodport = false;
Random rand = new Random();
port = rand.nextInt(65535) + 1;
while (!goodport) {
try {
testSocket = new DatagramSocket(port);
testSocket.close();
try {
testSocket = new DatagramSocket(port + 1);
goodport = true;
testSocket.close();
} catch (BindException e) {
System.out.println("badPort");
port = rand.nextInt(65535) + 1;
}
} catch (BindException e) {
System.out.println("badPort");
port = rand.nextInt(65535) + 1;
} catch (SocketException e) {}
}
}
public abstract void main();
}