问题描述:客户端有一个发送环,测试时写入10条消息,auto_send_msg_longSoC()在发送环不为空时自动读取一条待发送消息并发给服务器,但是奇怪的是服务器端只能收到9条消息,从控制台打印出的消息来看,发送出的第一条消息服务器并没有收到,从而读取这条消息的回复时没有读到内容,若把auto_send_msg_longSoC()中sleep(600)注释掉,则服务器端显示进行了8次回复,但客户端只能正常读取到第8条,提高或减小休眠时间问题依旧。​public static void auto_send_msg_longSoC() throws UnknownHostException,IOException{
        //boolean flag = false;
        CommQueueItem currentItem = null;
        String itemAddr = null, uid = null;
        byte[] response = new byte[4096];
        int length = 0;
        int result = 0;
        int itemPort = 0;
        int bytesRead = 0;
        int bytesToRead = 8;        // 若发送队列不为空则取队首
        while(!Communication.sendQueue.isEmpty()){
            currentItem = Communication.sendQueue.getHeader();
            itemAddr = currentItem.GetHost();
            itemPort = currentItem.GetPort();            uid = currentItem.GetUID();            // 若目的地不同则重建socket
            try {
                if(itemAddr != host || itemPort != port){
                    host = itemAddr;
                    port = itemPort;                    // 关闭之前的连接
                    if(out != null){
                        out.close();
                    }
                    if(in != null){
                        in.close();
                    }
                    if(long_Socket != null){
                        long_Socket.close();
                    }                    // 建立新的socket连接
                    long_Socket = new Socket(host, port);
                    if(long_Socket != null){
                        outputStream = long_Socket.getOutputStream();
                        inputStream = long_Socket.getInputStream();                        out = new BufferedOutputStream(outputStream);
                        in = new BufferedInputStream(inputStream);                    }
                }                if(long_Socket != null){                        out.write(currentItem.GetMsg().toBytes());
                    out.flush();                    try {
                        Thread.sleep(600);
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }                    try{
                        if(in.available() > 0){
                            while(bytesRead < bytesToRead){
                                bytesRead = in.read(response, 0, bytesToRead);
                            }
                        }
                        System.out.println("TcpConn, recieve 已读取" + bytesRead + "字节 ");
                        //前8字节读取正常,则按照消息体长度读取消息体    
                        if(bytesRead == 8){
                            //得到消息体长度
                            byte[] len = Arrays.copyOfRange(response, 4, 8);
                            length = Tool.bytesToInt(len);                            //读取消息体
                            if(bytesRead < length){
                                result = in.read(response, bytesRead, length - 8);
                            }                            if(result == -1){
                                //break;
                                return;
                            }else{
                                bytesRead += result;
                            }
                        }
                        //检验得到的消息长度是否正常,正常则放入收入环中
                        if(bytesRead == length){
                            Message newMessage = new Message();                            byte[] msgLen = Arrays.copyOfRange(response, 4, 8);
                            byte[] msgBody = Arrays.copyOfRange(response, 8, response.length);                            System.out.println(response[3]);
                            newMessage.setOption(response[3]);
                            newMessage.setMsgLen(Tool.bytesToInt(msgLen));
                            if(msgBody != null){
                                newMessage.setMsgBody(msgBody);
                            }
                            else{
                                newMessage.setMsgBody(null);
                            }                            System.out.println("recieve UID is: " + uid);
                            System.out.println("newMessage is null : " + (newMessage==null?true:false));
                            Communication.recieveQueue.put(uid, newMessage);                            //System.out.println("TCPConn:接收队列共"+Communication.recieveQueue.getRealSize()+"个消息");
                            System.out.println("TCPConn:接收队列共"+Communication.recieveQueue.size()+"个消息");
                            //flag = true;
                        }                    }catch (IOException e) {
                        // TODO: handle exception
                        System.out.println("TcpConn, 接收回复消息出错: in I\\O错误");
                        e.printStackTrace();
                    }
                }
            }catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                System.out.println("long_Socket 地址错误!");
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("long_Socket I\\O错误!");
                e.printStackTrace();
            }
        }        //return flag;        
    }服务器端对不同socket分别起一个线程处理public void run()
    {
        byte[] response = null;        int bytesRead = 0;
        int bytesToRead = 8;        try
        {
            System.out.println("New Connection accepted:"+socket.getInetAddress()+":"+socket.getPort());            outputStream = socket.getOutputStream();
            // write msg
            // buffer the stream
            out = new BufferedOutputStream(outputStream);
            // send
            //out.flush();            inputStream = socket.getInputStream();
            in = new BufferedInputStream(inputStream);            int result = 0, i=1;
            response = new byte[1024];            while(in.read(response, bytesRead, bytesToRead) > 0){
                //先读前8个字节,即魔数(前三字节)和消息体长度(四个字节)    
                while(bytesRead < bytesToRead){
                    result = in.read(response, bytesRead, bytesToRead);                    if(result == -1)
                        break;
                    bytesRead +=result;    
                }
                //前四字节读取正常,则按照消息体长度读取消息体    
                if(bytesRead == 8){
                    //得到消息体长度
                    byte[] len = Arrays.copyOfRange(response, 4, 8);
                    int length = Tool.bytesToInt(len);                    //读取消息体
                    while(bytesRead < length){
                        result = in.read(response, bytesRead, length);
                        if(result == -1)
                            break;
                        bytesRead +=result;
                    }                    System.out.println("收到消息长:"+bytesRead+"字节");
                }                byte[] sendMsg = new byte[8];
                System.arraycopy(response, 0, sendMsg, 0, 3);
                sendMsg[3] = (byte)0x19;
                System.arraycopy(Tool.intToBytes(8), 0, sendMsg, 4, 4);
                System.out.println("sendMsg is null: " + (sendMsg == null?true:false));
                out.write(sendMsg);
                out.flush();
                System.out.println("sendMsg 长" + Tool.bytesToInt(Arrays.copyOfRange(sendMsg, 4, 8)));
                System.out.println("第"+i+"次回复完毕.");
                i++;
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            try{
                if(out != null){
                    out.close();
                }
                if(in != null){
                    in.close();
                }
                if(socket != null)
                    socket.close();
            }
            catch(IOException e){
                e.printStackTrace();// the client close the socket to trigger this exception
            }
        }
    }下面是测试时打印出的信息:
客户端:
before send asm:发送队列共10
TcpConn, recieve 已读取0字节 
0
recieve UID is: 9dea5736-781e-4d3f-8011-868910e0ba9f
newMessage is null : false
TCPConn:接收队列共1个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: 171988da-d032-4012-a028-742852cd0f7c
newMessage is null : false
TCPConn:接收队列共2个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: 6e3574ba-a96f-49a9-9cef-f88a61f27925
newMessage is null : false
TCPConn:接收队列共3个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: 93d87602-6e2a-469f-87af-6a3247119ddd
newMessage is null : false
TCPConn:接收队列共4个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: bfac2964-b25c-4292-adf5-c36a096fb0f4
newMessage is null : false
TCPConn:接收队列共5个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: b12fb9da-8332-4e43-9936-bd739916ce33
newMessage is null : false
TCPConn:接收队列共6个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: 21255d17-824c-4a11-bd97-ed485d0c7e91
newMessage is null : false
TCPConn:接收队列共7个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: 7a7adcf8-f2e9-42ae-ad7d-7f7826ddf8a9
newMessage is null : false
TCPConn:接收队列共8个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: 72c58d0d-b0eb-4f71-b591-41e4c448ef36
newMessage is null : false
TCPConn:接收队列共9个消息
TcpConn, recieve 已读取8字节 
25
recieve UID is: 838902cb-f407-481d-971b-102de0710f93
newMessage is null : false
TCPConn:接收队列共10个消息
asm:发送队列共0
asm:接收队列共10服务器:
New Connection accepted:/127.0.0.1:25399
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第1次回复完毕.
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第2次回复完毕.
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第3次回复完毕.
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第4次回复完毕.
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第5次回复完毕.
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第6次回复完毕.
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第7次回复完毕.
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第8次回复完毕.
收到消息长:8字节
sendMsg is null: false
sendMsg 长8
第9次回复完毕.Javasocket