Jump to content

MyServerSocket.java: Difference between revisions

From EDM2
Created page with "Back to Into Java - Part XXIII <PRE> </PRE> Category:Languages Articles"
 
Undo revision 74153 by Ak120 (talk)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Back to [[Into Java - Part XXIII]]
<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();
    }


<PRE>
    // 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>
</PRE>
 
[[Category:Into Java]]
[[Category:Languages Articles]]

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;
        }
    }
}