2014-05-04 18:55:37 +00:00
|
|
|
import java.io.*;
|
|
|
|
import java.net.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Nicolás A. Ortega
|
|
|
|
* @license GNU GPLv3
|
|
|
|
* @year 2014
|
|
|
|
*
|
|
|
|
* For details on the copyright, look at the COPYRIGHT file that came with
|
|
|
|
* this program.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class ClientThread extends Thread {
|
|
|
|
private Socket socket = null;
|
|
|
|
private Client client = null;
|
|
|
|
private DataInputStream streamIn = null;
|
|
|
|
private boolean run = false;
|
|
|
|
|
2014-05-07 15:34:45 +00:00
|
|
|
// Constructor
|
2014-05-04 18:55:37 +00:00
|
|
|
public ClientThread(Client _client, Socket _socket) {
|
|
|
|
client = _client;
|
|
|
|
socket = _socket;
|
|
|
|
open();
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:34:45 +00:00
|
|
|
// Open all necessary streams/threads
|
2014-05-04 18:55:37 +00:00
|
|
|
public void open() {
|
|
|
|
try {
|
|
|
|
streamIn = new DataInputStream(socket.getInputStream());
|
|
|
|
} catch(IOException e) {
|
|
|
|
System.out.println("Error getting input stream: " + e);
|
|
|
|
client.stop();
|
|
|
|
}
|
|
|
|
run = true;
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:34:45 +00:00
|
|
|
// Close the streams
|
2014-05-04 18:55:37 +00:00
|
|
|
public void close() throws IOException {
|
|
|
|
if(streamIn != null) { streamIn.close(); }
|
|
|
|
run = false;
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:34:45 +00:00
|
|
|
// The run method which will be called every frame
|
2014-05-04 18:55:37 +00:00
|
|
|
public void run() {
|
|
|
|
while(run) {
|
|
|
|
try {
|
|
|
|
client.handle(streamIn.readUTF());
|
|
|
|
} catch(IOException e) {
|
|
|
|
System.out.println("Listening error: " + e.getMessage());
|
|
|
|
client.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|