linux-下用c语言编写的聊天室程序-服务器和客户端.doc
《linux-下用c语言编写的聊天室程序-服务器和客户端.doc》由会员分享,可在线阅读,更多相关《linux-下用c语言编写的聊天室程序-服务器和客户端.doc(70页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-datelinux-下用c语言编写的聊天室程序-服务器和客户端linux-下用c语言编写的聊天室程序-服务器和客户端/* * server.c * * Created on: 2012-6-15 * Author: root */#include #include #include #include #include #include #include #include #i
2、nclude#define PORT 7999#define MAX_NUM 3 /client连接最大个数#define MAX_CLIENT 15#define MAX_SIZE 1024pthread_rwlock_t idx_lock, wait_lock;/client 信息typedef struct _client int sockfd;char name20;pthread_t pid;int flg; c_client;c_client clientMAX_CLIENT;/定义client;/等待的clientstruct _client_ int sockfd;char n
3、ame20;pthread_t pid;struct _client_ *next;typedef struct _client_ c_client_c;c_client_c *head = NULL;c_client_c *temp_c1 = NULL, *temp_c2 = NULL;/等待的/初始化client信息void init_client() int i = 0;for (i = 0; i MAX_CLIENT; i+) clienti.sockfd = -1;memset(clienti.name, 0, 20);clienti.pid = -1;clienti.flg = -
4、1;/查找结构体数组中sockfd为-1的下标值int find_fd(c_client *client) int i = 0;while (i sockfd & i MAX_NUM) if (strcmp(clienti.name, p) = 0)return 0;i+;return 1;/SHOWvoid showuser(c_client *p_client) int i = 0;char buf1024 = 0 ;strcpy(buf, 200rn);for (i = 0; i sockfd, buf, strlen(buf), 0);/ALLvoid sendto_all(c_cli
5、ent *p_client, char *buf) int i = 0;char sendbuf1024 = 0 ;sprintf(sendbuf, AFROMrn%srn%s, p_client-name, buf + 5);for (i = 0; i MAX_NUM; i+) if (clienti.sockfd != -1 & clienti.flg != -1)if(send(clienti.sockfd, sendbuf, strlen(sendbuf), 0) = 0)printf(send errrrrrn);exit(1);int findname(char *name) in
6、t i = 0;for (i = 0; i sockfd, sendbuf, strlen(sendbuf), 0); else sprintf(sendbuf, FROMrn%srn%s, p_client-name, buf + 4 + strlen(name) + 2);if(send(sock, sendbuf, strlen(sendbuf), 0)name);close(p_client-sockfd);p_client-sockfd = -1;p_client-pid = -1;p_client-flg = -1;sprintf(buf,NOTICE1rn%s退出聊天室rnrn,
7、p_client-name);memset(p_client-name,0,20);for(i=0;inext != NULL)memset(buf,0,1024);pthread_rwlock_rdlock(&idx_lock);idx = find_fd(client);pthread_rwlock_unlock(&idx_lock);clientidx.sockfd = head-next-sockfd;pthread_rwlock_wrlock(&wait_lock);temp = head-next;head-next = head-next-next;free(temp);pthr
8、ead_rwlock_unlock(&wait_lock);sprintf(buf,NOTICErn您已被唤醒,请继续操作rnrn);send(clientidx.sockfd,buf,strlen(buf),0);if (pthread_create(&clientidx.pid, NULL, (void *)pthread_fun,(void *) &clientidx) != 0) perror(pthread_create);exit(1);pthread_detach(clientidx.pid);void pthread_fun(void* cclient) c_client *p
9、_client = (c_client *) cclient;char bufMAX_SIZE = 0 ;char sendbuf1024 = 0 ;int i, n;char *p;sprintf(sendbuf, %s, NOTICErn通讯通道开启rnrn);if (send(p_client-sockfd, sendbuf, strlen(sendbuf), 0) sockfd, buf, sizeof(buf) - 1, MSG_NOSIGNAL);if (n sockfd);p_client-sockfd = -1;break;if (logform(buf) if (cmpnam
10、e(buf, p_client) = 0) send(p_client-sockfd, ERRORrn用户名重复rnrn, 26, 0);continue; else p_client-flg = 1;p = strtok(buf + 7, rnrn);strcpy(p_client-name, p);sprintf(sendbuf, 100rn%srnrn, p_client-name);send(p_client-sockfd, sendbuf, sizeof(sendbuf), 0);printf(%s进入聊天室n, p_client-name);for (i = 0; i sockfd
11、 & clienti.flg != -1)send(clienti.sockfd, sendbuf, sizeof(sendbuf), 0);memset(sendbuf, 0, 1024);while (1) memset(buf, 0, MAX_SIZE);if (n = recv(p_client-sockfd, buf, MAX_SIZE, 0) sockfd, ERRORrn信息不符合协议要求rnrn,38, 0); else send(p_client-sockfd, ERRORrn未登录,请您登录再进行其他操作rnrn, 56, 0);pthread_exit(NULL);int
12、 main() int ser_sockfd, clt_sockfd;struct sockaddr_in addr;int idx;char buf1024 = 0 ;/pthread_rwlock_t idx_lock,wait_lock;pthread_rwlock_init(&idx_lock, NULL);pthread_rwlock_init(&wait_lock, NULL);init_client();/创建服务器sockfdif (ser_sockfd = socket(AF_INET, SOCK_STREAM, 0) = -1) perror(socket);exit(1)
13、;/设置服务器网络地址bzero(&addr, sizeof(addr);addr.sin_family = AF_INET;addr.sin_port = htons(PORT);addr.sin_addr.s_addr = htonl(INADDR_ANY);/设置端口可重用int opt = 1;setsockopt(ser_sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt);/将套接字绑定到服务器的网络地址上if (bind(ser_sockfd, (struct sockaddr*)&addr, sizeof(addr) = -1)
14、 perror(bind);exit(1);printf(bind successn);/监听连接请求-监听队列长度为10if (listen(ser_sockfd, 10) = -1) perror(listen);exit(1);printf(listen successn);while (1) if (clt_sockfd = accept(ser_sockfd, NULL, NULL) = -1) perror(accept);exit(1);pthread_rwlock_rdlock(&idx_lock);idx = find_fd(client);/printf(idx=%dn,i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- linux 语言 编写 聊天室 程序 服务器 客户端
限制150内