本帖最后由 onedear 于 2009-11-10 16:56:49 编辑

解决方案 »

  1.   

    好像没有连上服务器
    你要写socket 连接 地址和端口。
      

  2.   

    参考一下这个import java.io.DataInputStream;
    import java.io.IOException;
    import javax.microedition.io.Connection;
    import javax.microedition.io.Connector;
    import javax.microedition.io.ServerSocketConnection;
    import javax.microedition.io.SocketConnection;
    import javax.microedition.midlet.*;/**
     * Socket编程:服务器端
     *
     * @author mouyong
     */
    public class ServerMidlet1 extends MIDlet {
        public void startApp() {
            ServerSocketConnection con=null;
            try {
                //服务器监听9999端口
                con =(ServerSocketConnection) Connector.open("socket://:9999"); //open的参数是专门的格式来书写的
                System.out.println("服务器IP:"+con.getLocalAddress());
                System.out.println("服务器端口:"+con.getLocalPort());
                //等待客户端连接
                //如果没有连接,程序将在这里堵塞
                //如果有连接,则返回SocketConnection,充当通信的桥梁
                //之后,就可以通过sc进行通信
                SocketConnection sc=(SocketConnection)con.acceptAndOpen();
                System.out.println("客户端:"+sc.getAddress());            //接收客户端的输入
                DataInputStream din=sc.openDataInputStream();
                //readUTF()同样会出现堵塞
                System.out.println("客户端说:"+din.readUTF());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }    public void pauseApp() {
        }    public void destroyApp(boolean unconditional) {
        }
    }/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package lession18;import java.io.DataOutputStream;
    import java.io.IOException;
    import javax.microedition.io.Connector;
    import javax.microedition.io.SocketConnection;
    import javax.microedition.midlet.*;/**
     * Socket编程:客户端
     *
     * @author mouyong
     */
    public class ClientMidlet1 extends MIDlet {
        public void startApp() {
            try {
                //客户端可以通过sc与服务器端通信
                SocketConnection sc=(SocketConnection)Connector.open("socket://192.168.0.13:9999"); //连接到服务器端            //开始与服务器通信:送给服务器一个“你好!”
               DataOutputStream dout= sc.openDataOutputStream();
               dout.writeUTF("你好!");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }    public void pauseApp() {
        }    public void destroyApp(boolean unconditional) {
        }
    }
      

  3.   

      System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(10000));如果只是单单超时就设置一下 但是感觉你的不只是超时啊   in.read(buffer, 0,1024);是不是因为这个数据没读慢啊 所以一直读 就连接超时了  没看到全部代码 不敢确定
      

  4.   

    是的,就是一直读,然后超时了,应该怎么处理呢?
    主要的逻辑是我一旦out.flush();
    就马上读取管道信息 in.read(buffer, 0,1024);然后就超时了,但发送的数据还是到了服务端那边因为代码太长了,所以就没全部贴出来,如果还不能解决的话,我再把代码贴出来,
    谢谢大家
      

  5.   

    源代码是:
    这四步:
    //写入管道
    int ret = msgDataSubmit.write(baseSocket.getOut());
    //flush管道
    ret = baseSocket.flushOutputData() ;
    //设置超时时间,单位是秒
    ret = baseSocket.setSocketTimeout(20);
    //获取返回信息
    ret = baseSocket.receiveData(buffer);这是四个方法的源代码public int write(DataOutputStream out) {
    try {
    out.writeInt(len);
    out.writeChar(factorycode);
    out.writeChar(progid);
    out.writeChar(morepkt);
    out.write(cmd_id);
    out.writeInt(start_num);
    out.writeInt(end_num);
    out.writeInt(request_id);
    out.writeInt(sequence);
    out.write(rec_seperator);
    out.write(field_seperator);
    out.writeInt(reserved1);
    out.writeInt(reserved2);

    out.writeShort(errorcode);
    out.write(datatrans);
    return 0 ;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return -1 ;
    }

    }public int flushOutputData() {
            try {
                out.flush();
            } catch (Exception ex) {
                ex.printStackTrace();
                return -1;
            }
            return 0;
        }public int setSocketTimeout(int seconds) {
            try {
                socket.setSoTimeout(seconds * 1000);
            } catch (Exception ex) {
                ex.printStackTrace();
                return -1;
            }
            return 0;
        }public int receiveData(DataResultBuffer buffer) {
            int len = 0;
            int packetLen;
            do {
                do {
                    if (buffer.getBufferLen() >= 4)
                        break;
                    try {
                        len = in.read(buffer.getBuffer(), buffer.getBufferLen(),buffer.getBuffer().length - buffer.getBufferLen());
                        System.out.println("从网络管道中读取到的字节数:"+len);
                    } catch (InterruptedIOException ex) {
                        ex.printStackTrace();
                        return -1;
                    }catch (Exception ex) {
                        ex.printStackTrace();
                        return -1;
                    }
                    if (len == -1 || len == 0) {
                        System.out.println("从网络管道中读取到错误的字节长度(0/-1)");
                        return -1;
                    }
                    len += buffer.getBufferLen();
                    buffer.setBufferLen(len);
                } while (len < 4);            DataInputStream data = new DataInputStream(
                        new ByteArrayInputStream(buffer.getBuffer()));
                packetLen = 0;
                try {
                    packetLen = data.readInt();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return -1;
                }
                if (packetLen >= 1000 || packetLen <= 0) {
                    System.out.println("信息包数据长度解析出错:"+packetLen);
                    return -1;
                }
                if (buffer.getBufferLen() >= packetLen)
                    break;
                try {
                    len = in.read(buffer.getBuffer(), buffer.getBufferLen(),buffer.getBuffer().length - buffer.getBufferLen());
                } catch (InterruptedIOException ex) {
                    ex.printStackTrace();
                    return -1;
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return -1;
                }
                if (len == -1 || len == 0) {
                    System.out.println("从网络管道中读取到错误的字节长度(0/-1)");
                    return -1;
                }
                len += buffer.getBufferLen();
                buffer.setBufferLen(len);
            } while (len < packetLen);        return buffer.processBuffer(packetLen) == 0 ? 0 : -1;
        }其中获取服务端返回信息的时候,到len = in.read(buffer.getBuffer(), buffer.getBufferLen(),buffer.getBuffer().length - buffer.getBufferLen());
    这一步的时候就抛出java.net.SocketTimeoutException: Read timed out 
    初始化时buffer.getBufferLen()  =0 ,buffer.getBuffer().length - buffer.getBufferLen() = 1000
    buffer.getBuffer() = bufferr =  new byte[1000]
    所以这一句等同于len = in.read(bufferr, 0,1000);
      

  6.   

    其中获取服务端返回信息的时候,到len = in.read(buffer.getBuffer(), buffer.getBufferLen(),buffer.getBuffer().length - buffer.getBufferLen()); 
    是receiveData方法的第八行
      

  7.   


    DataInputStream in = new DataInputStream(s.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));这里就有问题了,后面的代码没看。
    你不能把socket.getInputStream构建成两个input.尤其是BufferedReader,因为BufferedReader是有缓存的,用了BufferedReader就会把更多的数据缓存在BufferedReader内部的cache里,DataInputStream就不可能读到正确的数据了。