Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从客户端向服务器发送字符串时,只接收第一个字节_C_Linux_Sockets_File Io - Fatal编程技术网

C 从客户端向服务器发送字符串时,只接收第一个字节

C 从客户端向服务器发送字符串时,只接收第一个字节,c,linux,sockets,file-io,C,Linux,Sockets,File Io,我制作了一个客户机/服务器程序,正在从客户机向服务器发送一个文件 下面是一段代码: 客户端: FILE *f = fopen("file.txt" ,"r"); size_t bytes = 0; while(bytes = fread(buffer ,sizeof(char) , sizeof(buffer) ,f)>0) {printf("buff%s\n" , buffer); send(sockfd ,buffer ,bytes , 0); } fclose(f); prin

我制作了一个客户机/服务器程序,正在从客户机向服务器发送一个文件

下面是一段代码:

客户端:

FILE *f = fopen("file.txt" ,"r");
size_t bytes = 0;
while(bytes = fread(buffer ,sizeof(char) , sizeof(buffer) ,f)>0)
{printf("buff%s\n" , buffer);
send(sockfd ,buffer ,bytes , 0);
} 
fclose(f);
  printf("%s\n",buffer);
服务器端:

FILE *f = fopen("file1.txt" ,"w");
while(bytes = recv(newsockfd ,buffer , sizeof(buffer) ,0)>0)
{
printf("bytes%d" , bytes);
fwrite(buffer,sizeof(char) ,bytes , f);
}
bytes = recv(newsockfd ,buffer , sizeof(buffer) ,0);
printf("bytessss%d" , bytes);
  fclose(f);  
     printf("Here is the message: %s\n",buffer);
     close(newsockfd);

但是,当我将其发送到服务器时,服务器生成一个文件并仅存储第一个字节,例如,当我发送“hi whats up”时,服务器仅存储“h”。

您缺少一个括号:

while(bytes = recv(newsockfd ,buffer , sizeof(buffer) ,0) > 0)
这使得
bytes
变量为1或0,因为表达式计算为
recv(newsockfd,buffer,sizeof(buffer),0)>0
,尽管读取的字节数是正确的。添加如下括号:

while ((bytes = recv(newsockfd ,buffer , sizeof(buffer) ,0)) > 0)
       ^                                                   ^
忽略了这一点,但同样适用于您的客户机,您将所有字节读取到缓冲区,但
bytes
变量再次被赋值为1,因为

while(bytes = fread(buffer ,sizeof(char) , sizeof(buffer) ,f)>0)
评估方法如下:

while(bytes = (fread(buffer ,sizeof(char) , sizeof(buffer) ,f)>0) )
               ^
               1. call fread, keep the result in temporary place (let's call it X)
                                                              ^
                                                    2. compare "x" to 0
            ^
            3. store result of comparison (instead of fread) in the variable bytes.
也就是说,从文件中读取
sizeof(buffer)
字节,如果读取的字节数大于0,则将1放入
bytes
,否则将0放入(布尔表达式的结果为1(true)或0(false)),因此即使读取100个字节,缓冲区也确实充满了它们,但是
bytes
变量等于1,因此发送1 byte。当您再次尝试读取时,没有任何内容可读取,因为上次您已经读取了100个字节。额外的圆括号使其首先将读取的字节数分配给
字节
变量,然后将其与0进行比较:

while((bytes = fread(buffer ,sizeof(char) , sizeof(buffer) ,f))>0)

您遗漏了一个括号:

while(bytes = recv(newsockfd ,buffer , sizeof(buffer) ,0) > 0)
这使得
bytes
变量为1或0,因为表达式计算为
recv(newsockfd,buffer,sizeof(buffer),0)>0
,尽管读取的字节数是正确的。添加如下括号:

while ((bytes = recv(newsockfd ,buffer , sizeof(buffer) ,0)) > 0)
       ^                                                   ^
忽略了这一点,但同样适用于您的客户机,您将所有字节读取到缓冲区,但
bytes
变量再次被赋值为1,因为

while(bytes = fread(buffer ,sizeof(char) , sizeof(buffer) ,f)>0)
评估方法如下:

while(bytes = (fread(buffer ,sizeof(char) , sizeof(buffer) ,f)>0) )
               ^
               1. call fread, keep the result in temporary place (let's call it X)
                                                              ^
                                                    2. compare "x" to 0
            ^
            3. store result of comparison (instead of fread) in the variable bytes.
也就是说,从文件中读取
sizeof(buffer)
字节,如果读取的字节数大于0,则将1放入
bytes
,否则将0放入(布尔表达式的结果为1(true)或0(false)),因此即使读取100个字节,缓冲区也确实充满了它们,但是
bytes
变量等于1,因此发送1 byte。当您再次尝试读取时,没有任何内容可读取,因为上次您已经读取了100个字节。额外的圆括号使其首先将读取的字节数分配给
字节
变量,然后将其与0进行比较:

while((bytes = fread(buffer ,sizeof(char) , sizeof(buffer) ,f))>0)

正如前面的答案所解释的,优先规则是代码中唯一的问题

>的优先级高于=。

我认为以下问题的前两个答案将帮助您


我认为那个家伙和你一样有同样的问题…

正如前面的答案所解释的,优先规则是代码中唯一的问题

>的优先级高于=。

我认为以下问题的前两个答案将帮助您


我想那家伙和你一样也有同样的问题……

是的,它起作用了,谢谢先生,请你解释一下发生了什么事,比如说它是如何产生1 tp 0的谢谢,它起作用了,谢谢先生,请你解释一下它是如何产生1 tp 0的谢谢