-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
41 lines (35 loc) · 1.1 KB
/
Server.java
File metadata and controls
41 lines (35 loc) · 1.1 KB
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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
ServerSocket serverSocket;
public Server(int port){
try {
this.serverSocket = new ServerSocket(port);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void startServer(){
try{
while(!this.serverSocket.isClosed()){
Socket socket = this.serverSocket.accept();
System.out.println("Someone Connected");
ClientHandler clientHandler = new ClientHandler(socket);
Thread thread = new Thread(clientHandler);
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Port");
String por = scanner.nextLine();
int port = Integer.parseInt(por);
Server server = new Server(port);
server.startServer();
}
}