sender.java

来自「用JAVA编写的带服务器端和客户端的射击类游戏,」· Java 代码 · 共 58 行

JAVA
58
字号
import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import java.io.*;public class Sender extends Thread {    private OutputStream os;    private String message;    public Sender(OutputStream os) {        /*得到父类的输出流*/        this.os = os;        start();    }    public synchronized void send(String msg) {        /*得到父类的传输值*/        message = msg;        notify();    }    public synchronized void run() {        while(true) {            /*如果传输的数据没有被接收,则等待*/            if (message == null) {                try {                    wait();                } catch (InterruptedException e) {                }            }            if (message == null) {                break;            }/*利用输出流传出数据,如果传到了目的端,则结束传输*/            try {                os.write(message.getBytes());               os.write("\n".getBytes());               stop();             } catch (IOException ioe) {                ioe.printStackTrace();            }            /* 标记,以用来重新传输*/           message = null;        }    }/*结束传输*/    public synchronized void stop() {        message = null;        notify();    }}

⌨️ 快捷键说明

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