课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——多点PtoP通信


  共有13171人关注过本帖树形打印复制链接

主题:程序代码——多点PtoP通信

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——多点PtoP通信  发帖心情 Post By:2008/11/8 22:49:44 [只看该作者]

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

//显示指定输入本地端口的窗体,一旦输入,建立该端口的监听
public class Exec extends MIDlet implements CommandListener {
        Display display;
        Form chooseForm = new Form("指定端口");
        TextField tfLocal = new TextField("本地端口:", "", 20, TextField.ANY);
        Command chooseCommand = new Command("确定", Command.OK, 1);

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                chooseForm.append(tfLocal);
                chooseForm.addCommand(chooseCommand);
                chooseForm.setCommandListener(this);
                display.setCurrent(chooseForm);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        // 获取本地端口号
        public String getLocalPort() {
                return tfLocal.getString();
        }

        public void commandAction(Command com, Displayable arg1) {
                // 创建列表并显示,同时启动监听线程来监听其他用户的连接请求
                if (com == chooseCommand) {
                        PersonList pl = new PersonList(this);
                        display.setCurrent(pl);
                        RecieveInfo ri = new RecieveInfo(tfLocal.getString(), pl);
                        Thread thread = new Thread(ri);
                        thread.start();
                }
        }

        public void setCurrent(Displayable disp) {
                display.setCurrent(disp);
        }
}

// 显示所有通信者的列表,每一个项目的值都是对方的端口
class PersonList extends List implements CommandListener {
        Command communicate = new Command("通讯", Command.OK, 1);
        Command newConnect = new Command("新建连接", Command.BACK, 1);
        Exec exec;
        Hashtable allForms = new Hashtable();
        String localPort;

        public PersonList(Exec exec) {
                super("本地:" + exec.getLocalPort(), List.IMPLICIT);
                localPort = exec.getLocalPort();
                this.exec = exec;
                this.addCommand(communicate);
                this.addCommand(newConnect);
                this.setCommandListener(this);
        }

        public void commandAction(Command com, Displayable arg1) {
                // 新建对其他用户的连接
                if (com == newConnect) {
                        ConnectForm cf = new ConnectForm(this);
                        exec.setCurrent(cf);
                }
                // 对现有用户进行通信
                else if (com == communicate) {
                        String currentPort = this.getString(this.getSelectedIndex());
                        CommunicateForm cf = (CommunicateForm) (allForms.get(currentPort));
                        exec.setCurrent(cf);
                }
        }

        // 恢复列表的显示
        public void displayList() {
                exec.setCurrent(this);
        }

        // 添加新的通信人窗体
        public void addForm(String title) {
                CommunicateForm cf = (CommunicateForm) (allForms.get(title));
                if (cf == null) {
                        this.append(title, null);
                        CommunicateForm newForm = new CommunicateForm(title, this);
                        newForm.append("已经连接\n");
                        allForms.put(title, newForm);
                }
        }

        // 在已有通信人窗体加入新的内容
        public void setForm(String key, String value) {
                CommunicateForm form = (CommunicateForm) allForms.get(key);
                form.append(value);
        }

        public String getLocalPort() {
                return localPort;
        }
}

// 监听线程
class RecieveInfo implements Runnable {
        String localPort;
        PersonList pl;
        ServerSocketConnection scn = null;
        SocketConnection sc = null;
        InputStream is = null;

        public RecieveInfo(String localPort, PersonList pl) {
                this.localPort = localPort;
                this.pl = pl;
        }

