• socket结合线程的测试demo


    //server.c
    #include
    #include 
    #include           
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define MAX_QUEUE   (10)
    /*
    #include 
    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                              void *(*start_routine) (void *), void *arg);
    int pthread_join(pthread_t thread, void **retval);
    
    */
    
    
    /*
    #include           
    #include 
    int socket(int domain, int type, int protocol);
    int bind(int sockfd, const struct sockaddr *addr,
                    socklen_t addrlen);
    int listen(int sockfd, int backlog);
    int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
    
    struct sockaddr_in{    
        sa_family_t     sin_family;   //地址族(Address Family),也就是地址类型    
        uint16_t        sin_port;     //16位的端口号    
        struct in_addr  sin_addr;     //32位IP地址    
        char            sin_zero[8];  //不使用,一般用0填充
    };
    
    
    */
    
    /*
    int connect(int sockfd, const struct sockaddr *addr,
                       socklen_t addrlen);
    
    
    */
    
    static pthread_t t1 = -1;
    static int running = 0;
    char s_buf[128] = {0};
    char *message = "hi,I am a server";
    void *server_func (void *)
    {
        socklen_t sock_len_1;
        int n_read;
        int n_write;
            
        //1、socket
        int s_fd = socket(AF_INET,SOCK_STREAM,0);//ipv4 tcp 
        if(-1 == s_fd)
        {
            perror("socket");
            return NULL;
        }
            
        //2、bind
        struct sockaddr_in s_addr;
        struct sockaddr_in c_addr;
        memset(&s_addr,0,sizeof(struct sockaddr_in));    //一般来说先清空空间数据,再配置。避免结构体里面有杂乱数据
        memset(&c_addr,0,sizeof(struct sockaddr_in));
        s_addr.sin_family = AF_INET;//AF_INET(又称 PF_INET)是 IPv4 网络协议的套接字类型
        s_addr.sin_port = htons(8989);
        inet_aton("192.168.175.129",&s_addr.sin_addr);      
        if(bind(s_fd, (struct sockaddr *)&s_addr,sizeof(s_addr)))
        {
            perror("bind");
            return NULL;
        }
        //3、listen
        if(listen(s_fd,MAX_QUEUE))
        {
            perror("listen");
            return NULL;
        }
        //4、accept
        sock_len_1 = sizeof(c_addr);
        int c_fd = accept(s_fd, (struct sockaddr *)&c_addr, &sock_len_1);
        if(-1 == c_fd)
        {
            perror("accept");
            return NULL; 
        }
        printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
        while(running)
        {
            printf("in thread\n");
            //5、read
            n_read = read(c_fd,s_buf,128);
            if(-1 == n_read)
            {
                perror("read");
                return NULL;
            }
            printf("read_buf = %s\n",s_buf);
            //6、write
            n_write = write(c_fd,message,strlen(message));
            if(-1 == n_read)
            {
                perror("read");
                return NULL;
            }
        }
    }
    int main()
    {
        running =1;
        int ret = pthread_create(&t1,NULL,server_func,NULL);
        if(ret)
            return -1;
    
        pthread_join(t1,NULL);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    //client.c
    #include
    #include 
    #include           
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define MAX_QUEUE   (10)
    /*
    #include 
    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                              void *(*start_routine) (void *), void *arg);
    int pthread_join(pthread_t thread, void **retval);
    
    */
    
    
    /*
    #include           
    #include 
    int socket(int domain, int type, int protocol);
    int bind(int sockfd, const struct sockaddr *addr,
                    socklen_t addrlen);
    int listen(int sockfd, int backlog);
    int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
    
    struct sockaddr_in{    
        sa_family_t     sin_family;   //地址族(Address Family),也就是地址类型    
        uint16_t        sin_port;     //16位的端口号    
        struct in_addr  sin_addr;     //32位IP地址    
        char            sin_zero[8];  //不使用,一般用0填充
    };
    
    
    */
    
    /*
    int connect(int sockfd, const struct sockaddr *addr,
                       socklen_t addrlen);
    
    
    */
    
    static pthread_t t1 = -1;
    static int running = 0;
    char c_buf[256] = {0};
    char *message = "hello johan";
    void *client_func (void *)
    {
        socklen_t sock_len_1;
        int n_read;
        int n_write;
    
        
        //1、socket
        int c_fd = socket(AF_INET,SOCK_STREAM,0);//ipv4 tcp 
        if(-1 == c_fd)
        {
            perror("socket");
            return NULL;
        }
            
        //2、connect
        struct sockaddr_in c_addr;
        memset(&c_addr,0,sizeof(struct sockaddr_in));
        c_addr.sin_family = AF_INET;//AF_INET(又称 PF_INET)是 IPv4 网络协议的套接字类型
        c_addr.sin_port = htons(8989);
        inet_aton("192.168.175.129",&c_addr.sin_addr);      
        if(connect(c_fd, (struct sockaddr *)&c_addr,sizeof(c_addr)))
        {
            perror("connect");
            return NULL;
        }
    
        while(running)
        {
            printf("in thread\n");
            //3、write
            n_write = write(c_fd,message,strlen(message));
            if(-1 == n_read)
            {
                perror("read");
                return NULL;
            }
       
            //4、read
            n_read = read(c_fd,c_buf,256);
            if(-1 == n_read)
            {
                perror("read");
                return NULL;
            }
            printf("read_buf = %s\n",c_buf);
            
        }
    }
    int main()
    {
        running =1;
        int ret = pthread_create(&t1,NULL,client_func,NULL);
        if(ret)
            return -1;
    
        pthread_join(t1,NULL);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    Python学习笔记第二十九天(N维数组(ndarray))
    day42 6-5 springMVC调度器、ModelAndView、配置thymeleaf模板引擎 & 6-6 thymeleaf语法 & 6-7 springMVC拦截器 & 6-8 设置请求编码过滤器Filter
    Java关于数组
    雪花算法基本原理与实现
    集火全屋智能“后装市场”,真正玩得转的没几个
    我的第一个NPM包:panghu-planebattle-esm(胖虎飞机大战)使用说明
    华为OD机试 - ABR 车路协同场景 - (Java 2023 B卷 100分)
    字节跳动小程序开发:探索创新的数字化世界
    算法竞赛进阶指南 0x68 二分图的匹配
    Euclidean Distance Transform - EDT
  • 原文地址:https://blog.csdn.net/mantouyouyou/article/details/133082251