-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServer.java
More file actions
40 lines (37 loc) · 1.12 KB
/
Copy pathServer.java
File metadata and controls
40 lines (37 loc) · 1.12 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;
public class Server{
private ServerSocket serverSocket;
public Server(ServerSocket serverSocket){
this.serverSocket=serverSocket;
}
public void startServer(){ //to make connection to client via socket
try{
while(!serverSocket.isClosed()){
Socket socket= serverSocket.accept();
System.out.println("a client is connected");
ClientHandler clientHandler= new ClientHandler(socket); //handling client
Thread thread=new Thread(clientHandler); //running client on multiple thread
thread.start();//starting the thread
}
}catch (IOException e){
e.printStackTrace();
}
}
public void closeServerSocket(){
try{
if(serverSocket!=null){
serverSocket.close();//to close the connection
}
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {//using console
ServerSocket serverSocket=new ServerSocket(8080);
Server server=new Server(serverSocket);
server.startServer();//to start the server when compiled
}
}