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);
}
//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);
}

socket结合线程的测试demo_第1张图片
socket结合线程的测试demo_第2张图片

你可能感兴趣的:(Linux系统编程,linux,c语言)