linux socket实现网络聊天室(二):使用线程独立收发消息

24 篇文章 0 订阅

一、引言

二、代码实现

1、服务器端

[cpp]  view plain copy
  1. /*tcpserver.c  2011.9.1 by yyg*/  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <errno.h>  
  5. #include <string.h>  
  6. #include <sys/types.h>  
  7. #include <netinet/in.h>  
  8. #include <sys/socket.h>  
  9. #include <sys/wait.h>  
  10. #include <pthread.h>  
  11.   
  12. #define MYPORT 3490  //定义端口  
  13. #define BACKLOG 10  
  14. #define MAXDATASIZE 1024  
  15.   
  16. int sockfd,new_fd;  
  17. pthread_t accthread,recthread;  
  18.   
  19. void recmessage(void){  //接收客户端信息函数  
  20.     while(1){  
  21.         int numbytes;  
  22.         char buf[MAXDATASIZE];  
  23.         if((numbytes = recv(new_fd,buf,MAXDATASIZE,0))==-1){  
  24.             perror("recv");  
  25.             exit(1);  
  26.         }  
  27.         buf[numbytes]='\0';  
  28.         if(strcmp(buf,"exit")==0){  //若收到的是exit字符,则代表退出通信  
  29.             printf("Client is closed\n");  
  30.             close(new_fd);  
  31.             close(sockfd);  
  32.             exit(1);  
  33.         }  
  34.         printf("client:%s\n",buf);  
  35.     }/*while*/  
  36. }  
  37.   
  38. void acceptconnect(void){  //接受客户端连接请求函数  
  39.     struct sockaddr_in their_addr;  
  40.     int sin_size;  
  41.     sin_size = sizeof(struct sockaddr_in);  
  42.     if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size))==-1){  
  43.         perror("accept");  
  44.         exit(1);  
  45.     }  
  46.     printf("server:got connection from %s\n",inet_ntoa(their_addr.sin_addr));  
  47.     /*创建子线程,用于接收信息*/  
  48.     if((pthread_create(&recthread,NULL,(void*)recmessage,NULL)) != 0){  
  49.         printf("create thread error!\r\n");  
  50.         exit(1);  
  51.     }  
  52. }  
  53.   
  54. int main(void){  
  55.     struct sockaddr_in my_addr;  
  56.     /*创建套接字*/  
  57.     if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){  
  58.         perror("socket");  
  59.         exit(1);  
  60.     }  
  61.     /*初始化sockaddr_in结构体相关参数*/  
  62.     my_addr.sin_family = AF_INET;  
  63.     my_addr.sin_port = htons(MYPORT);  
  64.     my_addr.sin_addr.s_addr = INADDR_ANY;  
  65.     bzero(&(my_addr.sin_zero),8);  
  66.   
  67.     /*绑定端口与套接字*/  
  68.     if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)) == -1){  
  69.         perror("bind");  
  70.         exit(1);  
  71.     }  
  72.     /*监听客户端套接字*/  
  73.     if(listen(sockfd,BACKLOG)== -1){  
  74.         perror("listen");  
  75.         exit(1);  
  76.     }  
  77.     printf("listening...\n");  
  78.     /*创建子线程,用于接收信息*/  
  79.     if((pthread_create(&accthread,NULL,(void*)acceptconnect,NULL))!=0){  
  80.         printf("create thread error!\n");  
  81.         exit(1);  
  82.     }  
  83.     while(1){  
  84.         char msg[MAXDATASIZE];  
  85.         scanf("%s",msg);  
  86.         if(send(new_fd,msg,strlen(msg),0) == -1){  //发送信息,与客户端交流  
  87.             perror("send");  
  88.             exit(1);  
  89.         }  
  90.         if(strcmp(msg,"exit") ==0){  
  91.             printf("byebye\n");  
  92.             close(new_fd);  
  93.             close(sockfd);  
  94.             exit(1);  
  95.         }  
  96.     }/*while*/  
  97.     return 0;  
  98. }/*main*/  

