在客户端上不获取任何值或垃圾值(C 中的 TCP 套接字编程)



我是套接字编程的初学者,我正在尝试开发一个简单的程序,客户端向服务器发送一个数字,服务器计算相同的阶乘并将其发送回客户端,客户端打印它。

服务器端的计算工作正常,但客户端要么不显示结果,要么打印垃圾值。

这背后的原因可能是什么?

客户端代码:

#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#define MAXSIZE 50

main()
{
int sockfd,retval;
int recedbytes,sentbytes;
struct sockaddr_in serveraddr;
char buff[MAXSIZE];
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("nSocket Creation Error");
}
//printf("%i",sockfd);
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(3388);
serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
retval=connect(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
if(retval==-1)
{
printf("Connection error");
}
while(1)
{
printf("Enter the Number: n");
scanf("%s",buff);
sentbytes=send(sockfd,buff,sizeof(buff),0);
if(sentbytes==-1)
{
printf("!!");
close(sockfd);
}
int num=atoi(buff);
if(num==-1 || num==0)
{
break;
}
strcpy(buff,"");
recedbytes=recv(sockfd,buff,sizeof(buff),0);
printf("The factorial is: ");
puts(buff);
printf("n");
}
close(sockfd);
}

服务器端代码:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#define MAXSIZE 90
main()
{
int sockfd,newsockfd,retval;
socklen_t actuallen;
int recedbytes,sentbytes;
struct sockaddr_in serveraddr,clientaddr;
char buff[MAXSIZE];
int a=0;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("nSocket creation error");
}
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(3388);
serveraddr.sin_addr.s_addr=htons(INADDR_ANY);
retval=bind(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
if(retval==1)
{
printf("Binding error");
close(sockfd);
}
retval=listen(sockfd,1);
if(retval==-1)
{
close(sockfd);
}
actuallen=sizeof(clientaddr);
newsockfd=accept(sockfd,(struct sockaddr*)&clientaddr,&actuallen);

if(newsockfd==-1)
{
close(sockfd);
}
while(1)
{
// printf("comes here");
strcpy(buff,"");
recedbytes=recv(newsockfd,buff,sizeof(buff),0);

if(recedbytes==0)
{ continue;}
if(recedbytes==-1)
{
close(sockfd);
close(newsockfd);
}
int num=atoi(buff);
if(num==-1)
{
break;
}

int res=1;
int i;
for(i=1;i<=num;i++)
{
res=res*i;
}
// printf("hello");

sprintf(buff, "%d", res);
printf("The factorial of %i = %sn",num,buff);
sentbytes=send(newsockfd,buff,sizeof(buff),0);
if(sentbytes==-1)
{
close(sockfd);
close(newsockfd);
}

}
close(sockfd);
close(newsockfd);
}

示例输出:

客户端:

Enter the Number: 
6
The factorial is: 720
Enter the Number: 
2
The factorial is: 
Enter the Number: 
4
The factorial is: 2
Enter the Number: 
7
The factorial is: 
Enter the Number: 
2
The factorial is: /V
Enter the Number: 
5
The factorial is: /V
Enter the Number:

服务器端:

The factorial of 6 = 720
The factorial of 2 = 2
The factorial of 4 = 24
The factorial of 7 = 5040
The factorial of 2 = 2
The factorial of 5 = 120

当客户端读取 50 字节缓冲区时,服务器将发回 90 字节缓冲区。 这意味着客户端首先读取 50 个字节,该字节以预期的字符串开头。 然后在下一次调用recv时,读取来自同一消息的接下来的 40 个字节,这些字节很可能包含垃圾。 此调用可能还包含末尾下一条消息的前 10 个字节。

最简单的解决方法是将两个send调用更改为发送strlen(buf)+1字节而不是sizeof(buf)字节,以便只发送所需的数据量。

相关内容

  • 没有找到相关文章

最新更新