点对点通讯:基于即连即用的双向多次Socket信息传输程序

来源:互联网 发布:网络电视机顶盒的安装 编辑:程序博客网 时间:2024/05/01 05:26

import java.io.InputStream;
import java.io.OutputStream;

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.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);
        TextField tfRemote = new TextField("远程端口:", "", 20, TextField.ANY);
        Command chooseCommand = new Command("确定", Command.OK, 1);

        Form form = new Form("");
        TextField tf = new TextField("输入:", "", 20, TextField.ANY);
        Command command = new Command("发送", Command.OK, 1);
        RecieveInfo ri = new RecieveInfo(this);

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

        public void startApp() {
                chooseForm.append(tfLocal);
                chooseForm.append(tfRemote);
                chooseForm.addCommand(chooseCommand);
                chooseForm.setCommandListener(this);
                display.setCurrent(chooseForm);
                form.append(tf);
                form.addCommand(command);
                form.setCommandListener(this);

        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
                ri.destroy();
        }

        public void commandAction(Command com, Displayable arg1) {
                if (com == chooseCommand) {
                        form.setTitle("本地:" + tfLocal.getString() + " | 远程:"
                                        + tfRemote.getString());
                        display.setCurrent(form);
                        Thread thread = new Thread(ri);
                        thread.start();
                } else if (com == command) {
                        SendInfo si = new SendInfo(this);
                        Thread thread = new Thread(si);
                        thread.start();
                }
        }
}

class RecieveInfo implements Runnable {
        Exec exec;
        ServerSocketConnection scn = null;
        SocketConnection sc = null;
        InputStream is = null;

        public RecieveInfo(Exec exec) {
                this.exec = exec;
        }

        public void run() {
                try {
                        scn = (ServerSocketConnection) Connector.open("socket://:"
                                        + exec.tfLocal.getString());
                        while (true) {
                                try {
                                        sc = (SocketConnection) scn.acceptAndOpen();
                                        is = sc.openInputStream();
                                        (new Thread(new Runnable() {
                                                public void run() {
                                                        try {
                                                                exec.form.append("已经建立对客户端的连接/n");
                                                                int c = 0;

                                                                StringBuffer sb = new StringBuffer();
                                                                while (((c = is.read()) != '/n') && (c != -1)) {
                                                                        sb.append((char) c);
                                                                }
                                                                exec.form
                                                                                .append("客户端的信息为:"
                                                                                                + new String(
                                                                                                                sb.toString().getBytes(
                                                                                                                                "ISO-8859-1"),
                                                                                                                "GB2312") + "/n");
                                                        } 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());
                }
        }

        public void destroy() {
                try {
                        is.close();
                        sc.close();
                        scn.close();
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
        }
}

class SendInfo implements Runnable {
        Exec exec;
        SocketConnection sc;
        OutputStream os;

        public SendInfo(Exec exec) {
                this.exec = exec;
        }

        public void run() {
                try {
                        sc = (SocketConnection) Connector.open("socket://localhost:"
                                        + exec.tfRemote.getString());
                        exec.form.append("已经建立对服务器端的连接/n");
                        os = sc.openOutputStream();
                        os.write(exec.tf.getString().getBytes());
                        os.write("/n".getBytes());
                        exec.form.append("已经发送/n");
                        os.close();
                        sc.close();
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
        }
}