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

📄 serversocketdemo.java

📁 人民邮电出版社的《J2ME手机开发入门》全部源代码
💻 JAVA
字号:
/*
 * ServerSocketDemo.java
 *
 * Created on 2005年5月5日, 下午2:39
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import javax.microedition.io.*;
import java.io.*;
/**
 *
 * @author  Liu Bin
 * @version
 */
public class ServerSocketDemo extends MIDlet
        implements  Runnable, CommandListener {
    private Display display = null;
    
    private Form form;
    private TextField tfData;
    private StringItem siMsg;
    
    //定义使用的命令按钮
    private Command cmdExit;
    private Command cmdSend;
    private Command cmdConnect;
    private Command cmdDisConnect;
    
    private boolean stopFlag;
    InputStream is;
    OutputStream os;
    ServerSocketConnection scn;
    Sender sender;
    
    public ServerSocketDemo() {
        cmdExit = new Command("退出", Command.EXIT, 0);
        cmdSend = new Command("发送", Command.SCREEN, 1);
        cmdConnect = new Command("监听", Command.SCREEN, 2);
        cmdDisConnect = new Command("断开", Command.SCREEN, 2);
        tfData = new TextField("请输入发送的消息",
                "",255,TextField.ANY);
        siMsg = new StringItem("当前状态:", "空闲");
        
        form = new Form("Socket服务器端演示");
        form.append(siMsg);
        form.append(tfData);
        form.addCommand(cmdExit);
        form.addCommand(cmdConnect);
        form.setCommandListener(this);
        
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        stop();
    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command cmd, Displayable d) {
        if (cmd == cmdExit) {
            notifyDestroyed();
        }
        
        if (cmd == cmdSend) {
            if (sender != null) {
                String str = tfData.getString();
                sender.send(str);
                form.append("[S}  " + str + "\n");
            }
        }
        
        if (cmd == cmdConnect) {
            form.removeCommand(cmdConnect);
            form.addCommand(cmdSend);
            form.addCommand(cmdDisConnect);
            start();
        }
        
        if (cmd == cmdDisConnect) {
            form.removeCommand(cmdDisConnect);
            form.removeCommand(cmdSend);
            form.addCommand(cmdConnect);
            stopFlag = true;
        }
        
        if (cmd == Alert.DISMISS_COMMAND) {
            display.setCurrent(form);
        }
    }
    public void start() {
        stopFlag = false;
        Thread t = new Thread(this);
        t.start();
    }
    
    public void stop() {
        try {
            stopFlag = true;
            
            if (is != null) {
                is.close();
                is = null;
            }
            
            if (os != null) {
                os.close();
                os = null;
            }
            
            if (scn != null) {
                scn.close();
                scn = null;
            }
            
            if (sender != null) {
                sender.stopFlag = true;
                sender = null;
            }
        } catch (IOException ioe) {
        }
    }
    
    public void run() {
        try {
            siMsg.setText("等待连接");
            scn = (ServerSocketConnection) Connector.open("socket://:9999");
            form.append("Local address: " + scn.getLocalAddress());
            form.append("Local Port: " + scn.getLocalPort());
            
            //等待一个连接
            SocketConnection sc = (SocketConnection)scn.acceptAndOpen();
            siMsg.setText("连接已经接受");
            is = sc.openInputStream();
            os = sc.openOutputStream();
            
            sender = new Sender(os);
            
            while (!stopFlag) {
                StringBuffer sb = new StringBuffer();
                int c = 0;
                
                while (((c = is.read()) != '\n') && (c != -1)) {
                    sb.append((char) c);
                }
                
                if (c == -1) {
                    break;
                }
                
                //显示接收到的消息
                form.append("[R]  " + sb.toString() + "\n");
            }
            siMsg.setText("连接已经关闭");
        } catch (Exception e) {
            if (e.getMessage().equals("ServerSocket Open")) {
                Alert a = new Alert("服务器端", "Port 9999已经被占用",
                        null, AlertType.ERROR);
                a.setTimeout(Alert.FOREVER);
                a.setCommandListener(this);
                display.setCurrent(a);
            } else {
                if (!stopFlag) {
                    e.printStackTrace();
                }
            }
        } finally {
            stop();
        }
    }
}

⌨️ 快捷键说明

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