-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
47 lines (43 loc) · 1.5 KB
/
Server.java
File metadata and controls
47 lines (43 loc) · 1.5 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
41
42
43
44
45
46
47
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
private ServerSocket serverSocket;
private Socket socket;
private DataInputStream input;
private DataOutputStream output;
public void start(int port) throws IOException {
ExecutorService threadPool = Executors.newFixedThreadPool(2);
System.out.println("[S1] Criando server socket para aguardar conexões de clientes em loop");
serverSocket = new ServerSocket(port);
while (serverSocket.isBound()) {
System.out.println("[S2] Aguardando conexão em: " + serverSocket.getLocalSocketAddress());
socket = serverSocket.accept();
ServerConnection serverConnection = new ServerConnection(socket);
threadPool.submit(serverConnection);
}
serverSocket.close();
}
public void stop() throws IOException {
input.close();
output.close();
socket.close();
serverSocket.close();
}
public static void main(String[] args) {
int serverPort = 6666;
try {
// Inicia e roda servidor
Server server = new Server();
server.start(serverPort);
// Finaliza servidor
server.stop();
} catch (IOException e) {
e.printStackTrace();
}
}
}