电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

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

linux-下用c语言编写的聊天室程序-服务器和客户端_第1页
1/14
linux-下用c语言编写的聊天室程序-服务器和客户端_第2页
2/14
linux-下用c语言编写的聊天室程序-服务器和客户端_第3页
3/14
/* * server.c * * Created on: 2025-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;char name[20];pthread_t pid;int flg;} c_client;c_client client[MAX_CLIENT];//定义 client;//等待的 clientstruct _client_ {int sockfd;char name[20];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++) {client[i].sockfd = -1;memset(client[i].name, 0, 20);client[i].pid = -1;client[i].flg = -1;}}//查找结构体数组中 sockfd 为-1 的下标值int find_fd(c_client *client) {int i = 0;while (i < MAX_NUM) {//printf("====%d\n",client[i].sockfd);if (client[i].sockfd == -1)return i;i++;}return -1;}//推断登录格式int logform(char *buf) {char *p = strstr(buf, "LOGIN\r\n");int n = strlen(buf);char *q = p + n - 4;if (p != NULL && p + 7 != q && strcmp(q, "\r\n\r\n") == 0)return 1;elsereturn 0;}int cmpname(char *buf, c_client *p_client) {int i = 0;char *p = strtok(buf + 7, "\r\n\r\n");while (client[i].sockfd != -1 && client[i].sockfd != p_client->sockfd && i< MAX_NUM) {if (strcmp(client[i].name, p) == 0)return 0;i++;}return 1;}//SHOWvoid showuser(c_client *p_client) {int i = 0;char buf[1024] = { 0 };strcpy(buf, "200\r\n");for (i = 0; i < MAX_NUM; i++) {if (client[i].sockfd != -1) {sprintf(buf + strlen(buf), "%s\r\n", client[i].name);}}sprintf(buf + strlen(buf), "\r\n");send(p_client->sockfd, buf, strlen(bu...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

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

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部