Created
May 27, 2014 21:45
-
-
Save anonymous/38199bcd23352ee08cf0 to your computer and use it in GitHub Desktop.
Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.Socket; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
public class Client | |
{ | |
private static final int USER_THROTTLE = 200; | |
private Socket socket; | |
private boolean connected; | |
private Inport inport; | |
private class Inport extends Thread | |
{ | |
private ObjectInputStream in; | |
public void run() { | |
// Try to connect to the socket and obtain the input stream. | |
try { | |
in = new ObjectInputStream(socket.getInputStream()); | |
} catch(IOException e) { | |
System.out.println("Could not get input stream from "+toString()); | |
return; | |
} | |
System.out.println(socket+" has connected input."); | |
// Loop infinitely until interrupted. | |
while(true) { | |
try { | |
Thread.sleep(USER_THROTTLE); | |
} catch(Exception e) { | |
System.out.println(toString()+" has input interrupted."); | |
} | |
} | |
} | |
} | |
public Client(Socket newSocket) { | |
// Assign the new socket | |
socket = newSocket; | |
// Set the connection status as connected. | |
connected = true; | |
// Create a new Inport and start it. | |
inport = new Inport(); | |
inport.start(); | |
} | |
/** | |
* | |
* Determine connection status. | |
* | |
* @return true|false | |
*/ | |
public boolean isConnected() { | |
return connected; | |
} | |
/** | |
* | |
* Close the connection to the socket. | |
* | |
*/ | |
public void purge() { | |
try { | |
connected = false; | |
socket.close(); | |
} catch(IOException e) { | |
System.out.println("Could not purge "+socket+"."); | |
} | |
} | |
public String toString() { | |
return new String(socket.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment