forked from Miltos717/SimpleChatApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultithreadedChatServerTCP.java
58 lines (43 loc) · 2.23 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
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
52
53
54
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("Received request from " + dataSocket2.getInetAddress());
ServerThread sthread1 = new ServerThread(dataSocket1, dataSocket2);
sthread1.start();
ServerThread sthread2 = new ServerThread(dataSocket2, dataSocket1);
sthread2.start();
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("Received request from " + dataSocket2.getInetAddress());
System.out.println("Received request from " + dataSocket3.getInetAddress());
System.out.println("Server is waiting second client in port: " + PORT);
Socket dataSocket2 = connectionSocket.accept();
System.out.println("Received request from " + dataSocket4.getInetAddress());
}
//remove this
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("Received request from " + dataSocket2.getInetAddress());
ServerThread sthread1 = new ServerThread(dataSocket1, dataSocket2);
sthread1.start();
ServerThread sthread2 = new ServerThread(dataSocket2, dataSocket1);
sthread2.start();
}
}
}