-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPClient.java
47 lines (40 loc) · 1.25 KB
/
TCPClient.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
43
44
45
46
47
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.Socket;
import java.net.InetSocketAddress;
import java.util.Arrays;
public class TCPClient {
public static void print(Boolean flag, Object... s) {
for (Object o : s) {
System.out.print(o);
}
if (flag)
System.out.println();
else
System.out.print("");
}
public static void main(String[] args) {
Socket client;
InetAddress ia;
InetSocketAddress isa;
client = new Socket();
try {
ia = InetAddress.getLocalHost();
isa = new InetSocketAddress(ia, Integer.parseInt(args[0]));
client.connect(isa);
// client.bind(isa);
print(true, "Porta allocata: " + client.getLocalPort());
print(true, "Indirizzo: " + client.getInetAddress()
+ "; porta: " + client.getPort());
Thread.sleep(10 * 1_000);
client.close();
} catch (UnknownHostException u) {
u.printStackTrace();
} catch (IOException i) {
i.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}