利用多线程实现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
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <pthread.h>
- #define MAXLINE 100;
- void *threadsend(void *vargp);
- void *threadrecv(void *vargp);
- int main()
- {
- int *clientfdp;
- clientfdp = (int *)malloc(sizeof(int));
- *clientfdp = socket(AF_INET,SOCK_STREAM,0);
- struct sockaddr_in serveraddr;
- struct hostent *hp;
- bzero((char *)&serveraddr,sizeof(serveraddr));
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_port = htons(15636);
- serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){
- printf("connect error\n");
- exit(1);
- }
- pthread_t tid1,tid2;
- printf("connected\n");
- while(1){
- pthread_create(&tid1,NULL,threadsend,clientfdp);
- pthread_create(&tid2,NULL,threadrecv,clientfdp);
- }
- return EXIT_SUCCESS;
- }
- void *threadsend(void * vargp)
- {
- //pthread_t tid2;
- int connfd = *((int *)vargp);
- int idata;
- char temp[100];
- while(1){
- //printf("me: \n ");
- fgets(temp,100,stdin);
- send(connfd,temp,100,0);
- printf(" client send OK\n");
- }
- printf("client send\n");
- return NULL;
- }
- void *threadrecv(void *vargp)
- {
- char temp[100];
- int connfd = *((int *)vargp);
- while(1){
- int idata = 0;
- idata = recv(connfd,temp,100,0);
- if(idata > 0){
- printf("server :\n%s\n",temp);
- }
- }
- return NULL;
- }
服务器端代码:
threadsend负责服务器端发送信息;
threadrecv负责接受客户端信息。
[html] view plain copy
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <pthread.h>
- #define PORT 15636
- void *thread(void *vargp);
- void *threadsend(void *vargp);
- void *threadrecv(void *vargp);
- int main()
- {
- int listenfd = socket(AF_INET, SOCK_STREAM,0);
- if(listenfd < 0){
- perror("socket");
- exit(1);
- }
- struct hostent *hp;
- struct sockaddr_in serveraddr;
- bzero((char *)&serveraddr,sizeof(serveraddr));
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
- serveraddr.sin_port = htons(PORT);
- if(bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){
- perror("connect");
- exit(1);
- }
- if(listen(listenfd,1024) < 0){
- perror("listen error");
- exit(1);
- }
- //char temp[100];
- struct sockaddr_in clientaddr;
- int clientlen, *connfdp;
- clientlen = sizeof(clientaddr);
- while(1){
- connfdp = (int *)malloc(sizeof(int));
- *connfdp = accept(listenfd,(struct sockaddr *)&clientaddr, &clientlen);
- pthread_t tid;
- printf("Accepted!\n");
- pthread_create(&tid,NULL,thread,connfdp);
- }
- EXIT_SUCCESS;
- }
- void *thread(void *vargp)
- {
- pthread_t tid1,tid2;
- int connfd = *((int *)vargp);
- int idata;
- char temp[100];
- pthread_create(&tid1,NULL,threadsend,vargp);
- pthread_create(&tid2,NULL,threadrecv,vargp);
- return NULL;
- }
- void *threadsend(void * vargp)
- {
- int connfd = *((int *)vargp);
- int idata;
- char temp[100];
- while(1){
- //printf("server input: ");
- fgets(temp,100,stdin);
- send(connfd,temp,100,0);
- printf(" server send OK\n");
- }
- return NULL;
- }
- void *threadrecv(void *vargp)
- {
- char temp[100];
- int connfd = *((int *)vargp);
- while(1){
- int idata = 0;
- idata = recv(connfd,temp,100,0);
- if(idata > 0){
- printf("client :\n%s\n",temp);
- }
- }
- return NULL;
- }
问题:
linux下编译多线程代码时,shell提示找不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译:
gcc echoserver.c -lpthread -o echoserver
只要注意 -lpthread参数就可以了。
运行结果:
客户端:
[html] view plain copy
- [root@localhost unixIO]# ./echoclient
- connected
- hello!
- client send OK
- goodmorning
- client send OK
- server :
- goodmorning too!
- server :
- how r u?
- fine
- client send OK
服务器端:
[html] view plain copy
- [root@localhost unixIO]# ./echoserver
- Accepted!
- client :
- hello!
- client :
- goodmorning
- goodmorning too!
- server send OK
- how r u?
- server send OK
- client :
- fine
阅读全文
0 0
- 利用多线程实现linux下C语言的聊天室程序:
- 【C语言】linux下简单的聊天室程序(TCP+多线程)
- Linux下C实现的聊天室
- Linux下C实现的聊天室
- Linux下C实现的聊天室
- Linux下基于socket和多线程的聊天室小程序
- Linux下基于socket和多线程的聊天室小程序
- Linux下C语言多线程小程序
- Linux下利用命名管道实现简单的聊天室
- Linux下C语言项目—聊天室的搭建1.0
- 利用select函数实现在Linux环境下实现一个聊天室程序
- Linux c语言 聊天室
- Linux下的C语言多线程编程
- Linux下的C语言多线程编程
- Linux下C语言的多线程编程
- linux下c语言的多线程编程
- Linux下C语言多线程网络通信简单聊天程序
- Linux下C语言多线程,网络通信简单聊天程序
- android WebView 简单使用
- 简单入门小程序03
- 顺序表应用8:最大子段和之动态规划法
- Linux下Samba服务配置笔记
- 顺序表应用7:最大子段和之分治递归法
- 利用多线程实现linux下C语言的聊天室程序:
- jQuery--7.效果
- next_permutation(全排列算法)
- POJ 2262
- 数据结构在线测试一二章
- Oracle创建新表:如果存在则删除重建
- 5分钟使用Unity制作AR应用
- Tomcat添加Apr后,http正常访问,https不能访问
- Service