Linux Socket编程实现简单的计算器

Linux Socket编程实现简单的计算器

socket网络通信实现,客户端发送2个数据到服务器计算,再返回结果。
代码如下:

服务器代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAXBUF 1024

typedef struct
{
     
	char s;
	float a;
	float b;
}Node, *PNode;

int main(int argc, char *argv[])
{
     
    int sockfd, new_fd;
    struct sockaddr_in my_addr;
    unsigned int myport, lisnum;
    char buf[MAXBUF + 1];

    if (argv[2])
		myport = atoi(argv[2]);
    else
		myport = 7575;

    if (argv[3])
		lisnum = atoi(argv[3]);
	else
		lisnum = 5;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
    {
     
		perror("socket");
		exit(EXIT_FAILURE);
    } 

    bzero(&my_addr, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(myport);
    if (argv[1])
		my_addr.sin_addr.s_addr = inet_addr(argv[1]);
    else
		my_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr))== -1) 
    {
     
		perror("bind");
		exit(EXIT_FAILURE);
    }
    if (listen(sockfd,lisnum ) == -1) 
    {
     
		perror("listen");
		exit(EXIT_FAILURE);
    }
    printf("wait for connect\n");
    
    struct sockaddr_in their_addr;   
	int len = sizeof(their_addr);
	if ((new_fd =accept(sockfd, (struct sockaddr *) &their_addr,(socklen_t *)&len)) == -1) 
	{
     
		perror("accept");
		exit(EXIT_FAILURE);
	} 
	else
			printf("server: got connection from %s, port %d, socket %d\n",inet_ntoa(their_addr.sin_addr),ntohs(their_addr.sin_port), new_fd);
/*
	if(fcntl(sockfd, F_SETFL, O_NONBLOCK) == -1) //设置socket为非阻塞
	{
		perror("fcntl");
		exit(EXIT_FAILURE);
	}				 
*/
	
	while(1)
	{
     
		Node myNode;
		int ret, rets;
		float result_send;
		ret = recv(new_fd,&myNode.s,sizeof(char),0);
		ret = recv(new_fd,(void *)&myNode.a,sizeof(float),0);
		myNode.a = ntohl(myNode.a);
		ret = recv(new_fd,(void *)&myNode.b,sizeof(float),0);
		myNode.b = ntohl(myNode.b);	
		result_send=0;
		char ps = myNode.s;
		switch(ps)
		{
     
			case '+':
				result_send = myNode.a + myNode.b;
				break;
			case '-':
				result_send = myNode.a - myNode.b;
				break;
			case '*':
				result_send = myNode.a * myNode.b;
				break;
			case '/':
				result_send = myNode.a / myNode.b;
				break;
		}
		printf("message:%f %c %f = %f \n", myNode.a, myNode.s, myNode.b,result_send);

		result_send = htonl(result_send);
		ret = send(new_fd, &result_send,sizeof(float),0);
		if(rets == -1)
		{
     
     		perror("result_send");
			exit(EXIT_FAILURE);
		}
	}

	close(new_fd);
	close(sockfd);
    return 0;
}

客户端代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MAXBUF 1024
typedef struct
{
     
	char s;
	float a;
	float b;
}Node, *PNode;

int main(int argc, char **argv)
{
     
    int sockfd, len, j=0;
    struct sockaddr_in dest;
    char buffer[MAXBUF + 1];
    if (argc != 3) 
    {
     
		printf(" error format,it must be:\n\t\t%s IP port\n",argv[0]);
		exit(EXIT_FAILURE);
    }

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     
		perror("Socket");
		exit(errno);
    }
    printf("socket created\n");

    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = htons(atoi(argv[2]));
    if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0) 
    {
     
		perror(argv[1]);	
		exit(errno);
    }
    if (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest))==-1) 
    {
     
		perror("Connect ");
		exit(errno);
    }
     	 printf("server connected\n");

	while(1)
	{
     
		bzero(buffer, MAXBUF + 1);
		Node myNode;
		printf("请输入运算符(+,-.*,/):\n");
		if(j++ >=1)  getchar();		//消去回车键的获取
		scanf("%c", &myNode.s);
		printf("请输入两个数:");
		scanf("%f %f",&myNode.a,&myNode.b);
		
		int num,byte,ret;
		float result_send,result_recv;

		ret = send(sockfd,&myNode.s,sizeof(char),0);
		myNode.a = htonl(myNode.a);
		ret = send(sockfd,(void *)&myNode.a,sizeof(float),0);
     		myNode.b = htonl(myNode.b);
		ret = send(sockfd,(void *)&myNode.b,sizeof(float),0);
        bzero(buffer, MAXBUF + 1);
		if((ret=recv(sockfd,&result_recv,sizeof(float), 0))<0)
		{
     
			perror("recv");
			exit(-1);
		}
		
		result_recv = ntohl(result_recv);
		printf("结果为:%f\n",result_recv);
	}
  	close(sockfd);
    return 0;
}

运行结果:
Linux Socket编程实现简单的计算器_第1张图片
Linux Socket编程实现简单的计算器_第2张图片

参考:
https://blog.csdn.net/xiao_huo_ban/article/details/78952282

你可能感兴趣的:(Linux Socket编程实现简单的计算器)