        // 启动监听线程,每次请求建立一次连接,接受完信息后即结束该连接
        public void run() {
                try {
                        scn = (ServerSocketConnection) Connector.open("socket://:"
                                        + localPort);
                        while (true) {
                                try {
                                        sc = (SocketConnection) scn.acceptAndOpen();
                                        is = sc.openInputStream();
                                        (new Thread(new Runnable() {
                                                public void run() {
                                                        try {
                                                                int c = 0;
                                                                StringBuffer sb = new StringBuffer();
                                                                while (((c = is.read()) != '\n') && (c != -1)) {
                                                                        sb.append((char) c);
                                                                }
                                                                String remotePort = sb.toString();
                                                                sb = new StringBuffer();
                                                                pl.addForm(remotePort);
                                                                while (((c = is.read()) != '\n') && (c != -1)) {
                                                                        sb.append((char) c);
                                                                }
                                                                if (!sb.toString().equals("")
                                                                                && sb.toString() != null)
                                                                        pl.setForm(remotePort, "对方:"
                                                                                        + new String(sb.toString()
                                                                                                        .getBytes("ISO-8859-1"),
                                                                                                        "GB2312") + "\n");

                                                        } catch (Exception e) {
                                                                System.out.println(e.getMessage());
                                                        } finally {
                                                                try {
                                                                        is.close();
                                                                        sc.close();
                                                                } catch (Exception e) {
                                                                        System.out.println(e.getMessage());
                                                                }
                                                        }
                                                }
                                        })).start();
                                } catch (Exception e) {
                                        System.out.println(e.getMessage());
                                }
                        }
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                } finally {
                        try {
                                scn.close();
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                }
        }
}

// 负责通讯的表单
class CommunicateForm extends Form implements CommandListener {
        PersonList pl;
        TextField content = new TextField("输入:", "", 20, TextField.ANY);
        Command chooseCommand = new Command("确定", Command.OK, 1);
        Command exitCommand = new Command("退出", Command.BACK, 1);

        public CommunicateForm(String title, PersonList pl) {
                super(title);
                this.pl = pl;
                this.append(content);
                this.addCommand(chooseCommand);
                this.addCommand(exitCommand);
                this.setCommandListener(this);
        }

        public void commandAction(Command com, Displayable arg1) {
                if (com == exitCommand) {
                        pl.displayList();
                } else if (com == chooseCommand) {
                        pl.setForm(this.getTitle(), "自己:" + content.getString() + "\n");
                        SendInfo si = new SendInfo(pl, this.getTitle(), content.getString());
                        Thread thread = new Thread(si);
                        thread.start();
                }
        }
}

// 负责连接的表单
class ConnectForm extends Form implements CommandListener {
        PersonList pl;
        TextField remotePort = new TextField("远程端口:", "", 20, TextField.ANY);
        Command chooseCommand = new Command("确定", Command.OK, 1);
        Command exitCommand = new Command("退出", Command.BACK, 1);

        public ConnectForm(PersonList pl) {
                super("");
                this.pl = pl;
                this.append(remotePort);
                this.addCommand(chooseCommand);
                this.addCommand(exitCommand);
                this.setCommandListener(this);
        }

        public void commandAction(Command com, Displayable arg1) {
                if (com == exitCommand) {
                        pl.displayList();
                } else if (com == chooseCommand) {
                        pl.addForm(remotePort.getString());
                        pl.displayList();

                        // 将当前监听端口发送过去,便于多方回传信息
                        SendInfo si = new SendInfo(pl, remotePort.getString(), "\n");
                        Thread thread = new Thread(si);
                        thread.start();
                }
        }
}

// 发送信息的线程
class SendInfo implements Runnable {
        PersonList pl;
        String remotePort;
        String value;
        SocketConnection sc;
        OutputStream os;

        public SendInfo(PersonList pl, String remotePort, String value) {
                this.pl = pl;
                this.remotePort = remotePort;
                this.value = value;
        }

        public void run() {
                try {
                        sc = (SocketConnection) Connector.open("socket://localhost:"
                                        + remotePort);
                        os = sc.openOutputStream();
                        os.write(pl.getLocalPort().getBytes());
                        os.write("\n".getBytes());
                        os.write(value.getBytes());
                        os.write("\n".getBytes());
                        os.close();
                        sc.close();
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
        }
}

[此贴子已经被作者于2010-12-12 18:45:55编辑过]

 回到顶部