首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

手机和电脑用socket连接不正常,该怎么解决

2012-01-22 
手机和电脑用socket连接不正常我用手机的J2ME和电脑的J2SE分别建立了socket连接,想实现直接通讯,但是貌似

手机和电脑用socket连接不正常
我用手机的J2ME和电脑的J2SE分别建立了socket连接,想实现直接通讯,但是貌似电脑不能向手机发信息。代码和输出结果如下,谢谢各位了~~~

这是电脑上的程序:

Java code
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class App {    private static ServerSocket server;    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("正在创建服务器……");        try {            server = new ServerSocket(10086);        } catch (IOException e) {            // TODO Auto-generated catch block            System.out.println("创建服务器时出现异常(IOException)!");            return;        }        System.out.println("服务器已建立。");        new Waiter();    }    private static class Waiter implements Runnable {        Thread thread;        Socket socket;        InputStream reader;        OutputStream writer;        int info = 0;        public Waiter() {            thread = new Thread(this);            thread.start();        }        @Override        public void run() {            // TODO Auto-generated method stub            System.out.println("正在等待客户端连接……");            try {                socket = server.accept();            } catch (IOException e) {                // TODO Auto-generated catch block                System.out.println("等待客户端连接时出现异常(IOException)!");                return;            }            System.out.println("客户端已连接。");            System.out.println("正在获取I/O通道……");            try {                /*reader = new BufferedReader(new InputStreamReader(                        socket.getInputStream()));*/                reader = socket.getInputStream();                /*writer = new BufferedWriter(new OutputStreamWriter(                        socket.getOutputStream()));*/                writer = socket.getOutputStream();            } catch (IOException e) {                // TODO Auto-generated catch block                System.out.println("获取I/O通道时出现异常(IOException)!");                return;            }            System.out.println("成功获取I/O通道。");                        System.out.println("正在输出信息……");            try {                writer.write(9);            } catch (IOException e) {                // TODO Auto-generated catch block                System.out.println("输出信息时出现异常(IOException)!");                return;            }            System.out.println("成功输出信息:9。");                        System.out.println("正在等待输入信息……");            try {                while(reader.available()<1);            } catch (IOException e1) {                // TODO Auto-generated catch block                System.out.println("等待输入信息时出现异常(IOException)!");                return;            }                        System.out.println("有输入信息。正在读取……");            try {                info = reader.read();            } catch (IOException e) {                // TODO Auto-generated catch block                System.out.println("读取输入信息时出现异常(IOException)!");                return;            }            System.out.println("成功读取输入信息。读取到的数据是:" + Integer.toString(info));                        System.out.println("正在关闭socket……");            try {                socket.close();            } catch (IOException e) {                // TODO Auto-generated catch block                System.out.println("关闭socket时出现异常(IOException)!");                return;            }            System.out.println("socket已关闭。");                        System.out.println("正在关闭服务器……");            try {                server.close();            } catch (IOException e) {                // TODO Auto-generated catch block                System.out.println("关闭服务器时出现异常(IOException)!");                return;            }            System.out.println("服务器已关闭。");        }    }} 



这时手机上的程序:
Java code
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.microedition.io.Connector;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.TextBox;import javax.microedition.lcdui.TextField;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class midlet extends MIDlet implements CommandListener{    Display display;    TextBox tbox;    Command exitCommand;    SocketConnection socket;    InputStream reader;    OutputStream writer;    int info=0;    public midlet() {        // TODO Auto-generated constructor stub        super();        display = Display.getDisplay(this);        tbox = new TextBox("SocketConnection", "O(∩_∩)O", 100, TextField.UNEDITABLE);        exitCommand = new Command("退出", Command.EXIT, 1);        tbox.addCommand(exitCommand);        tbox.setCommandListener(this);        display.setCurrent(tbox);                new Linker();    }    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {        // TODO Auto-generated method stub    }    protected void pauseApp() {        // TODO Auto-generated method stub    }    protected void startApp() throws MIDletStateChangeException {        // TODO Auto-generated method stub    }    public void commandAction(Command c, Displayable d) {        // TODO Auto-generated method stub        if(c == exitCommand)        {            try {                destroyApp(true);            } catch (MIDletStateChangeException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            notifyDestroyed();        }    }    private class Linker    {        public Linker()        {            tbox.setString("正在建立Socket并连接服务器……");            try {                socket = (SocketConnection)Connector.open("socket://121.250.223.63:10086");            } catch (IOException e) {                // TODO Auto-generated catch block                tbox.setString("建立Socket并连接服务器时出现异常(IOException)");                return;            }                        tbox.setString("正在获取I/O通道……");            try {                reader = socket.openInputStream();                writer = socket.openOutputStream();            } catch (IOException e) {                // TODO Auto-generated catch block                tbox.setString("获取I/O通道时出现异常(IOException)");                return;            }                        tbox.setString("正在等待输入信息……");            try {                while(reader.available()<1);            } catch (IOException e) {                // TODO Auto-generated catch block                tbox.setString("等待输入信息时出现异常(IOException)");                return;            }                        tbox.setString("有输入信息。正在读取……");            try {                info = reader.read();            } catch (IOException e) {                // TODO Auto-generated catch block                tbox.setString("读取输入信息时出现异常(IOException)");                return;            }            tbox.setString("成功读取输入信息。读取到的数据是:" + Integer.toString(info));                        tbox.setString("正在输出信息……");            try {                writer.write(8);            } catch (IOException e) {                // TODO Auto-generated catch block                tbox.setString("输出信息时出现异常(IOException)");                return;            }                        System.out.println("正在关闭socket……");            try {                socket.close();            } catch (IOException e) {                // TODO Auto-generated catch block                System.out.println("关闭socket时出现异常(IOException)!");                return;            }            System.out.println("socket已关闭。");        }    }} 



电脑上输出:
正在创建服务器……
服务器已建立。
正在等待客户端连接……
客户端已连接。
正在获取I/O通道……
成功获取I/O通道。
正在输出信息……
输出信息时出现异常(IOException)!

手机上输出:
正在建立Socket并连接服务器……
(然后就没有了,这时候电脑上的程序早已遇异常关闭,手机上程序貌似超时并显示“建立Socket并连接服务器时出现异常(IOException)”)


再次谢谢各位了。感激不尽!

[解决办法]
换个端口试试。。。。。

热点排行