-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConnection.java
More file actions
49 lines (44 loc) · 1.32 KB
/
ServerConnection.java
File metadata and controls
49 lines (44 loc) · 1.32 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
48
49
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ServerConnection extends Thread{
DataInputStream in;
DataOutputStream out;
Socket clientSocket;
public ServerConnection(Socket ClientSocket){
try{
clientSocket = ClientSocket;
in = new DataInputStream(clientSocket.getInputStream());
out = new DataOutputStream(clientSocket.getOutputStream());
HandleConnection.addSocket(this);
}catch(IOException e){
System.out.println(e.getMessage());
}
}
public void sendResponse(String msg){
try{
out.writeUTF(msg);
}catch(Exception e){
System.out.println("Não foi possivel enviar a mensagem");
}
}
public Boolean isOpen(){
return !clientSocket.isClosed();
}
@Override
public void run(){
try {
String mensagem = in.readUTF();
HandleConnection.sendToAll(mensagem,this);
} catch(Exception e){
System.out.println("Ocorreu um erro");
}finally{
try{
clientSocket.close();
}catch(Exception e){
System.out.println("Não foi possivel fechar a conexão");
}
}
}
}