2.客户端

[cpp]  view plain copy
  1. /*tcpclient.c    2010.9.1 by yyg*/  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <errno.h>  
  5. #include <string.h>  
  6. #include <sys/types.h>  
  7. #include <netinet/in.h>  
  8. #include <sys/socket.h>  
  9. #include <sys/wait.h>  
  10. #include <pthread.h>  
  11. #include <netdb.h>  
  12.   
  13. #define PORT 3490  
  14. #define BACKLOG 10  
  15. #define MAXDATASIZE 1024  
  16.   
  17. int sockfd;  
  18. pthread_t recthread;  
  19.   
  20. /*接收信息函数*/  
  21. void recmessage(void){  
  22.     while(1){  
  23.         int numbytes;  
  24.         char buf[MAXDATASIZE];  
  25.           
  26.         if((numbytes = recv(sockfd,buf,MAXDATASIZE,0))==-1){  
  27.             perror("recv");  
  28.             exit(1);  
  29.         }  
  30.         buf[numbytes]='\0';  
  31.         if(strcmp(buf,"exit")==0){  
  32.             printf("Server is closed\n");  
  33.             close(sockfd);  
  34.             exit(1);  
  35.         }  
  36.         printf("Server:%s\n",buf);  
  37.     }/*while*/  
  38. }  
  39.   
  40. int main(int argc,char *argv[]){  
  41.     struct hostent *he;  
  42.     struct sockaddr_in their_addr;  
  43.     /*客户端输入方式:./client 172.31.100.236,若无输入后面IP地址,会提示错误*/  
  44.     if(argc != 2){  
  45.         fprintf(stderr,"usage:client hostname\n");  
  46.         exit(1);  
  47.     }  
  48.     /*获取主机IP地址*/  
  49.     if((he = gethostbyname(argv[1])) == NULL){  
  50.         herror("gethostbyname");  
  51.         exit(1);  
  52.     }  
  53.     /*创建套接字*/  
  54.     if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){  
  55.         perror("socket");  
  56.         exit(1);  
  57.     }  
  58.     /*初始化sockaddr_in结构体*/  
  59.     their_addr.sin_family = AF_INET;  
  60.     their_addr.sin_port = htons(PORT);  
  61.     their_addr.sin_addr = *((struct in_addr *)he->h_addr);  
  62.     bzero(&(their_addr.sin_zero),8);  
  63.     /*向服务器发送连接请求*/  
  64.     if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1){  
  65.         perror("connect");  
  66.         exit(1);  
  67.     }  
  68.     /*创建子线程,用于接收信息*/  
  69.     if((pthread_create(&recthread,NULL,(void*)recmessage,NULL))!= 0){  
  70.         printf("create thread error!\r\n");  
  71.         exit(1);  
  72.     }  
  73.     /*发送信息。接收发送信息用的是同一端口,都 是sockfd*/  
  74.     while(1){  
  75.         char msg[MAXDATASIZE];  
  76.         scanf("%s",msg);  
  77.         if(send(sockfd,msg,strlen(msg),0) == -1){  
  78.             perror("send");  
  79.             exit(1);  
  80.         }  
  81.         if(strcmp(msg,"exit") ==0){  
  82.             printf("byebye\n");  
  83.             close(sockfd);  
  84.             exit(1);  
  85.         }  
  86.     }/*while*/  
  87.     return 0;  
  88. }  

运行结果:

终端1:

[cpp]  view plain copy
  1. [root@localhost net]# ./tcpserver  
  2. server:got connection from 172.31.100.236  
  3. Hello,World!   
  4. client:hello  
  5. client:xiaolian  
  6. client:iou  
  7. ok  
  8. exit  
  9. byebye  

终端2:

[cpp]  view plain copy
  1. [root@localhost net]# ./tcpclient 172.31.100.236  
  2. Server:Hello,World!  
  3. hello  
  4. xiaolian iou  
  5. Server:ok  
  6. Server is closed  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值