Linux嵌入式c语言聊天室,linux-下用c语言编写的聊天室程序-服务器和客户端

《linux-下用c语言编写的聊天室程序-服务器和客户端》由会员分享,可在线阅读,更多相关《linux-下用c语言编写的聊天室程序-服务器和客户端(14页珍藏版)》请在人人文库网上搜索。

1、* server.c* Created on: 2012-6-15* Author: root*/#include #include #include #include #include #include #include #include #include#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;ch。

2、ar name20;pthread_t pid;int flg; c_client;c_client clientMAX_CLIENT;/定义 client;/ 等待的 client struct _client_ int sockfd;char name20; 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 ini。

3、t_client() int i = 0;for (i = 0; i sockfd & i sockfd, buf, strlen(buf), 0);/ALLvoid sendto_all(c_client *p_client, char *buf) int i = 0;char sendbuf1024 = 0 ;sprintf(sendbuf, AFROMrn%srn%s, p_client-name, buf + 5); for (i = 0; i sockfd, sendbuf, strlen(sendbuf), 0); else sprintf(sendbuf, FROMrn%srn%。

4、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,p_client-name);memset(p_client-name,0,20);for(i=0;inext != NULL)memset(buf,0,1024);pthread_。

5、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);pthread_rwlock_unlock(&wait_lock);sprintf(buf,NOTICErn您已被唤醒 ,请继续操作 rnrn);send(clientidx.sockfd,。

6、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_client = (c_client *) cclient;char bufMAX_SIZE = 0 ;char sendbuf1024 = 0 ;int i, n;char *p;spr。

7、intf(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 (cmpname(buf, p_client) = 0) send(p_client-sockfd, ERRORrn 用户名重复 rnrn, 26, 0); continue; else p_cl。

8、ient-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 & clienti.flg != -1)send(clienti.sockfd, sendbuf, sizeof(sendbuf), 0);memset(sendbuf, 。

9、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 main() int ser_sockfd, clt_sockfd;struct sockaddr_in addr;int idx;char buf1024 。

10、= 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);/ 设置服务器网络地址bzero(&addr, sizeof(addr);addr.sin_family = AF_INET;addr.sin_p。

11、ort = 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) perror(bind);exit(1);printf(bind successn);/ 监听连接请求 - 监听队列长度为 10 if (l。

12、isten(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,idx);pthread_rwlock_unlock(&idx_lock);if (idx != -1) /连接末满client。

13、idx.sockfd = clt_sockfd;if (pthread_create(&clientidx.pid, NULL,(void *) pthread_fun, (void *)&clientidx) != 0) perror(pthread_create);exit(1);pthread_detach(clientidx.pid); else / 连接已满 ,等待temp_c1 = (c_client_c *) malloc(sizeof(c_client_c);temp_c1-sockfd = clt_sockfd;temp_c1-next = NULL;pthread_rwlo。

14、ck_wrlock(&wait_lock);if (head = NULL) head = (c_client_c *) malloc(sizeof(c_client_c);head-next = temp_c1; else for(temp_c2=head;temp_c2-next!=NULL;temp_c2=temp_c2-next);temp_c2-next = temp_c1;pthread_rwlock_unlock(&wait_lock);memset(buf, 0, 1024);sprintf(buf, NOTICErn 服务器已满 ,请等候 rnrn);/ 客户端接受 则等待 。

15、if (send(temp_c1-sockfd, buf, strlen(buf), 0) #include #include #include #include #include #include #include #include #include #include #define MAX_SIZE 1024#define PORT 7999static int FLAGE = -1;char name20 = 0;void fun_show(int sockfd)char sendbuf256 = 0;sprintf(sendbuf,SHOWrnrn);if(send(sockfd,se。

16、ndbuf,strlen(sendbuf),0) 0)usleep(100);/ 让子进程先运行while(FLAGE=0)printf( 服务器已满 ,wait.n);sleep(1);tcflush(0,TCIFLUSH);while(FLAGE=2) / 当 FLAGE 2 时正好父进程运行,则等待 ,FLAGE=1 usleep(100);if(FLAGE = 1)while(k3)/fflush(stdin);memset(name,0,20);printf(login:);scanf(%s,name);/fflush(stdin);sprintf(sendbuf,LOGINrn%s。

17、rnrn,name);send(sockfd,sendbuf,strlen(sendbuf),0);memset(sendbuf,0,1024);for(;)if(FLAGE=3 | FLAGE=4)break;if(FLAGE=3)k+;if(k=3)printf( 输入过多 ,退出 n);elseprintf( 还有 %d 次机会登录 n, 3 - k);FLAGE=1;continue;if(FLAGE=4)while(1)memset(str,0,6);scanf(%s,str);if(!strcmp(str,show)printf( 显示在线用户 :n);fun_show(sockf。

18、d);continue;else if(!strcmp(str,all)printf( 群聊模式);fun_all(sockfd);continue;else if(!strcmp(str,to)printf( 私聊模式 );fun_one(sockfd);continue;else if(!strcmp(str,quit)printf( 退出聊天室fun_quit(sockfd);close(sockfd);return;n);else if(!strcmp(str,help)printf(all 聊天内容 printf(toname聊天内容printf(quitprintf(help群聊 n);私聊 n);退出程序 n);显示帮助信息n);printf(please input con:n);continue;elseprintf(请输入help查看指令:n);continue;/ wait(NULL。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值