简体   繁体   English

确保TCP客户端和服务器之间没有丢包

[英]Ensuring no packet loss between TCP client and server

I am writing a Java TCP client which sends chunks of data to a C server. 我正在编写一个Java TCP客户端,它将数据块发送到C服务器。 The client-server worked very well on my development PC. 客户端 - 服务器在我的开发PC上运行良好。 This code upon deployment on a hardware board showed packet loss. 在硬件板上部署时,此代码显示数据包丢失。 I only have the logs with me and I know that the server did not receive all packets. 我只有我的日志,我知道服务器没有收到所有数据包。 I do not have the hardware to test. 我没有要测试的硬件。 Therefore, at the first level, I want to be very sure client code does send all the required data. 因此,在第一级,我想非常确定客户端代码确实发送了所有必需的数据。

Here is my code(the client part in Java). 这是我的代码(Java中的客户端部分)。 How do I make sure this is done? 我如何确保这样做? Is there some resend commands with timings etc? 是否有一些重定时命令等命令?

        Socket mySocket = new Socket("10.0.0.2",2800);
        OutputStream os = mySocket.getOutputStream();         

        System.out.println(" Sending 8 byte Header Msg with length of following data to Server");
        os.write(hdr, 0, 8);  
        os.flush();

        System.out.println(" Sending Data ");
        start = 0;
        for(int index=0; index < ((rbuffer.length/chucksize)+1); index++){             
            if(start + chucksize > rbuffer.length) {
                System.arraycopy(rbuffer, start, val, 0, rbuffer.length - start);
            } else {
                System.arraycopy(rbuffer, start, val, 0, chucksize);
            }
            start += chucksize ;

            os.write(val,0,chucksize);  
            os.flush();
        }

Here is the C snippet which receives this data: 以下是接收此数据的C片段:

while ((bytes_received = recv(connected, rMsg, sizeof(rMsg),0)) > 0){

 if (bytes_received  > 0)      // zero indicates end of transmission */
    {
    /* get length of message (2 bytes) */
    tmpVal = 0;
    tmpVal |= rMsg[idx++];
    tmpVal = tmpVal << 8;
    tmpVal |= rMsg[idx++];
    msg_len = tmpVal;

    len = msg_len;
    //printf("msg_len = %d\n", len);
    printf("length of following message from header message : %d\n", len);
    char echoBuffer[RCVBUFSIZE] ;        
    memset(echoBuffer, 0, RCVBUFSIZE);
    int recvMsgsize = 0;

    plain=(char *)malloc(len+1);
    if (!plain)
    {
     fprintf(stderr, "Memory error!");
    }
    for( i = RCVBUFSIZE; i < (len+RCVBUFSIZE); i=i+RCVBUFSIZE){
     if(i>=len){
         recvMsgSize = recv(connected, echoBuffer, (len - (i-RCVBUFSIZE)), 0);
         memcpy(&plain[k], echoBuffer, recvMsgSize);
         k = k+recvMsgSize;
     }
     else{
         recvMsgSize = recv(connected, echoBuffer, RCVBUFSIZE, 0);
         memcpy(&plain[k], echoBuffer, recvMsgSize);
         k = k+recvMsgSize;
      }
    }
 }//closing if
}//closing while

First of all there is no such thing as packet loss in TCP/IP. 首先,没有TCP / IP中的丢包。 This protocol was designed to reliably send a stream of bytes in correct order. 该协议旨在以正确的顺序可靠地发送字节流。 So the problem must be with your application or the other side. 所以问题必须在你的应用程序或另一方。

I am not really in a mood to analyze this whole arraycopy() madness (C anyone?), but why aren't you just sending the whole rbuffer in one go through BufferedOutputStream ? 我真的不想分析整个arraycopy()疯狂(C任何人?),但为什么你不是只通过BufferedOutputStream发送整个rbuffer

OutputStream os = new BufferedOutputStream(mySocket.getOutputStream());

and then: 然后:

os.write(rbuffer);

Believe me, BufferedOutputStream is doing the exact same thing (collecting bytes into chunks and sending them in one go). 相信我, BufferedOutputStream正在做同样的事情(将字节收集到块中并一次发送)。 Or maybe I am missing something? 或许我错过了什么?

I changed the C side program in the following way and it now works: 我通过以下方式更改了C端程序,现在可以正常工作:

                    printf("length of following message from header message : %d\n", len);

                    plain=(char *)malloc(len+1);
                    if (!plain)
                    {
                        fprintf(stderr, "Memory error!");

                    }
                    memset(plain, 0, len+1);
                    int remain = len;
                    k= 0;
                    while (remain){
                        int toGet = remain > RCVBUFSIZE ? RCVBUFSIZE : remain;
                        remain -= toGet;
                        int recvd = 0;
                        while(recvd < toGet) {
                            if((recvMsgSize = recv(connected, echoBuffer, toGet-recvd, 0)) < 0){
                                printf("error receiving data\n");
                            }
                            memcpy(&plain[k], echoBuffer, recvMsgSize);
                            k += recvMsgSize;
                            printf("Total data accumulated after recv input %d\n", k);
                            recvd += recvMsgSize;
                        }
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM