-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
43 lines (39 loc) · 1.41 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
package MultiThreaded;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
public Runnable getRunnable(){
return new Runnable() {
@Override
public void run() {
int port = 8010;
try {
InetAddress address = InetAddress.getByName("localhost");
Socket socket = new Socket(address, port);
PrintWriter toSocket = new PrintWriter(socket.getOutputStream());
BufferedReader fromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
toSocket.println("Hello from the client");
String line = fromSocket.readLine();
System.out.println("Response from the socket is " + line);
}catch (IOException ex){
ex.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;
}
}
}
}