TCP服务器没有收到来自客户的权利的字节数 [英] TCP server doesn't receive the right bytes number from client

查看:109
本文介绍了TCP服务器没有收到来自客户的权利的字节数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做用C语言的TCP连接的一个小项目,并与我的code中的问题是在标题中提到。而下面的是我的code的部分上传

I'm doing a little project of TCP connection by C language, and the problem with my code is mentioned in the title. And the following is the uploading part of my code

客户端:

        FILE * fp = fopen(f2d, "rb+");
                    if(fp == NULL) perror("Fail to upload(client)");
                    else
                    {
                            fseek(fp, 0, SEEK_END);
                            int filesize = ftell(fp);
                            memset(buf, '\0', MAX_SIZE);
                            sprintf(buf, "%d", filesize);
                            send(serverSocket, buf, strlen(buf), 0); //send the filesize to the server
                            rewind(fp);
                            int byteNum = 0, z = 0;
                            printf("Uploading......\n");
                            while((z += byteNum) < filesize)
                            {
                                    memset(buf, '\0', MAX_SIZE);
                                    byteNum = fread(&buf, sizeof(char), sizeof(buf), fp);
                                    printf("Bytes read to buf : %d\n", byteNum);
                                    send(serverSocket, buf, sizeof(buf), 0);          
                                    printf("Totally sent bytes: %d\n", z);
                            }
                            printf("Upload completed.\n");
                    }
                    fclose(fp);

服务器端:

        printf("Upload Requested.\n");
                    f2df = fopen(buf + 4, "wb+");
                    if(f2df == NULL) perror("Fail to upload(server)");
                    else
                    {
                            memset(buf, 0, MAX_SIZE);
                            recv(clientSocket, buf, sizeof(buf), 0); //receive the filesize
                            int filesize = atoi(buf);

                            int recvNum = 0, recv_Num = 0;
                            while((recvNum += recv_Num) < filesize)
                            {
                                    memset(buf, 0, MAX_SIZE);
                                    recv_Num = recv(clientSocket, buf, sizeof(buf), 0);
                                    fwrite(&buf, sizeof(char), sizeof(buf), f2df);
                                    printf("Bytes received from recv: %d\n", recv_Num);
                                    printf("Totally received bytes: %d\n", recvNum);
                            }
                            printf("Upload completed.\n");
                    }
                    fclose(f2df);

我code的想法是发送和,直到它到达文件大小接收的字节数。但有一个非常高的频率(当然,有时它的功能正常,所有字节传输到服务器)的服务器似乎错过来自客户端(和接收函数的返回值发送一些字节然后将后零客户端发送的所有字节到服务器,这使得while循环的终止条件永远无法实现),然后会导致在服务器端不定式循环。
附:其他所有的功能都没有问题(例如,发送和接收文件大小,文件大小的实际数量...等),我测试过他们。并有也在客户端没有问题。

The idea of my code is to send and receive the bytes until it reaches the filesize. But with a very high frequency(well, sometimes it functions normally, all bytes are transferred to the server) that the server seems to miss some bytes sent from the client(and the return value of "recv" function will then be zero after the client has sent all the bytes to the server, which makes the terminating condition of the while loop never be reached), which then causes an infinitive loop on the server side. P.S. all the other functions are without problems(e.g. sending and receiving filesize, the actual number of the filesize... etc), I had tested them all. And there's also no problem on the client side.

和以下是问题的截图

在这里输入的形象描述

因此​​,任何人可以帮助我与,我一直在试图解决这个数周,非常感谢事先!

So can anyone help me out with that, I've been trying to deal with this for weeks, thanks a lot in advance!

推荐答案

通过你的整个客户端和服务器code你去解决:

Go through you entire client and server code and fix:

1)所有那些你没有正确处理结果时间从系统调用返回。这尤其适用于的recv(),它可以是负的,(错误),零返回的结果,(对关闭了连接),或小于一些正数,或等于请求的字节数 - 的唯一途径YOU可以告诉到底有多少字节被读入缓冲区。忘在呼叫和之前的任何的memset / bzero请求的字节数,和/或的strlen /不管后,它返回,它们都是不足/误导

1) All those times where you do not handle correctly the results returned from system calls. This especially applies to the result returned by recv() which can be negative, (error), zero, (peer closed the connection), or some positive number less than, or equal to, the number of bytes requested - the ONLY WAY YOU CAN TELL FOR SURE HOW MANY BYTES HAVE BEEN READ IN TO THE BUFFER. Forget the number of bytes requested in the call and any memset/bzero before, and/or strlen/whatever after, it returns, they are all inadequate/misleading.

2)所有那些你认为,仅仅因为你要求的X字节和recv(recv的的()倍)返回一个正数,这个数字就是X

2) All those times where you assume that, just because you request the recv() of X bytes and recv() returns a positive number, that number will be X.

3)所有那些你打电话是不是100%保证是空终止的。

3) All those times that you call any str* C-style library calls on any buffers that are not 100% guaranteed to be NULL-terminated.

这篇关于TCP服务器没有收到来自客户的权利的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