-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatServerThread.java
64 lines (54 loc) · 1.33 KB
/
ChatServerThread.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
55
56
57
58
59
60
61
62
63
64
import java.net.*;
import java.io.*;
public class ChatServerThread extends Thread{
private ChatServer server = null;
private Socket socket = null;
private int ID = -1;
private DataInputStream streamIn = null;
private DataOutputStream streamOut = null;
public ChatServerThread(ChatServer _server, Socket _socket){
super();
server = _server;
socket = _socket;
ID = socket.getPort();
}
public void send(String msg){
try{
streamOut.writeUTF(msg);
streamOut.flush();
}
catch(IOException ioe){
System.out.println(ID + " ERROR sending: " + ioe.getMessage());
server.remove(ID);
stop();
}
}
public int getID(){
return ID;
}
public void run(){
System.out.println("Hebra del server " + ID + " ejecutandose.");
do{
try{
server.handle(ID, streamIn.readUTF());
}
catch(IOException ioe){
System.out.print(ID + "Error lectura: " + ioe.getMessage());
server.remove(ID);
stop();
}
}while(true);
}
public void open() throws IOException{
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
public void close() throws IOException{
if(socket != null)
socket.close();
if(streamIn != null)
streamIn.close();
if(streamOut != null)
streamOut.close();
}
}