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

这篇博客介绍了如何使用C语言在Linux环境下编写一个聊天室程序,包括服务器和客户端部分。程序实现了多个客户端连接,使用了线程同步锁,并且与MySQL数据库进行交互。内容涉及网络编程、多线程、字符串处理以及登录验证等功能。
摘要由CSDN通过智能技术生成

/*

server.c

Created on: 2012-6-15

Author: root

*/

#include

#include

#include

#include

#include

#inelude

#include

#include

#include

#define PORT 7999

#define MAX_NUM 3 //client 连接最大个数

#define MAX_CLIENT 15

#define MAX_SIZE 1024

pthread_rwlock_t idxjock, waitjock;

//client 信息

typedef struct _client {

int sockfd;

char name[20];

pthread_t pid;

int fig;

} c_client;

c_client client[MAX_CLIENT];//定义 client;

//等待的client

struct _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为的下标值

int find_fd(c_client *client) {

int i = 0;

while (i v 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\rT) == 0) return 1;

else

return 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 v MAX_NUM){

if (strcmp(client[i].name, p) == 0)

return 0;

i++;

}

return 1;

}

//SHOW

void showuser(c_client *p_client) {

int i = 0;

char buf[1024] = { 0 };

strcpy(buf, n200\r\nH);

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(buf), 0);

}

//ALL

void sendto_all(c_client *p_client, char *buf) {

int i = 0;

char sendbuf[1024] = { 0 };

sprintf(sendbuf, "AFROM\r\n%s\r\n%s”, p_client->name, buf + 5);

for (i

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值