Methods Summary |
---|
public void | connect()
if (server == null)
{
server = new ServerSocket(port);
}
|
public int | getPort()
return server == null ? port : server.getLocalPort();
|
public boolean | isRunning()
return running;
|
public static void | main(java.lang.String[] args)
TimeTestSimpleServer server = new TimeTestSimpleServer();
try
{
server.start();
} catch (IOException e)
{
}
|
public void | run()
Socket socket = null;
while (running)
{
try
{
socket = server.accept();
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
// add 500 ms to round off to nearest second
int time = (int) ((System.currentTimeMillis() + 500) / 1000 + SECONDS_1900_TO_1970);
os.writeInt(time);
os.flush();
} catch (IOException e)
{
} finally
{
if (socket != null)
try
{
socket.close(); // force closing of the socket
} catch (IOException e)
{
System.err.println("close socket error: " + e);
}
}
}
|
public void | start()Start time service and provide time to client connections.
if (server == null)
{
connect();
}
if (!running)
{
running = true;
new Thread(this).start();
}
|
public void | stop()Close server socket.
running = false;
if (server != null)
{
try
{
server.close(); // force closing of the socket
} catch (IOException e)
{
System.err.println("close socket error: " + e);
}
server = null;
}
|