zoukankan      html  css  js  c++  java
  • linux下基于socket多线程并发通信的实现

    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
    [cpp] view plain copy print?
    /*************************************************** 
    * 文件名:pthread_server.c 
    * 文件描述:创建子线程来接收客户端的数据 
    ***************************************************/  
    #include <sys/types.h>   
    #include <sys/socket.h>   
    #include <stdio.h>   
    #include <netinet/in.h>   
    #include <arpa/inet.h>   
    #include <unistd.h>   
    #include <stdlib.h>   
    #include <pthread.h>   
    void *rec_data(void *fd);   
    int main(int argc,char *argv[])   
    {   
           int server_sockfd;   
        int *client_sockfd;   
           int server_len, client_len;   
           struct sockaddr_in server_address;   
           struct sockaddr_in client_address;   
           struct sockaddr_in tempaddr;   
           int i,byte;   
           char char_recv,char_send;   
           socklen_t templen;   
           server_sockfd = socket(AF_INET, SOCK_STREAM, 0);//创建套接字   
     
           server_address.sin_family = AF_INET;   
           server_address.sin_addr.s_addr =  htonl(INADDR_ANY);   
           server_address.sin_port = htons(9734);   
           server_len = sizeof(server_address);   
     
           bind(server_sockfd, (struct sockaddr *)&server_address, server_len);//绑定套接字   
           templen = sizeof(struct sockaddr);   
     
           printf("server waiting for connect/n");   
           while(1){   
                  pthread_t thread;//创建不同的子线程以区别不同的客户端   
                  client_sockfd = (int *)malloc(sizeof(int));   
                  client_len = sizeof(client_address);   
                  *client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, (socklen_t *)&client_len);   
                  if(-1==*client_sockfd){   
                         perror("accept");   
                         continue;   
                  }   
                  if(pthread_create(&thread, NULL, rec_data, client_sockfd)!=0)//创建子线程   
                  {   
                         perror("pthread_create");   
                         break;   
                  }   
           }   
           shutdown(*client_sockfd,2);   
           shutdown(server_sockfd,2);   
    }   
    /***************************************** 
    * 函数名称:rec_data 
    * 功能描述:接受客户端的数据 
    * 参数列表:fd——连接套接字 
    * 返回结果:void 
    *****************************************/  
    void *rec_data(void *fd)   
    {   
           int client_sockfd;   
           int i,byte;   
           char char_recv[100];//存放数据   
           client_sockfd=*((int*)fd);   
           for(;;)   
           {   
                  if((byte=recv(client_sockfd,char_recv,100,0))==-1)   
                  {   
                         perror("recv");   
                         exit(EXIT_FAILURE);    
                  }   
                  if(strcmp(char_recv, "exit")==0)//接受到exit时,跳出循环   
                         break;   
                  printf("receive from client is %s/n",char_recv);//打印收到的数据   
           }   
           free(fd);   
           close(client_sockfd);   
           pthread_exit(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
    [cpp] view plain copy print?
    /*************************************************** 
    * 文件名:pthread_client.c 
    * 文件描述:创建子线程来接收客户端的数据 
    ***************************************************/  
    #include <sys/types.h>   
    #include <sys/socket.h>   
    #include <stdio.h>   
    #include <netinet/in.h>   
    #include <arpa/inet.h>   
    #include <unistd.h>   
    #include <stdlib.h>   
    int main(int argc,char *argv[])   
    {   
           int sockfd;   
           int len;   
           struct sockaddr_in address;      
           int result;   
           int i,byte;   
           char char_send[100] = { 0 };   
        if((sockfd = socket(AF_INET, SOCK_STREAM, 0))==-1)   
           {   
                  perror("socket");   
                  exit(EXIT_FAILURE);   
           }   
        if(argc != 3){   
          printf("Usage: fileclient <address> <port>/n");//用法:文件名 服务器IP地址 服务器端口地址   
          return 0;   
       }   
       if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){   
           perror("sock");   
           exit(1);   
       }   
       bzero(&address,sizeof(address));   
       address.sin_family = AF_INET;   
       address.sin_port = htons(atoi(argv[2]));   
       inet_pton(AF_INET,argv[1],&address.sin_addr);   
    len = sizeof(address);   
     
        if((result = connect(sockfd, (struct sockaddr *)&address, len))==-1)   
           {   
                  perror("connect");   
                  exit(EXIT_FAILURE);   
           }   
     
           for(;;)   
           {   
                 scanf("%s", char_send);//输入发送数据   
                  fflush(stdin);//清除输入缓存   
                  if(strcmp(char_send, "exit")==0){//如果输入exit,跳出循环   
                         if((byte=send(sockfd,char_send,100,0))==-1)   
                         {   
                                perror("send");   
                                exit(EXIT_FAILURE);   
                         }              
                         break;   
                  }                     
                  if((byte=send(sockfd,char_send,100,0))==-1)   
                  {   
                         perror("send");   
                         exit(EXIT_FAILURE);   
                  }   
           }   
        close(sockfd);   
        exit(0);   
    }
  • 相关阅读:
    Redis设置过期时间
    javascript 进制转换(2进制、8进制、10进制、16进制之间的转换)
    123456---com.twoapp.fengkuangqiuqiu---疯狂球球
    123456---com.twoapp.taiKongLvXing---太空旅行
    123457---com.twoapp.shuXueYouXi---小学数学口算
    123456---com.twoapp.huanYingMotro--- 幻影摩托
    123456---com.twoapp.xiaoxiaofeixingyuan---小小飞行员
    123456---com.twoapp.xiaoxiaozuqiujiang---小小足球将
    123457---脑筋急转弯01--com.threeObj03.JiZhuanWan
    123457---小小数学家--com.twoapp.xiaoxiaoshuxuejia
  • 原文地址:https://www.cnblogs.com/yjds/p/8597369.html
Copyright © 2011-2022 走看看