服务器和客户端的通信

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Zhuqiupei/article/details/78782345

服务器

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>

#define PORT   8888

int main()
{
    int sockfd, ret, fd;
    char buf[100] = {0};
    struct sockaddr_in server_addr;                //用于存放服务器本身的信息,包括自己的port和ip
    struct sockaddr_in client_addr;                //接受客户端连接的时候, 用于存放客户端信息

    printf("Start Server!\n");
    sockfd = socket(PF_INET, SOCK_STREAM, 0);      //创建socket,处理客户端的连接,不用于发送信息
    if (-1 == sockfd)
    {
        perror("socket");
        exit(1);
    }

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = PF_INET;                               //地址族
    server_addr.sin_port = PORT;                                    //指定socket的端口号
    server_addr.sin_addr.s_addr = inet_addr("192.168.1.111");       //本机ip

    ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));   //上述信息绑定到socket
    if (-1 == ret)
    {
        perror("bind");
        exit(1);
    }

    printf("Listening...\n");
    ret = listen(sockfd, 5);                               //监听,是否有客户端发起连接
    if(-1 == ret)
    {
        perror("listen");
        exit(1);
    }

    int length = sizeof(client_addr);
    fd = accept(sockfd, (struct sockaddr *)&client_addr, &length);//接受客户端的连接,返回值用于发送信息
    if (-1 == fd)
    {
        perror("accept");
        exit(1);
    }

    printf("Accept %d , Port %d\n", fd, client_addr.sin_port);

    while (1)
    {
        ret = recv(fd, buf, sizeof(buf), 0);             //接收消息,从fd里面接收
        if (-1 == ret)
        {
            perror("recv");
            exit(1);
        }
        if (!strcmp(buf, "bye"))
        {
            break;
        }
        printf("Recv From Client : %s\n", buf);
        memset(buf, 0, sizeof(buf));
    }

    close(fd);
    close(sockfd);

    return 0;
}

客户端

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT    8888

int main()
{
    int sockfd, ret;
    struct sockaddr_in server_addr;              //向server_addr发起连接
    char buf[100] = {0};

    sockfd = socket(PF_INET, SOCK_STREAM, 0);    //创建socket,既用于连接,又用于发送消息
    if (-1 == sockfd)
    {
        perror("socket");
        exit(1);
    }

    memset(&server_addr, 0, sizeof(server_addr));     //都是服务器的信息
    server_addr.sin_family = PF_INET;
    server_addr.sin_port = PORT;
    server_addr.sin_addr.s_addr = inet_addr("192.168.1.111");   

    printf("Start Connecting...\n");
    ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));//发起连接
    if (-1 == ret)
    {
        perror("connect");
        exit(1);
    }

    while (1)
    {
        scanf("%s", buf);

        ret = send(sockfd, buf, strlen(buf), 0);      //发送消息
        if (-1 == ret)
        {
            perror("send");
            exit(1);
        }

        if (!strcmp(buf, "bye"))
        {
            break;
        }

        memset(buf, 0, sizeof(buf));
    }

    close(sockfd);

    return 0;
}
展开阅读全文

没有更多推荐了,返回首页