-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMultithreadedChatServerTCP.java
26 lines (23 loc) · 1.04 KB
/
MultithreadedChatServerTCP.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
import java.net.*;
import java.io.*;
public class MultithreadedChatServerTCP {
private static final int PORT = 1234;
public static void main(String args[]) throws IOException {
ServerSocket connectionSocket = new ServerSocket(PORT);
// we wait 2 clients to connect
//then we create and start 2 threads,each for each client
while (true) {
System.out.println("Server is waiting first client in port: " + PORT);
Socket dataSocket1 = connectionSocket.accept();
System.out.println("Received request from " + dataSocket1.getInetAddress());
System.out.println("Server is waiting second client in port: " + PORT);
Socket dataSocket2 = connectionSocket.accept();
System.out.println("Server is waiting second client in port: " + PORT);
System.out.println("Received request from " + dataSocket2.getInetAddress());
ServerThread sthread1 = new ServerThread(dataSocket1, dataSocket2);
sthread1.start();
ServerThread sthread2 = new ServerThread(dataSocket2, dataSocket1);
sthread2.start();
}
}
}