不能用recv()传输最后一个数据包 ServerClient C文件 通过TCP传输的数据包

问题描述 投票:0回答:1

我一直在尝试通过TCP/IP在windows客户端到Linux服务器上传输文件,这几天一直是个大问题,因为不明白为什么服务器端没有收到最后一个数据包,这就影响了文件的写入。

我想实现的是下载图片(JPG,PNG),程序(EXE)等。

輸出方式

Output from server side

客户

            lltoa(size.QuadPart,container,10);
            //Send file size
            send(sock, container, sizeof(container), 0);
            bzero(container,1024);
            //End of file size
            unsigned __int64 uiPos = 0;
            unsigned __int64 uiRemaining = ul.QuadPart;
            while (uiRemaining > 0)
            {
                ul.QuadPart = uiPos;
                ov.Offset = ul.LowPart;
                ov.OffsetHigh = ul.HighPart;
                DWORD dwNumToSend = (uiRemaining <= 3) ? 1 :1024; //(DWORD)uiRemaining;
                if (!TransmitFile(sock,get_file, dwNumToSend, 0, &ov, NULL, 0))
                {
                    if ((GetLastError() != ERROR_IO_PENDING) && (WSAGetLastError() != WSA_IO_PENDING))
                    break;
                    WaitForSingleObject(ov.hEvent, INFINITE);
                }
                uiPos += dwNumToSend;
                uiRemaining -= dwNumToSend;
                //Sleep(1);
            }
            CloseHandle(ov.hEvent);
            CloseHandle(get_file);  

服务器

            file_size = atoi(response);
            printf("File size: %lli \n",file_size);
            bzero(response,18384);
            //End of file size
            
            //First packet of file
            
            len = recv(client_socket,response,sizeof(response)<file_size?(sizeof(response)):file_size,MSG_WAITALL);
            printf("Received: %i \n",len);
            if (len == 0)
            {
                printf("Error downloading file!");
                goto jump;
            }

            file_size = file_size - len;
            int counter = 0;
            while(file_size > 0)
            {
            //Start getting the file
                fwrite(response,1,len,new_file);
                bzero(response,18384);
                len = recv(client_socket,response,((file_size <= 1024) || (len < 1024))?file_size:sizeof(response),MSG_WAITALL);
                printf("Received: %i\t|",len);
                if(len <= 0)
                {
                    break;
                }
                //Check if this is the last packet
                
                file_size = file_size - len;
                printf("Response #%i\tbytes_received: %i\tbytes remain:%li \n",counter,len,file_size);
                counter += 1;
            }
            if(file_size > 0)
            {
                printf("\t\t\tError downloading file!  bytes left:%li \n",file_size);
                
            }
            else
            {
                printf("Successfully downloaded file! \n");
            }
            fclose(new_file);
            goto jump;

带有recv()的错误处理。

enter image description here如果需要更多的服务器端代码,请评论... 我想这样就可以了.

谢谢你!

c winapi tcp recv transmitfile
1个回答
1
投票

根据你展示的代码,发现了一些问题。经过编辑如下,它为我工作。你可以有一个尝试。(请显示 ul.QuadPart, ul.LowPartul.HighPart 相关代码,如果你还有问题的话)。)

在客户端。

更改 DWORD dwNumToSend = (uiRemaining <= 3) ? 1 :1024;DWORD dwNumToSend = (uiRemaining <= 1024) ? uiRemaining : 1024;

在服务器端。

改变 ((file_size <= 1024) || (len < 1024))?file_size:sizeof(response)(file_size <= sizeof(response))?file_size:sizeof(response)

© www.soinside.com 2019 - 2024. All rights reserved.