⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 talkserverthread.java

📁 北大Java 语言程序设计 ppt课件及源码
💻 JAVA
字号:
/* * 1.0 code. */import java.io.*;import java.net.*;import java.util.*;class TalkServerThread extends Thread {    public Socket socket;    public DataInputStream is;    public DataOutputStream os;    TalkServer server;    boolean DEBUG = false;    public String toString() {        return "TalkServerThread: socket = " + socket               + "; is = " + is               + "; os = " + os;    }    TalkServerThread(Socket socket, TalkServer server) throws IOException {        super("TalkServer");        is = new DataInputStream(socket.getInputStream());        os = new DataOutputStream(socket.getOutputStream());        if (is == null) {            System.err.println("TalkServerThread: Input stream seemed "                               + "to be created successfully, but it's null.");            throw new IOException();        }        if (os == null) {            System.err.println("TalkServerThread: Output stream seemed "                               + "to be created successfully, but it's null.");            throw new IOException();        }        this.socket = socket;        this.server = server;    }    public void run() {        while (socket != null) {            try {                //Read data.                String str = is.readUTF();                //Pass it on.                if (str != null) {                    server.forwardString(str, this);                }            } catch (EOFException e) { //No more data on this socket...                server.forwardString("SERVER SAYS other applet disconnected",                                     this);                cleanup();                return;            } catch (NullPointerException e) { //Socket doesn't exist...                server.forwardString("SERVER SAYS no socket to other applet",                                     this);                cleanup();                return;            } catch (IOException e) { //Read problem..                server.forwardString("SERVER SAYS socket trouble with other applet",                                     this);                cleanup();                return;            } catch (Exception e) { //Unknown exception. Complain and quit.                System.err.println("Exception on is.readUTF():");                e.printStackTrace();                cleanup();                return;            }        }    }    protected void finalize() {        cleanup();    }    void cleanup() {        try {            if (is != null) {                is.close();                is = null;            }        } catch (Exception e) {} //Ignore errors.        try {            if (os != null) {                os.close();                os = null;            }        } catch (Exception e) {} //Ignore errors.        try {            if (socket != null) {                socket.close();                socket = null;            }        } catch (Exception e) {} //Ignore errors.    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -