LinuxC之socket通信实现简单的计算器

LinuxC之socket通信实现简单的计算器

代码如下:

服务器端代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 5576
#define LISTEN_QUEUE 1000
#define BSIZE 1024

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

int main(int argc,char *argv[])
{
    struct sockaddr_in server_addr;
    bzero(&server_addr,sizeof(server_addr));
    server_addr.sin_family=AF_INET;
    server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    server_addr.sin_port=htons(SERVER_PORT);
    int server_socket=socket(AF_INET,SOCK_STREAM,0);
    if(server_socket<0)
    {
        printf("Creat Socket Failed!\n");
        exit(1);
    }
    if(bind(server_socket,(struct sockaddr *)&server_addr,sizeof(server_addr))==-1)
    {
        printf("Server bind port %d failed\n",SERVER_PORT);
        exit(1);
    }
    if(listen(server_socket,LISTEN_QUEUE))
    {
        printf("Server Listen Failed!\n");
        exit(1);
    }
    while(1)
    {
        struct sockaddr_in client_addr;
        int length=sizeof(client_addr);
        int new_server_socket=accept(server_socket,(struct sockaddr*)&client_addr,(socklen_t *)&length);
        if(new_server_socket<0)
        {
            printf("Server Accept Failed!\n");
            break;
        }
        while(1)
        {
        Node myNode;
        int needRecv=sizeof(myNode);
        float result_send;
        int num,n,byte;
        if((num=recv(new_server_socket,&myNode,needRecv,0))==-1)
        {
            perror("recv");
            exit(EXIT_FAILURE);
        }
        result_send=0;
        if(strcmp(myNode.s,"add")==0)
        {
            result_send=0;
            result_send=myNode.a+myNode.b;
        }
        else if(strcmp(myNode.s,"sub")==0)
        {
            result_send=0;
            result_send=myNode.a-myNode.b;
        }
        else if(strcmp(myNode.s,"mul")==0)
        {
            result_send=0;
            result_send=myNode.a*myNode.b;
        }
        if(strcmp(myNode.s,"div")==0)
        {
            result_send=0;
            if(myNode.b==0)
                result_send=0;
            else
            result_send=myNode.a/myNode.b;
        }
        if((byte=send(new_server_socket,&result_send,sizeof(result_send),0))==-1)
        {
            perror("result_send");
            exit(EXIT_FAILURE);
        }
      }
      close(new_server_socket);
    }
    close(server_socket);
    return 0;
}

客户端代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MYPORT 5576
#define BSIZE 1024

typedef struct 
{
    char s[100];
    float a;
    float b;
}Node,*PNode;
int main()
{
    int sock_cli=socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in servaddr;
    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port=htons(MYPORT);
    servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    if(connect(sock_cli,(struct sockaddr *)&servaddr,sizeof(servaddr))==-1)
    {
        perror("connect");
        exit(1);
    }
    while(1)
    {
    Node myNode;
    char echr[1024];
    printf("请输入数据:add/sub/mul/div\n");
    scanf("%s",myNode.s);
    printf("请输入两个数:");
    scanf("%f%f",&myNode.a,&myNode.b);
    int needSend=sizeof(myNode);
    int num,byte;
    float result_send,result_recv;
    if((num=send(sock_cli,&myNode,needSend,0))==-1)
    {
        perror("send");
        exit(EXIT_FAILURE);
    }
    result_recv=0;
    if((byte=recv(sock_cli,&result_recv,sizeof(float),0))==-1)
    {
        perror("recv");
        exit(-1);
    }
    printf("结果为:%f\n",result_recv);
    }
    close(sock_cli);
    printf("Send over !!!\n");
    return 0;
}



你可能感兴趣的:(Linux,C)