利用多线程实现linux下C语言的聊天室程序:

来源:互联网 发布:内地网络悬疑电视剧 编辑:程序博客网 时间:2024/06/04 14:37

转载:http://www.360doc.com/content/16/0421/11/478627_552531090.shtml

利用多线程实现linux下C语言的聊天室程序:

客户端代码:

threadsend线程负责客户端消息的发送;

threadrecv线程负责客户端接受服务器端的消息。

[html] view plain copy
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <errno.h>  
  4. #include <string.h>  
  5. #include <unistd.h>  
  6. #include <netdb.h>  
  7. #include <sys/socket.h>  
  8. #include <netinet/in.h>  
  9. #include <sys/types.h>  
  10. #include <arpa/inet.h>  
  11. #include <pthread.h>  
  12.   
  13. #define MAXLINE 100;  
  14. void *threadsend(void *vargp);  
  15. void *threadrecv(void *vargp);  
  16.   
  17. int main()  
  18. {  
  19.   
  20. int *clientfdp;  
  21. clientfdp = (int *)malloc(sizeof(int));  
  22. *clientfdp = socket(AF_INET,SOCK_STREAM,0);  
  23. struct sockaddr_in serveraddr;  
  24. struct hostent *hp;  
  25. bzero((char *)&serveraddr,sizeof(serveraddr));  
  26. serveraddr.sin_family = AF_INET;  
  27. serveraddr.sin_port = htons(15636);  
  28. serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");  
  29. if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){  
  30.         printf("connect error\n");  
  31.         exit(1);  
  32. }  
  33.   
  34. pthread_t tid1,tid2;  
  35. printf("connected\n");  
  36. while(1){  
  37. pthread_create(&tid1,NULL,threadsend,clientfdp);  
  38.   
  39. pthread_create(&tid2,NULL,threadrecv,clientfdp);  
  40. }  
  41.   
  42. return EXIT_SUCCESS;  
  43. }  
  44.   
  45. void *threadsend(void * vargp)  
  46. {  
  47. //pthread_t tid2;  
  48. int connfd = *((int *)vargp);  
  49.   
  50. int idata;  
  51. char temp[100];  
  52. while(1){  
  53. //printf("me: \n ");  
  54. fgets(temp,100,stdin);  
  55. send(connfd,temp,100,0);  
  56. printf("          client send OK\n");  
  57.   
  58. }  
  59.   
  60.   
  61. printf("client send\n");  
  62. return NULL;  
  63. }  
  64.   
  65.   
  66. void *threadrecv(void *vargp)  
  67. {  
  68. char temp[100];  
  69. int connfd = *((int *)vargp);  
  70. while(1){  
  71. int idata = 0;  
  72. idata = recv(connfd,temp,100,0);  
  73. if(idata > 0){  
  74. printf("server :\n%s\n",temp);  
  75. }  
  76. }  
  77.   
  78.   
  79. return NULL;  
  80. }  

服务器端代码:

threadsend负责服务器端发送信息;

threadrecv负责接受客户端信息。

[html] view plain copy
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <errno.h>  
  4. #include <string.h>  
  5. #include <unistd.h>  
  6. #include <netdb.h>  
  7. #include <sys/socket.h>  
  8. #include <netinet/in.h>  
  9. #include <sys/types.h>  
  10. #include <arpa/inet.h>  
  11. #include <pthread.h>  
  12. #define PORT 15636  
  13. void *thread(void *vargp);  
  14. void *threadsend(void *vargp);  
  15. void *threadrecv(void *vargp);  
  16.   
  17. int main()  
  18. {  
  19.   
  20. int listenfd = socket(AF_INET, SOCK_STREAM,0);  
  21. if(listenfd < 0){  
  22.         perror("socket");  
  23.         exit(1);  
  24. }  
  25.   
  26. struct hostent *hp;  
  27. struct sockaddr_in serveraddr;  
  28. bzero((char *)&serveraddr,sizeof(serveraddr));  
  29. serveraddr.sin_family = AF_INET;  
  30. serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);  
  31. serveraddr.sin_port = htons(PORT);  
  32.   
  33. if(bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){  
  34.         perror("connect");  
  35.         exit(1);  
  36. }  
  37.   
  38. if(listen(listenfd,1024) < 0){  
  39.         perror("listen error");  
  40.         exit(1);  
  41. }  
  42.   
  43. //char temp[100];  
  44. struct sockaddr_in clientaddr;  
  45. int clientlen, *connfdp;  
  46. clientlen = sizeof(clientaddr);  
  47. while(1){  
  48. connfdp = (int *)malloc(sizeof(int));  
  49. *connfdp = accept(listenfd,(struct sockaddr *)&clientaddr, &clientlen);  
  50. pthread_t tid;  
  51. printf("Accepted!\n");  
  52. pthread_create(&tid,NULL,thread,connfdp);  
  53. }  
  54. EXIT_SUCCESS;  
  55. }  
  56.   
  57. void *thread(void *vargp)  
  58. {  
  59.   
  60. pthread_t tid1,tid2;  
  61. int connfd = *((int *)vargp);  
  62. int idata;  
  63. char temp[100];  
  64. pthread_create(&tid1,NULL,threadsend,vargp);  
  65. pthread_create(&tid2,NULL,threadrecv,vargp);  
  66. return NULL;  
  67. }  
  68.   
  69. void *threadsend(void * vargp)  
  70. {  
  71.   
  72. int connfd = *((int *)vargp);  
  73.   
  74. int idata;  
  75. char temp[100];  
  76. while(1){  
  77. //printf("server input:  ");  
  78. fgets(temp,100,stdin);  
  79. send(connfd,temp,100,0);  
  80. printf("        server send OK\n");  
  81. }  
  82. return NULL;  
  83. }  
  84.   
  85. void *threadrecv(void *vargp)  
  86. {  
  87. char temp[100];  
  88. int connfd = *((int *)vargp);  
  89. while(1){  
  90. int idata = 0;  
  91. idata = recv(connfd,temp,100,0);  
  92. if(idata > 0){  
  93. printf("client :\n%s\n",temp);  
  94. }  
  95. }  
  96. return NULL;  
  97. }  

问题:

linux下编译多线程代码时,shell提示找不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译:

gcc echoserver.c -lpthread -o echoserver

只要注意 -lpthread参数就可以了。


运行结果:

客户端:

[html] view plain copy
  1. [root@localhost unixIO]# ./echoclient  
  2. connected  
  3. hello!  
  4.           client send OK  
  5. goodmorning  
  6.           client send OK  
  7. server :  
  8. goodmorning too!  
  9.   
  10. server :  
  11. how r u?  
  12.   
  13. fine  
  14.           client send OK  

服务器端:


[html] view plain copy
  1. [root@localhost unixIO]# ./echoserver  
  2. Accepted!  
  3. client :  
  4. hello!  
  5.   
  6. client :  
  7. goodmorning  
  8.   
  9. goodmorning too!  
  10.         server send OK  
  11. how r u?  
  12.         server send OK  
  13. client :  
  14. fine  

原创粉丝点击