Skip to content

Instantly share code, notes, and snippets.

Created May 27, 2014 21:45
Show Gist options
  • Save anonymous/38199bcd23352ee08cf0 to your computer and use it in GitHub Desktop.
Save anonymous/38199bcd23352ee08cf0 to your computer and use it in GitHub Desktop.
Client.java
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