MyServerSocket.java: Difference between revisions
Appearance
wrong content type |
|||
Line 1: | Line 1: | ||
<PRE> | |||
import java.net.*; | |||
import java.io.*; | |||
public class MyServerSocket extends Thread { | |||
private Socket sock; | |||
private Server owner; | |||
private long lastTime; | |||
// constructor | |||
public MyServerSocket(Socket s, Server o) { | |||
sock = s; | |||
owner = o; | |||
lastTime = System.currentTimeMillis(); | |||
} | |||
// run that is started by the start call to JVM | |||
public void run() { | |||
sendMessage("Welcome to this server." | |||
+ " Log off with 'logout'."); | |||
sendMessage("Get the list of connected users" | |||
+ " with command 'wwhhoo'"); | |||
try { | |||
BufferedReader bis = new BufferedReader( | |||
new InputStreamReader( | |||
sock.getInputStream())); | |||
String line; | |||
while ((line = bis.readLine()) != null) { | |||
lastTime = System.currentTimeMillis(); | |||
if (line.toLowerCase().trim(). | |||
equals("logout")) { | |||
break; // out from the while loop | |||
} else if (line.length() > 0) { | |||
// skip empty lines | |||
owner.handleMessage(line, this); | |||
} | |||
} | |||
sock.close(); | |||
} catch (Exception e) { } | |||
owner.loggedOf(this); | |||
} | |||
public String getIP() { | |||
return (sock.getInetAddress().getHostAddress() | |||
+ " at " | |||
+ sock.getInetAddress().getHostName()); | |||
} | |||
public void sendMessage(String txt) { | |||
try { | |||
PrintWriter pws = new PrintWriter( | |||
sock.getOutputStream(), | |||
true); | |||
pws.println(txt); | |||
} catch (IOException e) { } | |||
} | |||
public boolean isInactive(long time) { | |||
if (lastTime < time) { | |||
/* will cause an automatic logout in this | |||
* object's run method */ | |||
try { | |||
sock.close(); | |||
} catch (IOException e) {} | |||
return true; | |||
} else { | |||
return false; | |||
} | |||
} | |||
} | |||
</PRE> | |||
[[Category:Into Java]] |
Latest revision as of 19:15, 13 June 2020
import java.net.*; import java.io.*; public class MyServerSocket extends Thread { private Socket sock; private Server owner; private long lastTime; // constructor public MyServerSocket(Socket s, Server o) { sock = s; owner = o; lastTime = System.currentTimeMillis(); } // run that is started by the start call to JVM public void run() { sendMessage("Welcome to this server." + " Log off with 'logout'."); sendMessage("Get the list of connected users" + " with command 'wwhhoo'"); try { BufferedReader bis = new BufferedReader( new InputStreamReader( sock.getInputStream())); String line; while ((line = bis.readLine()) != null) { lastTime = System.currentTimeMillis(); if (line.toLowerCase().trim(). equals("logout")) { break; // out from the while loop } else if (line.length() > 0) { // skip empty lines owner.handleMessage(line, this); } } sock.close(); } catch (Exception e) { } owner.loggedOf(this); } public String getIP() { return (sock.getInetAddress().getHostAddress() + " at " + sock.getInetAddress().getHostName()); } public void sendMessage(String txt) { try { PrintWriter pws = new PrintWriter( sock.getOutputStream(), true); pws.println(txt); } catch (IOException e) { } } public boolean isInactive(long time) { if (lastTime < time) { /* will cause an automatic logout in this * object's run method */ try { sock.close(); } catch (IOException e) {} return true; } else { return false; } } }