发垃圾数据,能发,说明通的,异常说明~o~.................zzz

解决方案 »

  1.   

    try
    {
        s.getInputStream();
    }catch(Exception e)
    {
        //socket is closed.
    }
      

  2.   

    对,当他是连着的,当进行通讯时发生异常后就知道了.
    当你不断的read的时候,server断,exception就catch到了,没有问题的.
      

  3.   

    断开的时候,读取的长度是0,我根据为0判断断开。可是这时如果是SERVER向我发个长度为0的包,我也会认为是断开的:(
      

  4.   

    读到长度0不能断定是已经断开.除非是-1,才代表输入流已经被关闭,说名socket已经close,而且关键在于你怎么读,实际上应该是一个单独的线程一直在读,因为你无法预先知道能不能一次将发过来的包读完,也无法知道一次是不是读完整了包,所以线称一直循环在读并且一直在解包.这样即能知道什么时候server断,也完成了消息的解读,我的方案是这样的,在大数据量高速率网络上运行没有发生过丢包.
      

  5.   

    不对呀。我把SERVER突然断掉后,读到的数据长度是0,而不是-1呀。
      

  6.   

    读到0,没有数据read根本不返回,你怎么判断,server断,就出exception了,你还判断什么!!!
      

  7.   

    server断,没有exception. 而且读到0,也返回。 :(
    怎么和老兄说的都不一样啊?
      

  8.   

      //线程运行主程序段
      public void run(){
        while (Action !=0){
         try{
           is = socket.getInputStream();
           ir = new InputStreamReader(is);
           AppServer.sysinfo.HNMState = "正常!!";
           char str[] = new char[1024];
           ir.read(str);    //平时在这里等待,但是SERVER一断就立即会向下运行。
           String Packet = new String(str);
           if ( Packet.trim().length()>0) {
    //         AppServer.log.showmessage(Packet,0);
             UnPack(Packet);
           }
           else{
             Integer ee = new Integer(Packet.trim().length());
             AppServer.sysinfo.HNMState = "断开!!";
             break;
             }
          }catch (Exception err){
           AppServer.log.showmessage("解包错误",0);  //没有出现过EXCEPTION
          }
        }  }
      

  9.   

    SERVER端就是个简单的监听SOCKET呀,有什么关系?
      

  10.   

    把InputStreamReader换成BufferedInputStream,或者直接用InputStream,Reader是基与char的,Java 中char占2bytes,缓充也最好用byte类型再试试.
    程序其他没有什么问题.
    把getInputStream放到循环外面效率高点.
      

  11.   

    import java.net.*;                                
    import java.io.*;                                 
                                                     
    public class SimpleClient {                       
                                                     
        public static void main(String args[])            
        throws IOException {                              
            int c;                                            
            Socket s1;                                        
            InputStream s1In;                                
            DataInputStream dis;                             
                                                     
            // Open your connection to a server, at port 5432
            // localhost used here                           
            s1 = new Socket("192.111.111.2",5432);    
            
            //set up the time out of read operation = 100s, if occured assume server dead.
            s1.setSoTimeout(10000);           
                                                     
            // Get an input file handle from the socket and  
            // read the input                                
            s1In = s1.getInputStream();                      
            dis = new DataInputStream(s1In);                         while (true){
                try {                                         
                    String st = new String (dis.readUTF());          
                    System.out.println(st);                          
                }catch (InterruptedIOException ee){
                    System.out.println("Time out , Maybe Server dead or power down casually");
                    break;
                }catch (EOFException ee){
                    System.out.println("End of Stream");
                    break;
                }catch (IOException e){
                    System.out.println("IOException, May be server closed");
                    break;
                }
            }
                                                     
            // When done, just close the connection and exit 
            dis.close();                                     
            s1In.close();                                    
            s1.close();                                      
        }                                                
    }                                                
      

  12.   

    import java.net.*;
    import java.io.*;public class SimpleServer {
        public static void main(String args[]) {
            ServerSocket s = null;
            Socket s1;
            String sendString = "Hello Net World!";
            int slength = sendString.length();
            OutputStream s1out;
            DataOutputStream dos;        // Register your service on port 5432
            try {
                s = new ServerSocket(5432);
            } catch (IOException e) {
                // ignore
            }
            // Run the listen/accept loop forever
            int count=0;
            while (true) {
                try {
                    // Wait here and listen for a connection
                    s1=s.accept();                // Get a communication stream associated with
                    // the socket
                    s1out = s1.getOutputStream();
                    dos = new DataOutputStream (s1out);                // Send your string!
                    // (UTF provides machine independence)
                    while (System.in.available()==0){
                        dos.writeUTF(sendString);
                    }
                    // clear key board input
                    while (System.in.available()!=0){
                        System.in.read();
                    }                System.out.println("SocketClosed "+ count++);
     
                    // Close the connection, but not the server socket
                    dos.close();
                    s1out.close();
                    s1.close();
     
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
      

  13.   

    我重写了一个简单的server和client,考虑到再internet上应用采用unicode作为传输编码。
    当再server端按 enter键正常close掉socket,
    按ctrl+c相当于结束server,
    直接关掉运行server的电源会引起client的等待,。
    则在client端分别引起EOFExceptin 
    IOException 以区别这两种情况,
    等待10秒钟后就会因超时退出。
    这样应该能解决你的问题了吧,别忘了给加份哦