-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
51 lines (46 loc) · 1.75 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
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public Runnable getRunnable() throws UnknownHostException, IOException {
return new Runnable() {
@Override
public void run() {
int port = 8010;
try {
InetAddress address = InetAddress.getByName("localhost");
Socket socket = new Socket(address, port);
try (
PrintWriter toSocket = new PrintWriter(socket.getOutputStream(), true);
BufferedReader fromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()))
) {
toSocket.println("Hello from Client " + socket.getLocalSocketAddress());
String line = fromSocket.readLine();
System.out.println("Response from Server " + line);
} catch (IOException e) {
e.printStackTrace();
}
// The socket will be closed automatically when leaving the try-with-resources block
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
public static void main(String[] args){
Client client = new Client();
for(int i=0; i<100; i++){
try{
Thread thread = new Thread(client.getRunnable());
thread.start();
}catch(Exception ex){
return;
}
}
return;
}
}