聊天室
在linux下使用C语言开发的一款简单的聊天程序。
服务器
ser.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mariadb/mysql.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <errno.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <libgen.h>//basename():从路径中获取文件名及后缀
MYSQL *conn;
#define OPEN_MAX 1024
typedef struct xinxi{
int ice_1; //功能选择
int ice_2;
int ice_3;
int ice_4;
int m_id; //客户id
int y_id; //好友id
int q_id; //群id
int fd; //套接字码
int zt; //状态
char name[20]; //name
char password[16]; //密码
char qu[200]; //问题
char an[100]; //答案
char beizhu[20]; //备注
char hylb[20]; //好友列表
char cylb[20]; //成员列表
char jl[20]; //聊天记录
char buf[200]; //信息内容
}XINXI;
typedef struct denn{
int ice; //功能选择
int id; //id
int zt; //状态
char name[20]; //name
char password[16]; //密码
char qu[200]; //问题
char an[100]; //答案
char hylb[20]; //好友列表
}DENN;
typedef struct liaot{
int ice;
int id;
int zt;
char name[20];
char qu[200];
char an[100];
char buf[50];
char beizhu[20];
char xinxi[200];
}LIAOT;
typedef struct Doc{
int sign; //
char buf[1024];
}DOC;
typedef struct E_list{
int index;
int efd;
int sum;
struct epoll_event tep, ep[OPEN_MAX];
struct E_list *next;
}E_LIST;
E_LIST EPOL[20];
pthread_t thid[20];
int S_ID,E_ID,S_FD,Lock;
int denglu(XINXI *YY,int sfd); //登陆
int zhuce(XINXI *YY,int sfd); //注册
int zhaohui(XINXI *YY,int sfd); //找回密码
int zhaohui_1(XINXI *YY,int sfd); //找回密码
void liaotian(DENN *XX, LIAOT *XZ,int sfd);
int xuanzhe_1(DENN *XX, XINXI *YY, int sfd); //好友管理
int xuanzhe_2(DENN *XX, XINXI *YY, int sfd); //好友聊天
int group_1(DENN *XX, XINXI *YY, int sfd); //群管理
int group_2(DENN *XX, XINXI *YY, int sfd); //群聊天
void G_send(XINXI *YY, int sfd);
void G_get(XINXI *YY, int sfd);
int ADD(XINXI *YY, DENN *XX) ;
int TongZ(XINXI *YY, int sfd); //添加好友,加群通知
void HY_get(XINXI *YY, int id, int sfd); //epoll收发信息
void HY_send(XINXI *YY, int sfd); //将未读信息发送给客户端
int G_ADD(XINXI *YY, DENN *XX);
void G_ADD_1(XINXI *YY,int sfd);
void getDENN(DENN *XX, int id, int sfd);
void logof(XINXI *YY, int sfd);
void get_TZ(XINXI *YY, int sfd);
int set_1(int id);
int drop_1(int id);
int panduan_1(int id); //判断客户是否空闲
int panduan_2(int id);
void TX(XINXI *YY);
void Document(XINXI *YY, int sfd, E_LIST *p);
void login(XINXI *YY, int sfd);
void sort();
void *thread_account(void *arg);
char *getname_from_id(int id);
char *getjl_from_id(int id_I, int id_II);
int getid_from_beizhu(int id,char *beizhu);
char *getbeizhu_from_id(int m_id,int id);
char *getgroupname_from_id(int id);
char *getgroupjl_from_id(int id);
ser.c
#include "ser.h"
void Document(XINXI *YY, int sfd, E_LIST *p)
{
int ret, fd, dfd;
char A[100];
char file_len[16], file_name[128], buf[1024],file_new_name[128],sign[10];
if(YY->ice_2 == 71) //接收来自客户端的文件
{
strncpy(file_len, YY->buf, sizeof(file_len));
strncpy(file_name, YY->qu, sizeof(file_name));
//printf("文件名:%s\n文件大小:%s\n", file_name, file_len);
sprintf(file_new_name, "./document/recv-%s", file_name);
//printf("%s", file_new_name);
fd = open(file_new_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
//printf("fd = %d\n", fd);
int size = atoi(file_len);
int write_len = 0, s_size = 1024,r_ice = 1;
DOC *D = (DOC*)malloc(sizeof(DOC));
while(1)
{
memset(D, 0, sizeof(DOC));
recv(sfd, D, sizeof(DOC), 0);
printf("D->sign = %d\n", D->sign);
printf("r_ice = %d\n", r_ice);
if(D->sign == r_ice)
{
if(write_len + 1024 > size)
{
s_size = size - write_len;
}
ret = write(fd, D->buf, s_size);
write_len += ret;
++r_ice;
sprintf(sign, "ok");
printf("file_len = %d\nwrite_len = %d\n", size, write_len);
}
else
{
sprintf(sign, "no");
}
send(sfd, sign, sizeof(sign), 0);
if(write_len >= size)
{
close(fd);
break;
}
}
printf("file_len = %d\nwrite_len = %d\n", size, write_len);
sprintf(A, "insert into document (from_id, end_id, file_name) values (%d, %d, '%s')", YY->m_id, YY->y_id, file_new_name);
mysql_query(conn,A);
}
else if(YY->ice_2 == 72) //向客户端发送文件
{
int sum = 0;
sprintf(A, "select file_name from document where from_id = %d and end_id = %d", YY->y_id, YY->m_id);
mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
sum++;
}
write(sfd, &sum, sizeof(int));
if(sum <= 0)
{
return ;
}
sprintf(A, "select file_name from document where from_id = %d and end_id = %d", YY->y_id, YY->m_id);
mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
strncpy(file_name, (char *)res_row[0], sizeof(file_name));
fd = open(file_name, O_RDWR);
if(fd == -1)
{
printf("打开文件失败!!!\n");
return ;
}
int len = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET); //文件光标移动到开始位置
strncpy(file_name, basename(file_name), sizeof(file_name));
strncpy(XZ->xinxi, file_name, sizeof(file_name));
sprintf(XZ->beizhu, "%d", len);
//printf("len = %s\nname = %s\n",XZ->beizhu, XZ->xinxi);
send(sfd, XZ, sizeof(LIAOT), 0);
int send_len = 0; //记录发送的字节数
int s_ice = 1;
DOC *D = (DOC*)malloc(sizeof(DOC));
while(1)
{
memset(D, 0, sizeof(DOC));
memset(sign, 0, sizeof(sign));
ret = read(fd, D->buf, sizeof(D->buf));
if(ret <= 0)
{
close(fd);
printf("\t\t文件%s发送成功!!!\n", file_name);
break;
}
D->sign = s_ice;
send(sfd, D, sizeof(DOC), 0);
recv(sfd, sign, sizeof(sign), 0);
printf("sign = %s\n", sign);
if(strcmp(sign, "ok") == 0)
{
printf("\t\tlen = %d\n\t\tsend_len = %d\n", len, send_len);
send_len += ret;
++s_ice;
}
else if(strcmp(sign, "no") == 0)
{
while(1)
{
send(sfd, D, sizeof(DOC), 0);
recv(sfd, sign, sizeof(sign), 0);
if(strcmp(sign, "ok") == 0)
{
printf("\t\tlen = %d\n\t\tsend_len = %d\n", len, send_len);
send_len += ret;
++s_ice;
break;
}
}
}
else
{
exit(-1);
}
if(send_len == len)
{
close(fd);
printf("\t\t文件%s发送成功!!!\n", file_name);
break;
}
}
sprintf(A, "delete from document where from_id = %d and end_id = %d and file_name = '%s'", YY->y_id, YY->m_id, res_row[0]);
//printf("A = %s\n", A);
mysql_query(conn,A);
free(XZ);
}
}
}
void login(XINXI *YY, int sfd)
{
char A[100];
time_t timep_1;
time(&timep_1);
struct tm *p;
p = gmtime(&timep_1);
memset(A, 0, sizeof(A));
if(YY->ice_1 < 0 || YY->ice_1 > 1000)
{
YY->ice_1 = 0;
}
if(YY->ice_2 < 0 || YY->ice_2 > 1000)
{
YY->ice_2 = 0;
}
if(YY->ice_3 < 0 || YY->ice_3 > 1000)
{
YY->ice_3 = 0;
}
if(YY->ice_4 < 0 || YY->ice_4 > 1000)
{
YY->ice_4 = 0;
}
if(YY->zt < 0 || YY->zt > 100)
{
YY->zt = 0;
}
if(YY->ice_1 == 0 && YY->ice_2 == 0 && YY->ice_3 == 0 && YY->ice_4 == 0 && YY->m_id == 0 && YY->y_id == 0 && YY->q_id == 0 && YY->zt == 0)
{
time_t timep_1;
time(&timep_1);
struct tm *p;
p = gmtime(&timep_1);
sprintf(A, "%4d/%2d/%2d %2d/%2d/%2d\t client closed client_fd:%d\n",p->tm_year + 1900,p->tm_mon + 1,p->tm_mday
,p->tm_hour + 8,p->tm_min,p->tm_sec,sfd);
int fd = open("server_log.txt", O_WRONLY | O_APPEND);
write(fd, A, strlen(A));
close(fd);
return ;
}
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
sprintf(A, "select * from student where id = %d", YY->m_id);
mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row == NULL)
{
if(YY->ice_1 != 1 || YY->ice_2 != 12)
{
YY->m_id = 0;
}
}
sprintf(A, "select * from student where id = %d", YY->y_id);
mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row == NULL)
{
YY->y_id = 0;
}
sprintf(A, "select * from allgroup where id = %d", YY->q_id);
mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row == NULL)
{
if(YY->ice_1 != 3 || YY->ice_2 != 31 || YY->ice_3 != 311)
{
YY->q_id = 0;
}
}
//int day = (p->tm_year + 1900)*10000 + (p->tm_mon + 1)*100 + p->tm_mday;
//int time = (p->tm_hour + 8)*10000 + (p->tm_min)*100 + p->tm_sec;
sprintf(A, "%4d/%2d/%2d %2d/%2d/%2d\t %3d\t\t%3d\t\t%3d\t\t%3d\t %8d\t%8d\t%8d\t %d\t %d\n",p->tm_year + 1900,p->tm_mon + 1,p->tm_mday
,p->tm_hour + 8,p->tm_min,p->tm_sec,YY->ice_1, YY->ice_2, YY->ice_3, YY->ice_4,YY->m_id, YY->y_id, YY->q_id, YY->zt, sfd);
int fd = open("server_log.txt", O_WRONLY | O_APPEND);
write(fd, A, strlen(A));
close(fd);
}
void *thread_account(void *arg)
{
//printf("YYYYYYYYYYYYYYYYYYY\n");
char A[100];
int nready,i, n, ret, sfd;
DENN *XX = (DENN*)malloc(sizeof(DENN));
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
XINXI YY;
/*
conn = mysql_init(conn); //初始化一个句柄
mysql_library_init(0,NULL,NULL); //初始化数据库
if(conn == NULL)
{
printf("mysql_init failed!!!\n");
exit(1);
}
conn = mysql_real_connect(conn,"127.0.0.1","root","181219","db1",0,NULL,0); //连接数据库
mysql_set_character_set(conn,"utf8"); //调整为中文字符
if(conn == NULL)
{
printf("mysql_real_connect failed!!!\n");
exit(1);
}
//sprintf(A, "update student set zt=0"); //服务器重启,所有用户都不在线
//mysql_query(conn,A);
*/
E_LIST *p = (E_LIST *)arg;
while(p->sum == 0)
{
//空语句
}
//struct epoll_event tep, ep[OPEN_MAX]; //定义描述符和响应队列
//p->efd = epoll_create(OPEN_MAX);
//p->tep.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP;
//p->tep.data.fd = sfd;
//epoll_ctl(p->efd, EPOLL_CTL_ADD, sfd, &(p->tep)); //将监听套接字加入等待队列
//printf("AA\n");
while(1)
{
//printf("A\n");
nready = epoll_wait(p->efd, p->ep, OPEN_MAX, -1); //等待就绪套接字,将就绪套接字放入响应队列,返回响应套接字的个数
//printf("nready = %d\n", nready);
//printf("errno is: %d\n",errno);
for(i = 0; i < nready; i++)
{
//printf("B\n");
if(!p->ep[i].events & EPOLLIN) //该套接字描述符不可读
continue;
else if(p->ep[i].events & EPOLLIN)
{
//printf("C\n");
sfd = p->ep[i].data.fd;
memset(&YY, 0, sizeof(XINXI));
//n = read(sfd, buf, sizeof(buf)); //读取套接字内容
n = recv(sfd, &YY, sizeof(XINXI), 0);
//TX(&YY);
login(&YY, sfd);
//printf("%d\n", n);
//TX(&YY);
if(n == 0) //客户端关闭
{
//printf("qu\n");
ret = epoll_ctl(p->efd, EPOLL_CTL_DEL, sfd, NULL); //将套接字sfd从等待队列中删除
sprintf(A, "update student set zt = 0 where id = %d", YY.m_id);
mysql_query(conn,A); //用户退出,将状态设置为不在线
sprintf(A, "update student set fd = 0 where id = %d",YY.m_id);
mysql_query(conn,A); //用户退出后将fd归0
close(sfd); //关闭该套接字
}
else
{
//printf("YY->ice = %d\n", YY.ice);
if(YY.ice_1 == 1) //登陆,注册,找回密码
{
int ret = 0;
if(YY.ice_2 == 11) //登陆
{
//printf("id = %d\tice = %d\n", XX->id, XX->ice);
ret = denglu(&YY, sfd);
}
else if(YY.ice_2 == 12) //注册
{
ret = zhuce(&YY, sfd);
//printf("QQQQQQQ\n");
}
else if(YY.ice_2 == 13) //找回密码
{
if(YY.ice_3 == 131)
{
zhaohui_1(&YY, sfd);
}
else
{
ret = zhaohui(&YY, sfd);
}
}
}
else if(YY.ice_1 == 2) //好友管理
{
getDENN(XX, YY.m_id, sfd);
xuanzhe_1(XX, &YY, sfd);
}
else if(YY.ice_1 == 3) //聊天群管理
{
// TX(&YY);
if(YY.ice_4 == 3111)
{
G_ADD_1(&YY, sfd);
}
else
{
getDENN(XX, YY.m_id, sfd);
group_1(XX,&YY, sfd);
}
}
else if(YY.ice_1 == 4 && YY.ice_4 == 0) //好友聊天
{
//printf("AAAAAAAAAAAAAs\n");
getDENN(XX, YY.m_id, sfd);
//printf("BBBBBBBBBBBBBBS\n");
xuanzhe_2(XX, &YY, sfd);
}
else if(YY.ice_1 == 5 && YY.ice_4 == 0) //群聊天
{
//printf("AAAAAAAAAAAAAs\n");
getDENN(XX, YY.m_id, sfd);
//printf("BBBBBBBBBBBBBBS\n");
group_2(XX, &YY, sfd);
}
else if(YY.ice_1 == 6) //查看通知
{
TongZ(&YY, sfd);
}
else if(YY.ice_1 == 7)
{
Document(&YY, sfd, p);
}
else if(YY.ice_4 == 666 && YY.ice_1 == 4)
{
//TX(&YY);
HY_send(&YY, sfd);
}
else if(YY.ice_4 == 777 && YY.ice_1 == 4)
{
//TX(&YY);
//printf("XX->id = %d\n", XX->id);
HY_get(&YY, YY.m_id, sfd);
}
else if(YY.ice_4 == 888 && YY.ice_1 == 5)
{
G_get(&YY, sfd);
}
else if(YY.ice_4 == 999 && YY.ice_1 == 5)
{
//YY.m_id = XX->id;
G_send(&YY, sfd);
}
else if(YY.ice_1 == 404)
{
logof(&YY, sfd);
}
else if(YY.ice_1 == 555)
{
get_TZ(&YY, sfd);
}
}
//printf("a\n");
}
//printf("b\n");
}
//printf("c\n");
}
//printf("d\n");
free(XX);
}
void getDENN(DENN *XX, int id, int sfd)
{
char A[100];
int ret;
memset(XX, 0, sizeof(DENN));
XX->id = id; //将m_id存入XX中
sprintf(A, "update student set fd = %d where id = %d", sfd, XX->id);
ret = mysql_query(conn,A); //登陆后将fd存入数据库中
//printf("A = %s\nret = %d\n", A, ret);
//获取name
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
sprintf(A, "select name from student where id = %d", id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
//printf("%s\n", res_row[0]);
strncpy(XX->name, res_row[0], sizeof(XX->name));
mysql_free_result(res_ptr);
//获取yhlb
sprintf(A, "select hylb from student where id = %d", id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
strncpy(XX->hylb, res_row[0], sizeof(XX->hylb));
mysql_free_result(res_ptr);
}
char *getname_from_id(int id)
{
int ret;
char A[100], *name = (char *) malloc(sizeof(char)*20);
//获取name
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
sprintf(A, "select name from student where id = %d", id);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
strncpy(name, res_row[0], sizeof(name));
}
else
{
printf("NULL\n");
}
mysql_free_result(res_ptr);
return name;
}
char *getjl_from_id(int id_I, int id_II)
{
//printf("id_I = %d, id_II = %d\n", id_I, id_II);
char A[20],B[20], *jl = (char *)malloc(sizeof(char)*20);
strncpy(A, getname_from_id(id_I), sizeof(A));
strncpy(B, getname_from_id(id_II), sizeof(B));
//printf("A: %s\n", A);
//printf("B: %s\n", B);
if(strcmp(A,B) > 0)
{
strncpy(jl, A, sizeof(A));
strcat(jl,B);
}
else
{
strncpy(jl, B, sizeof(B));
strcat(jl,A);
}
//printf("jl = %s\n", jl);
return jl;
}
int getid_from_beizhu(int id,char *beizhu)
{
char A[100];
int ID;
int ret;
sprintf(A, "select id from %shylb where beizhu = '%s'", getname_from_id(id), beizhu);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
ID = atoi(res_row[0]);
return ID;
}
else
{
return 0;
}
}
char *getbeizhu_from_id(int m_id,int id)
{
char A[100],*beizhu = (char *) malloc(sizeof(char)*20);
int ret;
sprintf(A, "select beizhu from %shylb where id = %d", getname_from_id(m_id), id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
//printf("YYYYYYYYYY\n");
if(res_row != NULL)
{
strncpy(beizhu, res_row[0], sizeof(beizhu));
}
//printf("XXXXXXXXXXXX\n");
return beizhu;
}
char *getgroupname_from_id(int id)
{
char A[100], *name = (char *) malloc(sizeof(char)*20);
int ret;
sprintf(A, "select name from allgroup where id = %d", id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
strncpy(name, res_row[0], sizeof(name));
}
mysql_free_result(res_ptr);
return name;
}
char *getgroupjl_from_id(int id)
{
char A[100], *jl = (char *) malloc(sizeof(char)*20);
int ret;
sprintf(A, "select jl from allgroup where id = %d", id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
strncpy(jl, res_row[0], sizeof(jl));
}
mysql_free_result(res_ptr);
return jl;
}
void get_TZ(XINXI *YY, int sfd)
{
char A[100],B[50], ch;
int ret, field, sum = 0, m = 0;
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
sprintf(A, "select message from box where end_id = %d", YY->m_id);
ret = mysql_query(conn,A);
if(ret)
{
printf("wrong!\n");
}
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
while(res_row=mysql_fetch_row(res_ptr))
{
sum++;
//printf("sum1 = %d\n", sum);
}
mysql_free_result(res_ptr);
if(sum > 0)
{
sprintf(B, "----------有%d条加好友通知----------", sum);
send(sfd, B, sizeof(B), 0);
}
sprintf(A, "select id from %sgrouptable where sf > 0",getname_from_id(YY->m_id));
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
//printf("AAAA\n");
int ID = atoi(res_row[0]);
sprintf(A, "select message from box where end_id = %d", ID);
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr_1;
MYSQL_ROW res_row_1;
res_ptr_1 = mysql_store_result(conn);
do
{
res_row_1 = mysql_fetch_row(res_ptr_1);
if(res_row_1 != NULL)
{
m++;
}
else
{
break;
}
}while(1);
mysql_free_result(res_ptr_1);
}
mysql_free_result(res_ptr);
if(m > 0)
{
sprintf(B, "----------有%d条加群通知------------", m);
send(sfd, B, sizeof(B), 0);
}
sprintf(A, "select id from %shylb", getname_from_id(YY->m_id));
mysql_query(conn, A);
res_ptr = mysql_store_result(conn);
while(res_row=mysql_fetch_row(res_ptr))
{
int ID = atoi(res_row[0]);
int s = 0;
sprintf(A, "select * from %s where zt = 1 and end_id = %d", getjl_from_id(YY->m_id, ID), YY->m_id);
mysql_query(conn, A);
MYSQL_RES *res_ptr_1;
MYSQL_ROW res_row_1;
res_ptr_1 = mysql_store_result(conn);
while(res_row_1 = mysql_fetch_row(res_ptr_1))
{
s++;
}
if(s > 0)
{
sprintf(B, "----------有来自%s的%d条信息-------------", getbeizhu_from_id(YY->m_id, ID), s);
send(sfd, B, sizeof(B), 0);
}
}
//q_id需要循环
int day, time_1;
sprintf(A, "select id from %sgrouptable", getname_from_id(YY->m_id));
mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
sum = 0;
YY->q_id = atoi(res_row[0]);
sprintf(A, "select day from %scylb where id = %d", getgroupname_from_id(YY->q_id), YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\nYY->m_id = %d\nYY->q_id = %d\n", A, ret, YY->m_id, YY->q_id);
MYSQL_RES *res_ptr_1;
MYSQL_ROW res_row_1;
res_ptr_1 = mysql_store_result(conn);
res_row_1 = mysql_fetch_row(res_ptr_1);
if(res_row_1 != NULL)
{
day = atoi(res_row_1[0]);
}
mysql_free_result(res_ptr_1);
sprintf(A, "select time from %scylb where id = %d", getgroupname_from_id(YY->q_id), YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\nYY->m_id = %d\nYY->q_id = %d\n", A, ret, YY->m_id, YY->q_id);
res_ptr_1 = mysql_store_result(conn);
res_row_1 = mysql_fetch_row(res_ptr_1);
if(res_row_1 != NULL)
{
time_1 = atoi(res_row_1[0]);
}
mysql_free_result(res_ptr_1);
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
sprintf(A, "select * from %sjl where day = %d and time > %d and id != %d", getgroupname_from_id(YY->q_id), day, time_1, YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr_1 = mysql_store_result(conn);
while(res_row_1 = mysql_fetch_row(res_ptr_1))
{
sum++;
}
mysql_free_result(res_ptr_1);
sprintf(A, "select * from %sjl where day > %d and id != %d", getgroupname_from_id(YY->q_id),day,YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr_1 = mysql_store_result(conn);
while(res_row_1 = mysql_fetch_row(res_ptr_1))
{
sum++;
}
mysql_free_result(res_ptr_1);
if(sum > 0)
{
sprintf(B, "----------有来自群%s的%d条信息-----------", getgroupname_from_id(YY->q_id), sum);
send(sfd, B, sizeof(B), 0);
}
}
mysql_free_result(res_ptr);
sprintf(A, "select from_id from document where end_id = %d", YY->m_id);
mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
int id = atoi(res_row[0]);
sprintf(B, "----------有来自好友%d的文件传输----------",id);
send(sfd, B, sizeof(B), 0);
}
mysql_free_result(res_ptr);
sprintf(B, "over");
send(sfd, B, sizeof(B), 0);
}
int G_ADD(XINXI *YY, DENN *XX)
{
int field, fd, ret;
char B[50], name[20], A[100];
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
strncpy(name, getgroupname_from_id(YY->q_id), sizeof(name));
sprintf(A, "select * from box where from_id = %d and end_id = %d", YY->m_id, YY->q_id);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row == NULL)
{
sprintf(B, "用户%s请求加入群聊%s!", XX->name, name);
//printf("id = %d\n", XZ->id);
sprintf(A,"select * from %sgrouptable where id = %d", getname_from_id(YY->m_id), YY->q_id);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
ret = -1; //该群聊已经加入
}
else
{
sprintf(A, "insert into box (from_id,end_id,message,ZT,ice,beizhu) values (%d,%d,'%s',0,1,'%s')", XX->id, YY->q_id, B, XX->name);
ret = mysql_query(conn,A);
if(ret)
{
printf("wrong!\n");
ret = 0; //信息发送失败
}
else
{
ret = 1; //信息发送成功
}
}
}
else
{
ret = -2;
}
return ret;
}
void G_ADD_1(XINXI *YY, int sfd) //问题回答
{
char A[100],name[20],B[50];
if(YY->zt == 1)
{
int ret_1, ret_2, ret_3;
strncpy(name, getgroupname_from_id(YY->q_id), sizeof(name));
sprintf(A, "insert into %sgrouptable (id, name, sf) values (%d, '%s', 0)", getname_from_id(YY->m_id), YY->q_id, name);
ret_1 = mysql_query(conn,A); //添加到已加群表
sprintf(A, "insert into %scylb (id, name, sf) values (%d, '%s', 0)", name, YY->m_id, getname_from_id(YY->m_id));
ret_2 = mysql_query(conn,A); //添加到成员列表
if(ret_1 == 0 && ret_2 == 0)
sprintf(B, "加入成功!\n");
else
sprintf(B, "加入失败!\n");
send(sfd, B, sizeof(B), 0);
}
}
int group_1(DENN *XX, XINXI *YY, int sfd) //群管理
{
char A[100],B[50],name[20],ch;
int ret, id, ret_1, ret_2, ret_3, ret_4, field;
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
YY->m_id = XX->id;
if(YY->ice_2 == 31) //群的创建与解散
{
time_t timep_1;
struct tm *p;
time(&timep_1);
if(YY->ice_3 == 311) //群的创建
{
if(YY->zt == 1 || YY->zt == 0) //需要管理员同意或无要求
{
sprintf(A, "select * from allgroup where id = %d", YY->q_id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL) //该ID已存在
{
sprintf(B, "该ID已存在,请更换!");
}
else
{
sprintf(A, "select * from allgroup where name = %s", YY->name);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL) //该name已存在
{
sprintf(B, "该name已存在,请更换!");
}
else
{
p = gmtime(&timep_1);
int day = (p->tm_year + 1900)*10000 + (p->tm_mon + 1)*100 + p->tm_mday;
int time = (p->tm_hour + 8)*10000 + (p->tm_min)*100 + p->tm_sec;
//printf("m_id = %d,id = %d\n",YY->m_id, XX->id);
sprintf(A, "insert into %sgrouptable (id, name, sf) values (%d, '%s', 2)", getname_from_id(XX->id), YY->q_id, YY->name);
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
sprintf(A, "insert into allgroup (id, name, cylb, jl, qu, an, zt) values (%d, '%s', '%scylb', '%sjl', NULL, NULL, %d)", YY->q_id, YY->name, YY->name, YY->name, YY->zt);
ret_1 = mysql_query(conn,A); //添加至群信息列表
sprintf(A, "create table %scylb (id int, name varchar(20), sf int, day int, time int)",YY->name);
ret_2 = mysql_query(conn,A); //创建群成员列表
sprintf(A, "create table %sjl (id int, name varchar(20), xinxi varchar(100), day int, time int)", YY->name);
ret_3 = mysql_query(conn,A); //创建群聊天记录列表
sprintf(A, "insert into %scylb (id, name, sf, day, time) values (%d, '%s', 2, %d, %d)", YY->name, YY->m_id, getname_from_id(XX->id), day, time);
ret_4 = mysql_query(conn,A);
//printf("ret_1 = %d\tret_2 = %d\tret_3 = %d\n", ret_1, ret_2, ret_3);
if(ret_1 == 0 && ret_2 == 0 && ret_3 == 0 && ret_4 == 0 && ret == 0)
{
sprintf(B, "创建成功!!!");
}
else
{
sprintf(B, "创建失败!!!");
}
}
}
//mysql_free_result(res_ptr);
send(sfd, B, sizeof(B), 0);
}
else if(YY->zt == 2) //需要回答问题
{
time_t timep_1;
sprintf(A, "select * from allgroup where id = %d", YY->q_id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL) //该ID已存在
{
sprintf(B, "该ID已存在,请更换!");
}
else
{
sprintf(A, "select * from allgroup where name = %s", YY->name);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL) //该name已存在
{
sprintf(B, "该name已存在,请更换!");
}
else
{
p = gmtime(&timep_1);
int day = (p->tm_year + 1900)*10000 + (p->tm_mon + 1)*100 + p->tm_mday;
int time = (p->tm_hour + 8)*10000 + (p->tm_min)*100 + p->tm_sec;
//printf("id = %d\n", XX->id);
sprintf(A, "insert into %sgrouptable (id, name, sf) values (%d, '%s', 2)", getname_from_id(XX->id), YY->q_id, YY->name);
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
sprintf(A, "insert into allgroup (id, name, cylb, jl, qu, an, zt) values (%d, '%s', '%scylb', '%sjl', '%s', '%s', %d)", YY->q_id, YY->name, YY->name, YY->name, YY->qu, YY->an, YY->zt);
//printf("A = %s\n", A);
ret_1 = mysql_query(conn,A); //添加至群信息列表
sprintf(A, "create table %scylb (id int, name varchar(20), sf int,day int, time int)",YY->name);
//printf("A = %s\n", A);
ret_2 = mysql_query(conn,A); //创建群成员列表
sprintf(A, "create table %sjl (id int, name, xinxi varchar(100), day int, time int)", YY->name);
//printf("A = %s\n", A);
ret_3 = mysql_query(conn,A); //创建群聊天记录列表
sprintf(A, "insert into %scylb (id, name, sf, day, time) values (%d, '%s', 2, %d, %d)", YY->name, YY->m_id, getname_from_id(XX->id), day, time);
//printf("A = %s\n", A);
ret_4 = mysql_query(conn,A);
if(ret_1 == 0 && ret_2 == 0 && ret_3 == 0 && ret_4 == 0 && ret == 0)
{
sprintf(B, "创建成功!!!");
}
}
}
//mysql_free_result(res_ptr);
send(sfd, B, sizeof(B), 0);
}
}
else if(YY->ice_3 == 312) //群的解散
{
//TX(YY);
sprintf(A, "select * from allgroup where id = %d", YY->q_id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
int I;
strncpy(name, getgroupname_from_id(YY->q_id), sizeof(name));
sprintf(A, "select id from %scylb", name);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
I = atoi(res_row[0]);
sprintf(A, "delete from %sgrouptable where id = %d", getname_from_id(I), YY->q_id);
ret = mysql_query(conn,A); //将已加群列表中该群的信息删除
if(ret != 0)
{
printf("群解散失败!!!\n");
}
}
mysql_free_result(res_ptr);
sprintf(A, "delete from allgroup where id = %d", YY->q_id);
ret_1 = mysql_query(conn,A);
sprintf(A, "drop table %scylb", name);
ret_2 = mysql_query(conn,A);
sprintf(A, "drop table %sjl", name);
ret_3 = mysql_query(conn,A);
//printf("ret_1 = %d\tret_2 = %d\tret_3 = %d\n", ret_1, ret_2, ret_3);
if(ret_1 == 0 && ret_2 == 0 && ret_3 == 0)
{
sprintf(B, "解散成功!!!");
}
else
{
sprintf(B, "解散失败!!!");
}
}
else
{
sprintf(B, "该群ID不存在!");
//printf("QQQ\n");
}
//printf("YYY\n");
send(sfd, B, sizeof(B), 0);
}
}
else if(YY->ice_2 == 32) //申请加群退群*
{
if(YY->ice_3 == 321) //加群
{
int ZT;
sprintf(A, "select zt from allgroup where id = %d", YY->q_id);
//printf("%s\n", A);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row == NULL)
{
sprintf(XZ->buf, "无此ID的群!!!");
}
else
{
//printf("res_row = %s\n",res_row[0]);
ZT = atoi(res_row[0]);
}
//printf("ZT = %d\n", ZT);
if(ZT == 0) //直接加入
{
sprintf(A, "select * from %sgrouptable where id = %d", getname_from_id(YY->m_id), YY->q_id);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
sprintf(XZ->buf, "你已经加入了此群!!!\n");
}
else
{
int ret_1, ret_2, ret_3;
strncpy(name, getgroupname_from_id(YY->q_id), sizeof(name));
sprintf(A, "insert into %sgrouptable (id, name, sf) values (%d, '%s', 0)", getname_from_id(YY->m_id), YY->q_id, name);
ret_1 = mysql_query(conn,A); //添加到已加群表
sprintf(A, "insert into %scylb (id, name, sf) values (%d, '%s', 0)", name, YY->m_id, getname_from_id(YY->m_id));
ret_2 = mysql_query(conn,A); //添加到成员列表
XZ->zt = 0;
if(ret_1 == 0 && ret_2 == 0)
sprintf(XZ->buf, "加入成功!\n");
else
sprintf(XZ->buf, "加入失败!\n");
}
}
else if(ZT == 1) //需要管理员同意才能加入
{
ret = G_ADD(YY, XX);
XZ->zt = 1;
if(ret == 0)
{
sprintf(XZ->buf, "请求发送失败!");
}
else if(ret == 1)
{
sprintf(XZ->buf, "请求发送成功,等待同意中!");
}
else if(ret == -1)
{
sprintf(XZ->buf, "你已经加入此群!");
}
else if(ret == -2)
{
sprintf(XZ->buf, "请求已经发送过!");
}
}
else if(ZT == 2) //回答问题加入
{
sprintf(A, "select qu and an from allgroup where id = %d", YY->q_id);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
XZ->zt = 100;
strncpy(XZ->qu, res_row[0], sizeof(XZ->qu));
strncpy(XZ->an, res_row[1], sizeof(XZ->an));
mysql_free_result(res_ptr);
}
send(sfd, XZ, sizeof(LIAOT), 0);
}
else if(YY->ice_3 == 322) //退群
{
sprintf(A, "select * from %sgrouptable where id = %d", getname_from_id(YY->m_id),YY->q_id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
int ret_1, ret_2;
sprintf(A, "delete * from %sgrouptable where id = %d", getname_from_id(YY->m_id), YY->q_id);
ret_1 = mysql_query(conn,A); //从已加列表中删除
sprintf(A, "delete from %scylb where id = %d", getgroupname_from_id(YY->q_id), YY->m_id);
ret_2 = mysql_query(conn,A); //从群成员列表中删除
if(ret_1 == 0 && ret_2 == 0)
{
sprintf(B, "退出成功!");
}
}
else
{
sprintf(B, "你没有加入此群!!!");
}
send(sfd, B, sizeof(B), 0);
}
}
else if(YY->ice_2 == 33) //查看已加群和群成员
{
if(YY->ice_3 == 331) //查看已加群
{
sprintf(A, "select * from %sgrouptable", getname_from_id(YY->m_id));
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
if(ret == 0)
{
sprintf(B, "ok");
send(sfd, B, sizeof(B), 0);
}
while(res_row = mysql_fetch_row(res_ptr))
{
int i, sf;
memset(B, 0, sizeof(B));
for(i = 0; i < field-1; i++)
{
strcat(B, res_row[i]);
strcat(B, "\t");
}
sf = atoi(res_row[i]);
if(sf == 1)
{
strcat(B, "管理员");
}
else if(sf == 2)
{
strcat(B, "群主");
}
else if(sf == 0)
{
strcat(B, "普通群成员");
}
//printf("B = %s\n", B);
send(sfd, B, sizeof(B), 0);
}
sprintf(B, "over");
send(sfd, B, sizeof(B), 0);
mysql_free_result(res_ptr);
}
else if(YY->ice_3 == 332) //查看群成员
{
sprintf(A, "select * from allgroup where id = %d", YY->q_id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL) //该群存在
{
sprintf(A, "select * from %sgrouptable where id = %d", getname_from_id(YY->m_id), YY->q_id);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL) //用户在该群中
{
sprintf(A, "select * from %scylb", getgroupname_from_id(YY->q_id));
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
while(res_row = mysql_fetch_row(res_ptr))
{
int i, sf;
memset(B, 0, sizeof(B));
for(i = 0; i < 2; i++)
{
strcat(B, res_row[i]);
strcat(B, "\t");
}
sf = atoi(res_row[i]);
if(sf == 1)
{
strcat(B, "管理员");
}
else if(sf == 2)
{
strcat(B, "群主");
}
else if(sf == 0)
{
strcat(B, "普通群成员");
}
//printf("B = %s\n", B);
send(sfd, B, sizeof(B), 0);
}
mysql_free_result(res_ptr);
}
else
{
sprintf(B, "你不是该群的成员,无权查询!");
send(sfd, B, sizeof(B), 0);
}
}
else
{
sprintf(B, "该群不存在!");
send(sfd, B, sizeof(B), 0);
}
sprintf(B, "over");
send(sfd, B, sizeof(B), 0);
}
}
else if(YY->ice_2 == 34) //查看聊天记录
{
sprintf(A, "select * from %sgrouptable where id = %d", getname_from_id(YY->m_id), YY->q_id);
ret = mysql_query(conn, A);
//printf("A = %s\nret = %d\n",A, ret);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
strncpy(XZ->buf, "ok", sizeof(XZ->buf));
send(sfd, XZ, sizeof(LIAOT), 0);
sprintf(A, "select * from %sjl", getgroupname_from_id(YY->q_id));
ret = mysql_query(conn, A);
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
while(res_row = mysql_fetch_row(res_ptr))
{
XZ->id = atoi(res_row[0]);
strncpy(XZ->name, res_row[1], sizeof(XZ->name));
strncpy(XZ->xinxi, res_row[2], sizeof(XZ->xinxi));
XZ->zt = atoi(res_row[3]);
XZ->ice = atoi(res_row[4]);
send(sfd,XZ, sizeof(LIAOT), 0);
}
mysql_free_result(res_ptr);
memset(XZ, 0, sizeof(LIAOT));
strncpy(XZ->buf, "over", sizeof(XZ->buf));
send(sfd, XZ, sizeof(LIAOT), 0);
}
}
else if(YY->ice_2 == 35) //设置管理员
{
int pow,ret_1, ret_2;
memset(B, 0, sizeof(B));
strncpy(name, getgroupname_from_id(YY->q_id), sizeof(name));
sprintf(A, "select sf from %scylb where id = %d", name, YY->m_id);
ret = mysql_query(conn, A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
pow = atoi(res_row[0]);
if(pow == 2) //群主
{
sprintf(A, "update %scylb set sf = 1 where id = %d", name, YY->y_id);
ret_1 = mysql_query(conn,A);
sprintf(A, "update %sgrouptable set sf = 1 where id = %d", getname_from_id(YY->y_id), YY->q_id);
//printf("A = %s\n", A);
ret_2 = mysql_query(conn,A);
if(ret_1 == 0 && ret_2 == 0)
{
sprintf(B, "设置成功!");
//printf("B = %s\n", B);
}
//printf("ret_1 = %d\nret_2 = %d\n", ret_1, ret_2);
}
else
{
sprintf(B, "你不是群主,没有权限设置管理员!");
}
}
else
{
sprintf(B, "你不是该群的成员!");
}
send(sfd, B, sizeof(B), 0);
//printf("B = %s\n", B);
mysql_free_result(res_ptr);
}
else if(YY->ice_2 == 36) //踢人
{
//printf("AAA\n");
int pow_1, pow_2;
strncpy(name, getgroupname_from_id(YY->q_id), sizeof(name));
sprintf(A, "select sf from %scylb where id = %d", name,YY->m_id);
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
pow_1 = atoi(res_row[0]);
if(pow_1 > 0)
{
sprintf(A, "select sf from %scylb where id = %d", name, YY->y_id);
//printf("A = %s\n", A);
ret = mysql_query(conn, A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
pow_2 = atoi(res_row[0]);
if(pow_1 > pow_2)
{
int ret_1, ret_2;
sprintf(A, "delete from %scylb where id = %d", name, YY->y_id);
//printf("A = %s\n", A);
ret_1 = mysql_query(conn, A);
sprintf(A, "delete from %sgrouptable where id = %d", getname_from_id(YY->y_id), YY->q_id);
//printf("A = %s\n", A);
ret_2 = mysql_query(conn,A);
//printf("ret_1 = %d\nret_2 = %d\n", ret_1,ret_2);
if(ret_1 == 0 && ret_2 == 0)
{
sprintf(B, "已踢出此群!\n");
}
}
else
{
sprintf(B, "你没有权限踢人\n");
}
}
mysql_free_result(res_ptr);
}
}
else
{
sprintf(B, "你没有权限踢人\n");
}
//printf("B = %s\n", B);
send(sfd, B, sizeof(B), 0);
}
free(XZ);
}
void G_send(XINXI *YY, int sfd)
{
//printf("AAA\n");
char A[100];
int day, time_1, ret, sum = 0, i;
sprintf(A, "select day from %scylb where id = %d", getgroupname_from_id(YY->q_id), YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\nYY->m_id = %d\nYY->q_id = %d\n", A, ret, YY->m_id, YY->q_id);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
day = atoi(res_row[0]);
}
mysql_free_result(res_ptr);
sprintf(A, "select time from %scylb where id = %d", getgroupname_from_id(YY->q_id), YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\nYY->m_id = %d\nYY->q_id = %d\n", A, ret, YY->m_id, YY->q_id);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
time_1 = atoi(res_row[0]);
}
mysql_free_result(res_ptr);
sum = 0;
sprintf(A, "select * from %sjl where day = %d and time > %d and id != %d", getgroupname_from_id(YY->q_id), day, time_1, YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
sum++;
}
mysql_free_result(res_ptr);
sprintf(A, "select * from %sjl where day > %d and id != %d", getgroupname_from_id(YY->q_id),day,YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
sum++;
}
mysql_free_result(res_ptr);
//printf("sum = %d\n",sum);
send(sfd, &sum, sizeof(sum), 0);
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
sprintf(A, "select * from %sjl where day = %d and time > %d and id != %d", getgroupname_from_id(YY->q_id), day, time_1, YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
//有未读信息
sprintf(A, "select * from %sjl where day = %d and time > %d and id != %d", getgroupname_from_id(YY->q_id), day, time_1, YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr = mysql_store_result(conn);
//int field = mysql_num_fields(res_ptr);
while(res_row = mysql_fetch_row(res_ptr))
{
int ret_1, ret_2;
XZ->zt = 1;
strncpy(XZ->name, res_row[1], sizeof(XZ->name)); //名字
strncpy(XZ->xinxi, res_row[2], sizeof(XZ->xinxi)); //信息
day = atoi(res_row[3]); //该信息时间
time_1 = atoi(res_row[4]);
sprintf(A, "update %scylb set day = %d where id = %d", getgroupname_from_id(YY->q_id), day, YY->m_id);
ret_1 = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
sprintf(A, "update %scylb set time = %d where id = %d", getgroupname_from_id(YY->q_id), time_1, YY->m_id);
ret_2 = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
if(ret_1 == 0 && ret_2 == 0)
{
//printf("%s : %s\n", XZ->name, XZ->xinxi);
send(sfd, XZ, sizeof(LIAOT), 0);
}
}
mysql_free_result(res_ptr);
}
else
{
sum = 0; //无未读信息
}
sprintf(A, "select * from %sjl where day > %d and id != %d", getgroupname_from_id(YY->q_id),day,YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
XZ->zt = 1;
//有未读信息
sprintf(A, "select * from %sjl where day > %d and id != %d", getgroupname_from_id(YY->q_id),day,YY->m_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
int ret_1, ret_2;
XZ->zt = 1;
strncpy(XZ->name, res_row[1], sizeof(XZ->name)); //名字
strncpy(XZ->xinxi, res_row[2], sizeof(XZ->xinxi)); //信息
day = atoi(res_row[3]); //该信息时间
time_1 = atoi(res_row[4]);
sprintf(A, "update %scylb set day = %d where id = %d", getgroupname_from_id(YY->q_id), day, YY->m_id);
ret_1 = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
sprintf(A, "update %scylb set time = %d where id = %d", getgroupname_from_id(YY->q_id), time_1, YY->m_id);
ret_2 = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
if(ret_1 == 0 && ret_2 == 0)
{
send(sfd, XZ, sizeof(LIAOT), 0);
}
}
}
//XZ->zt = 0;
//send(sfd, XZ, sizeof(LIAOT), 0);
}
void G_get(XINXI *YY, int sfd)
{
char A[100];
time_t timep_1;
time(&timep_1);
struct tm *p;
p = gmtime(&timep_1);
int day = (p->tm_year + 1900)*10000 + (p->tm_mon + 1)*100 + p->tm_mday;
int time = (p->tm_hour + 8)*10000 + (p->tm_min)*100 + p->tm_sec;
int ret;
if(strcmp(YY->buf, "exit") == 0)
{
return;
}
sprintf(A, "insert into %sjl (id, name, xinxi, day, time) values (%d, '%s', '%s', %d, %d)",getgroupname_from_id(YY->q_id), YY->m_id, getname_from_id(YY->m_id), YY->buf, day, time);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
}
int group_2(DENN *XX, XINXI *YY, int sfd) //群聊天
{
//printf("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY\n");
int ret;
char A[100], B[50];
YY->m_id = XX->id;
if(YY->ice_2 == 41)
{
//printf("YY->m_id = %d\n", YY->m_id);
sprintf(A, "select * from %sgrouptable where id = %d", getname_from_id(YY->m_id), YY->q_id);
ret = mysql_query(conn,A);
//printf("A = %s\nret = %d\n", A, ret);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
sprintf(B, "ok");
write(sfd, B, sizeof(B));
}
else
{
//用户未加入此群
sprintf(B, "你未加入该群!");
write(sfd, B, sizeof(B));
}
}
}
int TongZ(XINXI *YY, int sfd)
{
char A[100],B[50], ch;
int ret, field, sum = 0, q[20], m = 0;
//printf("id = %d\n", YY->id);
//printf("m_id = %d\n", YY->m_id);
sprintf(A, "select message from box where end_id = %d", YY->m_id);
ret = mysql_query(conn,A);
if(ret)
{
printf("wrong!\n");
}
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
//printf("field = %d\n", field);
while(res_row=mysql_fetch_row(res_ptr))
{
sum++;
//printf("sum1 = %d\n", sum);
}
mysql_free_result(res_ptr);
sprintf(A, "select id from %sgrouptable where sf > 0",getname_from_id(YY->m_id));
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
//printf("AAAA\n");
int ID = atoi(res_row[0]);
sprintf(A, "select message from box where end_id = %d", ID);
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr_1;
MYSQL_ROW res_row_1;
res_ptr_1 = mysql_store_result(conn);
/*
res_row_1 = mysql_fetch_row(res_ptr_1);
if(res_row_1 == NULL)
{
printf("AAAA\n");
}
else
{
printf("BBB\n");
}
*/
do
{
res_row_1 = mysql_fetch_row(res_ptr_1);
if(res_row_1 != NULL)
{
sum++;
q[m] = ID;
m++;
}
else
{
break;
}
}while(1);
mysql_free_result(res_ptr_1);
}
mysql_free_result(res_ptr);
//printf("sum2 = %d\n", sum);
//printf("m = %d\n", m);
write(sfd, &sum, sizeof(int));
read(sfd, B, sizeof(B)); //接收客户端选择
if(strcmp(B, "A") == 0)
{
sprintf(A, "select message from box where end_id = %d and ice = 0", YY->m_id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
//printf("field = %d\n", field);
while(res_row=mysql_fetch_row(res_ptr))
{
sprintf(B, "%s", res_row[0]);
write(sfd, B, sizeof(B));
}
mysql_free_result(res_ptr);
for(int i=0; i < m;i++)
{
sprintf(A, "select message from box where end_id = %d and ice = 1", q[i]);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
//printf("field = %d\n", field);
while(res_row=mysql_fetch_row(res_ptr))
{
sprintf(B, "%s", res_row[0]);
write(sfd, B, sizeof(B));
}
mysql_free_result(res_ptr);
}
sprintf(B, "over");
write(sfd, B, sizeof(B));
}
else if(strcmp(B, "B") == 0)
{
//printf("AAA\n");
sprintf(A, "select * from box where end_id = %d and ice = 0", YY->m_id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
//printf("field = %d\n", field);
while(res_row=mysql_fetch_row(res_ptr))
{
//printf("CCC\n");
int i,s_id, e_id;
for(i = 0; i < field; i++)
{
if(i == 0)
{
s_id = atoi(res_row[i]);
}
else if(i == 1)
{
e_id = atoi(res_row[i]);
}
}
sprintf(XZ->buf, "%s", res_row[i]);
XZ->zt = 0; //加好友
send(sfd, XZ, sizeof(LIAOT), 0);
recv(sfd, XZ, sizeof(LIAOT), 0);
//printf("buf = %s\n",XZ->buf);
if(strcmp(XZ->buf, "Y") == 0) //群人分开
{
//人
int ret_1,ret_2,ret_3;
//printf("s_id = %d\te_id = %d\n",s_id,e_id);
sprintf(A, "insert into %shylb (id, beizhu, jl, zt) values (%d, '%s', '%s', 0)", getname_from_id(e_id),s_id,XZ->beizhu,getjl_from_id(s_id,e_id));
ret_1 = mysql_query(conn,A); //添加到好友列表
sprintf(A, "insert into %shylb (id, beizhu, jl, zt) values (%d, '%s', '%s', 0)", getname_from_id(s_id),e_id,res_row[field-1],getjl_from_id(s_id,e_id));
ret_2 = mysql_query(conn,A); //添加到好友列表
sprintf(A, "create table %s (from_id int, xinxi varchar(200), end_id int, zt int)", getjl_from_id(s_id,e_id));
ret_3 = mysql_query(conn,A); //创建信息表
sprintf(A, "delete from box where from_id = %d and end_id = %d and zt = 0", s_id, e_id);
ret = mysql_query(conn,A); //删除该通知
if(ret == 0 && ret_1 == 0 && ret_2 == 0 && ret_3 == 0)
sprintf(B, "添加成功!\n");
else
sprintf(B, "添加失败!\n");
strncpy(XZ->buf, B, sizeof(B));
send(sfd, XZ, sizeof(LIAOT), 0);
}
else if(strcmp(XZ->buf, "N") == 0)
{
sprintf(A, "delete from box where from_id = %d and end_id = %d", s_id, e_id);
mysql_query(conn,A); //删除该通知
}
}
for(int i=0; i < m;i++)
{
//printf("BBB\n");
sprintf(A, "select * from box where end_id = %d and ice = 1", q[i]);
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
//printf("field = %d\n", field);
while(res_row=mysql_fetch_row(res_ptr))
{
//printf("YYY\n");
int i,s_id, e_id,I;
for(i = 0; i < 2; i++)
{
if(i == 0)
{
s_id = atoi(res_row[i]);
}
else if(i == 1)
{
e_id = atoi(res_row[i]);
}
}
sprintf(XZ->buf, "%s", res_row[i]);
XZ->zt = 1; //加群
//ice++;
//XZ->ice = ice;
///printf("ice = %d\n", ice);
send(sfd, XZ, sizeof(LIAOT), 0);
//printf("XZ->buf = %s\n", XZ->buf);
memset(XZ, 0, sizeof(LIAOT));
recv(sfd, XZ, sizeof(LIAOT), 0);
//mysql_free_result(res_ptr);
if(strcmp(XZ->buf, "Y") == 0)
{
time_t timep_1;
time (&timep_1);
struct tm *p;
p = gmtime(&timep_1);
int day = (p->tm_year + 1900)*10000 + (p->tm_mon + 1)*100 + p->tm_mday;
int time = (p->tm_hour + 8)*10000 + (p->tm_min)*100 + p->tm_sec;
int ret_1, ret_2, ret_3;
sprintf(A, "insert into %sgrouptable (id, name, sf) values (%d, '%s', 0)", getname_from_id(s_id), e_id, getgroupname_from_id(e_id));
//printf("A = %s\n", A);
ret_1 = mysql_query(conn,A); //添加到已加群表
sprintf(A, "insert into %scylb (id, name, sf, day, time) values (%d, '%s', 0, %d, %d)", getgroupname_from_id(e_id), s_id, getname_from_id(s_id), day, time);
//printf("A = %s\n", A);
ret_2 = mysql_query(conn,A);
sprintf(A, "delete from box where from_id = %d and end_id = %d and ice = 1", s_id, e_id);
//printf("A = %s\n", A);
ret = mysql_query(conn,A); //删除该通知
if(ret_1 == 0 && ret_2 == 0 && ret == 0)
sprintf(B, "添加成功!\n");
else
sprintf(B, "添加失败!\n");
strncpy(XZ->buf, B, sizeof(B));
send(sfd, XZ, sizeof(LIAOT), 0);
}
else if(strcmp(XZ->buf, "N") == 0)
{
sprintf(A, "delete from box where from_id = %d and end_id = %d", s_id, e_id);
mysql_query(conn,A); //删除该通知
}
}
//printf("x = %d\n", x);
}
memset(XZ, 0, sizeof(LIAOT));
XZ->ice = 100;
sprintf(XZ->buf, "over");
send(sfd,XZ, sizeof(XZ), 0);
mysql_free_result(res_ptr);
free(XZ);
return 0; //结束循环
}
else if(strcmp(B, "R") == 0)
{
return 0; //结束循环
}
}
int set_1(int id) //设置为忙碌
{
char A[100];
sprintf(A, "update student set sj = 1 where id = %d", id);
int ret = mysql_query(conn,A);
if(ret == 0)
{
return 0;
}
return 1;
}
int drop_1(int id) //设置为空闲
{
char A[100];
sprintf(A, "update student set sj = 0 where id = %d", id);
int ret = mysql_query(conn,A);
if(ret == 0)
{
return 0;
}
return 1;
}
int panduan_1(int id) //判断客户是否空闲
{
char A[100];
int sj, res, field;
//printf("HHH %d HHH\n", id);
sprintf(A, "select sj from student where id = %d", id);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
if(res_row == NULL)
{
printf("NULL\n");
}
sj = atoi(res_row[0]);
return sj;
}
int panduan_2();
int ADD(XINXI *YY, DENN *XX) //添加好友
{
int field, fd, ret;
if(YY->y_id == XX->id)
{
//printf("YYY\n");
ret = -2;
return ret;
}
char B[50];
sprintf(B, "用户%s请求添加你为好友!", XX->name);
char A[100];
//printf("id = %d\n", XZ->id);
sprintf(A,"select * from %shylb where id = %d", getname_from_id(XX->id), YY->y_id);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
ret = -1; //该用户已经是好友
return ret;
}
sprintf(A, "insert into box (from_id,end_id,message,ZT,ice,beizhu) values (%d,%d,'%s',0,0,'%s')", XX->id, YY->y_id, B, YY->beizhu);
ret = mysql_query(conn,A);
if(ret)
{
printf("wrong!\n");
return 0; //信息发送失败
}
else
{
return 1; //信息发送成功
}
/*
sprintf(A, "select fd from student where id = %d", XZ->id); //XZ->id出错
int res = mysql_query(conn,A);
if(res)
{
printf("wrong!!!\n");
}
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
res_row=mysql_fetch_row(res_ptr);
printf("B\n");
if(res_row == NULL)
{
printf("over\n");
}
else
{
fd = atoi(res_row[0]); //获取目标好友的套接字码
printf("fd = %d\n", fd);
}
*/
}
int xuanzhe_1(DENN *XX, XINXI *YY, int sfd)
{
int ret;
char buf[50],A[100];
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
if(YY->ice_2 == 21) //好友的添加,删除,查询
{
char B[50];
if(YY->ice_3 == 211) //添加好友
{
//printf("sfd = %d\n", sfd);
ret = ADD(YY, XX);
if(ret == 1)
{
sprintf(B, "信息发送成功!\n");
}
else if(ret == 0)
{
sprintf(B, "信息发送失败!\n");
}
else if(ret == -1)
{
sprintf(B, "该用户已经是好友,无需再添加!\n");
}
else if(ret == -2)
{
sprintf(B, "该ID为当前帐号!\n");
}
write(sfd, B, sizeof(B));
}
else if(YY->ice_3 == 212)
{
if(YY->zt == 1) //根据ID删除好友
{
int ret_1,ret_2,ret_3;
sprintf(A, "delete from %shylb where id = %d", getname_from_id(XX->id), YY->y_id);
ret_1 = mysql_query(conn,A);
sprintf(A, "delete from %shylb where id = %d", getname_from_id(YY->y_id), XX->id);
ret_2 = mysql_query(conn,A);
sprintf(A, "drop table %s", getjl_from_id(XX->id, YY->y_id));
ret_3 = mysql_query(conn,A);
if(ret_1 == 0 && ret_2 == 0 && ret_3 == 0)
{
sprintf(buf, "删除成功!\n");
write(sfd, buf, sizeof(buf));
}
else
{
sprintf(buf, "删除失败!\n");
write(sfd, buf, sizeof(buf));
}
}
else if(YY->zt == 2) //根据备注删除好友
{
int ID = getid_from_beizhu(XX->id, YY->beizhu);
YY->y_id = ID;
sprintf(A, "delete from %shylb where id = %d", getname_from_id(XX->id), YY->y_id);
ret = mysql_query(conn,A);
sprintf(A, "delete from %shylb where id = %d", getname_from_id(YY->y_id), XX->id);
if(ret)
ret = mysql_query(conn,A);
sprintf(A, "drop table %s", getjl_from_id(XX->id, YY->y_id));
if(ret)
ret = mysql_query(conn,A);
if(ret == 0)
{
sprintf(buf, "删除成功!\n");
write(sfd, buf, sizeof(buf));
}
else
{
sprintf(buf, "删除失败!\n");
write(sfd, buf, sizeof(buf));
}
}
}
else if(YY->ice_3 == 213)
{
if(YY->zt == 1) //根据ID查询备注
{
sprintf(A, "select beizhu from %shylb where id = %d", getname_from_id(XX->id), YY->y_id);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
sprintf(buf, "ID为%d的好友备注为:%s\n", YY->y_id, res_row[0]);
write(sfd,buf, sizeof(buf));
}
mysql_free_result(res_ptr);
}
else if(YY->zt == 2) //根据备注查询ID
{
sprintf(A, "select id from %shylb where beizhu = '%s'", getname_from_id(XX->id), YY->beizhu);
ret = mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
sprintf(buf,"备注为%s的好友ID为:%s\n", YY->beizhu, res_row[0]);
write(sfd,buf, sizeof(buf));
}
mysql_free_result(res_ptr);
}
}
}
else if(YY->ice_2 == 22) //查看好友列表
{
int field;
char B[50], A[100];
sprintf(A, "select * from %shylb", getname_from_id(XX->id));
int res = mysql_query(conn,A);
if(res)
{
printf("wrong!!!\n");
}
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
//printf("field = %d\n", field);
memset(B, 0, sizeof(B));
while(res_row=mysql_fetch_row(res_ptr) )
{
for(int i=0;i<2;i++)
{
if(i != 2)
{
//printf("%s\n", res_row[i]);
strncat(B, res_row[i],strlen(res_row[i]));
strcat(B, "\t");
}
}
write(sfd, B, sizeof(B));
memset(B, 0, sizeof(B));
}
//printf("CCC\n");
sprintf(B, "over");
write(sfd, B, sizeof(B));
mysql_free_result(res_ptr);
}
else if(YY->ice_2 == 23) //查看好友状态
{
int field;
char A[100];
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
sprintf(A, "select zt from student where id = %d", YY->y_id);
int res = mysql_query(conn,A);
if(res)
{
printf("wrong!!!\n");
}
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr);
res_row=mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
XZ->zt = atoi(res_row[0]);
//printf("XZ->zt = %d\n", XZ->zt);
}
mysql_free_result(res_ptr);
sprintf(A, "select * from %shylb where id = %d", getname_from_id(XX->id), YY->y_id);
//printf("A = %s\n", A);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row == NULL)
{
XZ->zt = -1; //没有此ID的好友
}
mysql_free_result(res_ptr);
send(sfd, XZ, sizeof(LIAOT), 0);
}
else if(YY->ice_2 == 24) //查看聊天记录
{
//printf("AAAAAAAAAAAAAAAAAA\n");
int field;
char A[100];
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
//printf("XX->id = %d\tYY->y_id = %d\n",XX->id,YY->y_id);
sprintf(A, "select * from %s", getjl_from_id(XX->id, YY->y_id));
int res = mysql_query(conn,A);
if(res)
{
printf("wrong!!!\n");
}
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
//printf("field = %d\n", field);
while(res_row=mysql_fetch_row(res_ptr))
{
for(int i = 0;i<field;i++)
{
if(i == 0)
{
//printf("11111111111\n");
XZ->id = atoi(res_row[i]);
//printf("2222222222222\n");
strncpy(XZ->beizhu, getbeizhu_from_id(XX->id,XZ->id),sizeof(XZ->beizhu));
//printf("33333333333\n");
}
if(i == 1)
{
//printf("444444444444\n");
strncpy(XZ->xinxi, res_row[i], sizeof(XZ->xinxi));
//printf("5555555555555\n");
}
}
send(sfd, XZ, sizeof(LIAOT), 0);
}
//printf("over\n");
sprintf(XZ->xinxi, "over");
send(sfd, XZ, sizeof(LIAOT), 0);
mysql_free_result(res_ptr);
}
else if(YY->ice_2 == 25) //屏蔽好友信息 或取消屏蔽
{
int field;
sprintf(A, "select * from %shylb where id = %d",getname_from_id(XX->id), YY->y_id);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
ret = mysql_query(conn,A);
if(ret)
{
printf("wrong!!!\n");
}
res_ptr = mysql_store_result(conn);
field = mysql_num_fields(res_ptr);
res_row=mysql_fetch_row(res_ptr);
if(res_row != NULL)
{
if(YY->ice_3 == 251)
{
sprintf(A, "update %shylb set zt = -1 where id = %d", getname_from_id(XX->id), YY->y_id);
ret = mysql_query(conn,A);
sprintf(buf, "屏蔽成功!\n");
}
if(YY->ice_3 == 252)
{
sprintf(A, "update %shylb set zt = 0 where id = %d", getname_from_id(XX->id), YY->y_id);
ret = mysql_query(conn,A);
sprintf(buf, "取消屏蔽成功!\n");
}
}
else
{
sprintf(buf, "你无此ID的好友!\n");
}
write(sfd, buf, sizeof(buf));
mysql_free_result(res_ptr);
}
free(XZ);
return 0;
}
void HY_send(XINXI *YY,int sfd) //将未读信息发送给客户端
{
//printf("BBBA\n");
int ret,sum = 0;
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
char A[100],B[50],beizhu[20];
strncpy(YY->jl, getjl_from_id(YY->m_id, YY->y_id), sizeof(YY->jl));
//printf("YY->jl = %s\tYY->m_id = %d\n",YY->jl,YY->m_id);
sprintf(A, "select xinxi from %s where end_id = %d and zt = 1", YY->jl, YY->m_id);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
sum++;
}
XZ->zt = sum;
send(sfd, XZ, sizeof(LIAOT), 0);
if(sum > 0)
{
mysql_free_result(res_ptr);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
while(res_row = mysql_fetch_row(res_ptr))
{
//printf("res_row: %s\n", res_row[0]);
strncpy(XZ->beizhu, getbeizhu_from_id(YY->m_id,YY->y_id), sizeof(XZ->beizhu));
strncpy(XZ->xinxi, res_row[0], sizeof(XZ->xinxi));
send(sfd, XZ, sizeof(LIAOT), 0);
sprintf(A, "update %s set zt = 0 where zt = 1 and end_id = %d and xinxi = '%s'", YY->jl, YY->m_id, XZ->xinxi);
ret = mysql_query(conn,A); //将未读更新为已读
//printf("ret = %d", ret);
//printf("%s : %s\n", XZ->beizhu, XZ->xinxi);
}
mysql_free_result(res_ptr);
}
/*
if(sum > 0)
{
sprintf(A, "update %s set zt = 0 where zt = 1", YY->jl);
ret = mysql_query(conn,A); //将未读更新为已读
//printf("ret = %d", ret);
}
*/
free(XZ);
//printf("AAA\n");
}
void HY_get(XINXI *YY, int id, int sfd)
{
//printf("CCCCCCCCCCCcCCCCC\n");
char A[100];//B[50],buf[50],beizhu[20];
int ret;//,field,zt;
//TX(YY);
//printf("YY->buf: %s\n", YY->buf);
if(strcmp(YY->buf, "exit") == 0)
{
//pthread_join(thid, NULL);
return;
}
sprintf(A, "select * from %shylb where id = %d and zt = 0", getname_from_id(YY->m_id), YY->y_id);
mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
sprintf(A, "select * from %shylb where id = %d and zt = 0", getname_from_id(YY->y_id), YY->m_id);
mysql_query(conn,A);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row != NULL)
{
sprintf(A, "insert into %s (from_id, xinxi, end_id, zt) values (%d, '%s', %d, 1)",getjl_from_id(id,YY->y_id), id, YY->buf, YY->y_id);
//printf("%s\n",A);
ret = mysql_query(conn,A);
//printf("ret = %d\n\n\n", ret);
//printf("DDDDDDDDDDDDDDDDDDDDDDDD\n");
}
}
}
int xuanzhe_2(DENN *XX, XINXI *YY, int sfd)
{
int id, ret;
char buf[50], A[100];
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
//获取未浏览信息数量
YY->m_id = XX->id;
//ret = recv(sfd, XZ, sizeof(LIAOT), 0);
if(YY->ice_2 == 31)
{
//判断是否存在该好友
//printf("name: %s\tYY->y_id = %d\n", getname_from_id(XX->id), YY->y_id);
strncpy(YY->name, getname_from_id(XX->id), sizeof(YY->name));
sprintf(A, "select * from %shylb where id = %d", YY->name, YY->y_id);
ret = mysql_query(conn,A);
//printf("rett = %d\n", ret);
if(!ret)
{
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row != NULL) //存在该好友
{
mysql_free_result(res_ptr);
//printf("YY->m_id = %d\n", YY->m_id);
//printf("YY->y_id = %d\n", YY->y_id);
strncpy(YY->jl, getjl_from_id(YY->m_id, YY->y_id), sizeof(YY->jl));
strncpy(YY->beizhu, getbeizhu_from_id(YY->m_id, YY->y_id), sizeof(YY->beizhu));
sprintf(buf, "OK");
write(sfd, buf, sizeof(buf));
sleep(1);
//HY_send(YY,sfd); //将未读信息发送个客户端
S_FD = sfd;
S_ID = YY->y_id;
E_ID = XX->id;
//pthread_create(&thid, NULL, thread, NULL);
return 0; //回到epoll,让epoll来随时接受信息
}
else if(res_row == NULL) //不存在该好友
{
sprintf(buf, "不存在该好友!\n");
write(sfd, buf, sizeof(buf));
}
mysql_free_result(res_ptr);
}
}
}
int denglu(XINXI *YY,int sfd) //登陆
{
int row, res, ret;
char A[100],B[50];
//printf("ID = %d\n", XX->id);
sprintf(A, "select zt from student where id = %d", YY->m_id);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row == NULL)
{
//id错误
strncpy(B,"无此id",50);
ret = 0;
}
else if(atoi(res_row[0]) == 1)
{
sprintf(B, "该帐号已在其他地方登陆!");
}
else
{
sprintf(A, "select id from student where id = %d and password = %s", YY->m_id, YY->password);
res = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
if(res_row == NULL)
{
//password错误
strncpy(B,"密码错误",50);
ret = 0;
}
else
{
sprintf(A, "update student set zt=1 where id = %d", YY->m_id); //在线状态
mysql_query(conn, A);
sprintf(A, "update student set fd=%d where id = %d", sfd, YY->m_id); //在线状态
mysql_query(conn, A);
strncpy(B,"登陆成功",50);
ret = 1;
}
mysql_free_result(res_ptr);
}
write(sfd, B, sizeof(B));
return ret;
}
void logof(XINXI *YY, int sfd)
{
int ret_1, ret_2;
char A[100];
sprintf(A, "update student set zt = 0 where id = %d", YY->m_id);
ret_1 = mysql_query(conn,A); //用户退出,将状态设置为不在线
sprintf(A, "update student set fd = 0 where id = %d",YY->m_id);
ret_2 = mysql_query(conn,A); //用户退出后将fd归
if(ret_1 != 0 || ret_2 != 0)
{
printf("退出登陆信息更新失败!!!\n");
}
}
int zhuce(XINXI *YY,int sfd) //注册
{
int res, ret, ret_1, ret_2,ret_3;
char A[100],B[50];
//printf("YY->m_id = %d\n", YY->m_id);
sprintf(A, "select id from student where id = %d", YY->m_id);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row)
{
//id已经存在
ret = 0;
strncpy(B,"id已经被注册,请更换一个id",50);
}
else
{
sprintf(A, "select id from student where id = %d and name = %s", YY->m_id, YY->name);
res = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(res_row) //name已存在
{
ret = 0;
strncpy(B, "name已经被注册,请更换一个name", 50);
}
else
{
//strncpy(YY->hylb, YY->name, sizeof(YY->hylb));
//printf("AAA\n");
sprintf(A, "insert into student (id,password,name,qu,an,hylb,zt,sj,grouptable) values (%d, '%s', '%s', '%s', '%s', '%shylb', 0, 0, '%sgrouptable')",YY->m_id, YY->password,YY->name,YY->qu,YY->an, YY->name, YY->name);
//printf("%s\n",A);
//return 0;
ret_1 = mysql_query(conn,A);
//printf("ret_1 = %dBBB\n", ret_1);
sprintf(A, "create table %s (id int, beizhu varchar(20), jl varchar(20), zt int)", YY->hylb);
ret_2 = mysql_query(conn,A); //创建好友列表
//printf("ret_2 = %dCCC\n", ret_2);
sprintf(A, "create table %sgrouptable (id int, name varchar(20),sf int)", YY->name);
ret_3 = mysql_query(conn,A); //创建已加群列表
//printf("ret_3 = %dDDD\n", ret_3);
if(ret_1 == 0 && ret_2 == 0 && ret_3 == 0)
{
ret = 1;
sprintf(B,"注册成功");
}
else
{
sprintf(B, "注册失败");
ret = 0;
}
}
}
//printf("A\n");
write(sfd, B, sizeof(B));
return ret;
}
int zhaohui(XINXI *YY,int sfd) //找回密码
{
int res, field, ret;
char A[200],B[50],C[100];
sprintf(A, "select qu from student where id = %d", YY->m_id);
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
res = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
//printf("AA%dAA\n", field);
//printf("%s\n",res_row[0]);
strncpy(A,res_row[0],200);
write(sfd, A, sizeof(A)); //将密保问题发送到客户端
mysql_free_result(res_ptr);
return 1;
}
int zhaohui_1(XINXI *YY,int sfd) //找回密码
{
int ret,field;
char A[100],B[50];
MYSQL_RES *res_ptr;
MYSQL_ROW res_row;
sprintf(A,"select an from student where id = %d AND an = '%s'",YY->m_id, YY->an);
//printf("%d\t%s\n",XX->id,XX->an);
mysql_query(conn, A);
res_ptr = mysql_store_result(conn);
MYSQL_ROW row = mysql_fetch_row(res_ptr);
mysql_free_result(res_ptr);
if(row == NULL)
{
strncpy(B,"答案错误",50);
ret = 0;
}
else
{
sprintf(A, "select password from student where an = '%s'", YY->an);
ret = mysql_query(conn,A);
res_ptr = mysql_store_result(conn);
res_row = mysql_fetch_row(res_ptr);
field = mysql_num_fields(res_ptr); //返回你这张表有多少列
sprintf(B,"密码为:%s",res_row[0]);
mysql_free_result(res_ptr);
ret = 1;
}
write(sfd, B, sizeof(B)); //将密码发送到客户端
return ret;
}
void TX(XINXI *YY)
{
printf("ice_1 = %d\n", YY->ice_1);
printf("ice_2 = %d\n", YY->ice_2);
printf("ice_3 = %d\n", YY->ice_3);
printf("ice_4 = %d\n", YY->ice_4);
printf("zt = %d\n", YY->zt);
printf("m_id = %d\n", YY->m_id);
printf("y_id = %d\n", YY->y_id);
printf("q_id = %d\n", YY->q_id);
//printf("name = %s\n", YY->name);
printf("password = %s\n", YY->password);
//printf("buf = %s\n", YY->buf);
printf("*****************\n");
}
server.c
#include "ser.h"
int main()
{
char A[100];
conn = mysql_init(conn); //初始化一个句柄
mysql_library_init(0,NULL,NULL); //初始化数据库
if(conn == NULL)
{
printf("mysql_init failed!!!\n");
exit(1);
}
conn = mysql_real_connect(conn,"127.0.0.1","root","181219","db1",0,NULL,0); //连接数据库
mysql_set_character_set(conn,"utf8"); //调整为中文字符
if(conn == NULL)
{
printf("mysql_real_connect failed!!!\n");
exit(1);
}
sprintf(A, "update student set zt=0"); //服务器重启,所有用户都不在线
mysql_query(conn,A);
sprintf(A, "update student set fd=0"); //服务器重启,所有用户都不在线
mysql_query(conn,A);
sprintf(A, "update student set sj=0"); //服务器重启,所有用户都不在线
mysql_query(conn,A);
//创建epoll线程
for(int i = 0;i < 5; i++)
{
EPOL[i].sum = 0;
E_LIST *p = &EPOL[i];
EPOL[i].efd = epoll_create(OPEN_MAX);
pthread_create(&thid[i], NULL, thread_account, p);
}
int cfd, lfd, sfd, ret, n, i, maxi, efd, nready, PD;
struct sockaddr_in caddr, saddr;
char buf[50], str[INET_ADDRSTRLEN]; //缓存区
socklen_t clen = sizeof(struct sockaddr_in);;
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9999); //端口号9999
saddr.sin_addr.s_addr = htonl(INADDR_ANY); //TCP
lfd = socket(AF_INET, SOCK_STREAM, 0); //创建套接字
int mw_optval = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&mw_optval,sizeof(mw_optval));
bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)); //绑定端口
listen(lfd, 128); //设置监听套接字
int j = 0;
Lock = 0;
while(1)
{
//printf("AAAAAAAAA\n");
cfd = accept(lfd, (struct sockaddr *)&caddr, &clen); //连接套接字
printf("connection--port:%d\tip:%s\n", ntohs(caddr.sin_port), inet_ntop(AF_INET, &caddr.sin_addr.s_addr, str, sizeof(str)));
//printf("cfd = %d\n", cfd);
time_t timep_1;
time(&timep_1);
struct tm *p;
p = gmtime(&timep_1);
sprintf(A, "%4d/%2d/%2d %2d/%2d/%2d\t connection--port:%d\tip:%s\tclient_fd:%d\n",p->tm_year + 1900,p->tm_mon + 1,p->tm_mday
,p->tm_hour + 8,p->tm_min,p->tm_sec,ntohs(caddr.sin_port), inet_ntop(AF_INET, &caddr.sin_addr.s_addr, str, sizeof(str)),cfd);
int fd = open("server_log.txt", O_WRONLY | O_APPEND);
write(fd, A, strlen(A));
close(fd);
if(j == 5)
{
j = 0;
}
EPOL[j].tep.events = EPOLLIN;
EPOL[j].tep.data.fd = cfd;
EPOL[j].sum++;
//EPOL[j].efd = cfd;
//printf("sfd = %d\n", cfd);
epoll_ctl(EPOL[j].efd, EPOLL_CTL_ADD, cfd, &(EPOL[j].tep)); //将监听套接字加入等待队列
j++;
}
//printf("BBB\n");
close(lfd);
return 0;
}
客户端
cli.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mariadb/mysql.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <errno.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <assert.h>
#include <libgen.h>//basename():从路径中获取文件名及后缀
MYSQL *conn;
#define OPEN_MAX 1024
typedef struct xinxi{
int ice_1; //功能选择
int ice_2;
int ice_3;
int ice_4;
int m_id; //客户id
int y_id; //好友id
int q_id; //群id
int fd; //套接字码
int zt; //状态
char name[20]; //name
char password[16]; //密码
char qu[200]; //问题
char an[100]; //答案
char beizhu[20]; //备注
char hylb[20]; //好友列表
char cylb[20]; //成员列表
char jl[20]; //聊天记录
char buf[200]; //信息内容
}XINXI;
typedef struct denn{
int ice; //功能选择
int id; //id
int zt; //状态
char name[20]; //name
char password[16]; //密码
char qu[200]; //问题
char an[100]; //答案
char hylb[20]; //好友列表
}DENN;
typedef struct liaot{
int ice;
int id;
int zt;
char name[20];
char qu[200];
char an[100];
char buf[50];
char beizhu[20];
char xinxi[200];
}LIAOT;
typedef struct E_list{
int index;
int efd;
int sum;
struct epoll_event tep, ep[OPEN_MAX];
struct E_list *next;
}E_LIST;
typedef struct Doc{
int sign; //
char buf[1024];
}DOC;
int Socket_fd, Y_ID, M_ID, Q_ID, M;
void C_denn(XINXI *YY, int socket_fd);
void C_zhuce(XINXI *YY, int socket_fd);
void C_zhaohui(XINXI *YY, int socket_fd);
void C_haoy(XINXI *YY, DENN *XX, int socket_fd);
void C_haoyouliaot(XINXI *YY, DENN *XX, int socket_fd);
void C_group(XINXI *YY, DENN *XX, int socket_fd);
void C_group_com(XINXI *YY, DENN *XX, int socket_fd);
int C_TongZ(XINXI *YY, DENN *XX, int socket_fd);
void C_get(int socket_fd);
void C_document(XINXI *YY, int socket_fd);
void get_XX(int socket_fd);
void *thread(void *arg);
void *thread_g(void *arg);
void face(XINXI *YY);
cli.c
#include "cli.h"
void get_XX(int socket_fd)
{
XINXI YY;
char buf[50];
YY.m_id = M_ID;
YY.ice_1 = 555;
send(socket_fd, &YY, sizeof(XINXI), 0);
do
{
recv(socket_fd, buf, sizeof(buf), 0);
if(strcmp(buf, "over") == 0)
{
break;
}
printf("\t\t%s\n", buf);
}while(1);
//printf("SSSSSS\n");
}
void C_document(XINXI *YY, int socket_fd)
{
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
int n, fd, ret, id;
char file_len[16], file_name[128],file_path[128],sign[10];
char buf[1024];
setbuf(stdin, NULL);
printf("\t\t********************1 文件发送 *********************\n");
printf("\t\t********************2 文件接收 *********************\n");
printf("\t\t********************0 退出 *********************\n");
printf("\t\t请输入你的选择:");
scanf("%d", &n);
printf("\t\t请输入好友ID:");
scanf("%d", &id);
YY->y_id = id;
if(n == 0)
{
return ;
}
else if(n == 1) //文件发送
{
setbuf(stdin, NULL);
printf("\t\t请输入文件:");
scanf("%s", &file_path);
memset(file_name, 0, sizeof(file_name));
strncpy(file_name, basename(file_path), sizeof(file_name));
fd = open(file_path, O_RDWR);
if(fd == -1)
{
printf("\t\t打开文件失败!!!\n");
return ;
}
int len = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET); //文件光标移动到开始位置
YY->ice_2 = 71;
sprintf(YY->buf, "%d", len);
strncpy(YY->qu, file_name, sizeof(file_name));
send(socket_fd, YY, sizeof(XINXI),0);
int send_len = 0; //记录发送的字节数
int s_ice = 1;
DOC *D = (DOC*)malloc(sizeof(DOC));
while(1)
{
memset(D, 0, sizeof(DOC));
memset(sign, 0, sizeof(sign));
ret = read(fd, D->buf, sizeof(D->buf));
if(ret <= 0)
{
close(fd);
printf("\t\t文件%s发送成功!!!\n", file_name);
break;
}
D->sign = s_ice;
send(socket_fd, D, sizeof(DOC), 0);
recv(socket_fd, sign, sizeof(sign), 0);
printf("sign = %s\n", sign);
if(strcmp(sign, "ok") == 0)
{
printf("\t\tlen = %d\n\t\tsend_len = %d\n", len, send_len);
send_len += ret;
++s_ice;
}
else if(strcmp(sign, "no") == 0)
{
while(1)
{
send(socket_fd, D, sizeof(DOC), 0);
recv(socket_fd, sign, sizeof(sign), 0);
if(strcmp(sign, "ok") == 0)
{
printf("\t\tlen = %d\n\t\tsend_len = %d\n", len, send_len);
send_len += ret;
++s_ice;
break;
}
}
}
else
{
exit(-1);
}
if(send_len == len)
{
close(fd);
printf("\t\t文件%s发送成功!!!\n", file_name);
break;
}
//sleep(1);
}
printf("\t\tlen = %d\n\t\tsend_len = %d\n", len, send_len);
}
else if(n == 2) //文件接收
{
YY->ice_2 = 72;
send(socket_fd, YY, sizeof(XINXI),0);
int ret, fd, sum,i;
char file_len[16], file_name[128], buf[1024],file_new_name[128],sign[10];
read(socket_fd, &sum, sizeof(int));
printf("\t\t有%d份文件需要接收!!!\n", sum);
if(sum == 0)
{
return ;
}
for(i = 0; i < sum; i++)
{
printf("\t\t等待文件传入!\n");
recv(socket_fd, XZ, sizeof(LIAOT), 0);
strncpy(file_len, XZ->beizhu, sizeof(file_len));
strncpy(file_name, XZ->xinxi, sizeof(file_name));
printf("\t\t文件名:%s\n文件大小:%s\n", file_name, file_len);
sprintf(file_new_name, "c-%s", file_name);
//printf("\t\t%s", file_new_name);
fd = open(file_new_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
//printf("fd = %d\n", fd);
int size = atoi(file_len), r_ice = 1;
int write_len = 0, s_size = 1024;
DOC *D = (DOC*)malloc(sizeof(DOC));
while(1)
{
memset(D, 0, sizeof(DOC));
recv(socket_fd, D, sizeof(DOC), 0);
printf("D->sign = %d\n", D->sign);
printf("r_ice = %d\n", r_ice);
if(D->sign == r_ice)
{
if(write_len + 1024 > size)
{
s_size = size - write_len;
}
ret = write(fd, D->buf, s_size);
write_len += ret;
++r_ice;
sprintf(sign, "ok");
printf("file_len = %d\nwrite_len = %d\n", size, write_len);
}
else
{
sprintf(sign, "no");
}
send(socket_fd, sign, sizeof(sign), 0);
if(write_len >= size)
{
close(fd);
break;
}
}
printf("\t\tsize = %d\nwrite_len = %d\n", size, write_len);
}
}
free(XZ);
//return;
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
//printf("AAAAAAA\n");
}
void *thread_g(void *arg)
{
int n;
LIAOT XZ;
XINXI YY;
do
{
sleep(5);
//printf("AAAAAA\n");
//pthread_mutex_lock(&lock); //加锁
YY.ice_4 = 999;
YY.ice_1 = 5;
YY.m_id = M_ID;
//printf("mid = %d\tyid = %d\n",M_ID,YY->y_id);
YY.q_id = Q_ID;
send(Socket_fd, &YY, sizeof(XINXI), 0);
recv(Socket_fd, &n, sizeof(n), 0);
//printf("n = %d\n", n);
while(n > 0)
{
recv(Socket_fd, &XZ, sizeof(LIAOT), 0);
//printf("XZ->zt = %d\n",XZ.zt);
if(XZ.zt > 0)
{
printf("\b\b\b\b\b\b \n");
printf("\t\t\033[34m%s : %s\033[0m", XZ.name, XZ.xinxi);
}
--n;
if(n == 0)
{
printf("\t\t\033[31mMINE :");
}
}
}while(M == 1);
}
void C_group_com(XINXI *YY, DENN *XX, int socket_fd) //群聊天
{
pthread_t thid;
int id,ret,ic,N = 0;
char beizhu[20],buf[50];
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
get_XX(socket_fd);
printf("\t\t**************************************\n");
printf("\t\t*************0 退出 *******************\n");
printf("\t\t*************1 群聊天 **************\n");
printf("\t\t请输入你的选择:");
scanf("%d", &ic);
printf("\033c");
if(ic == 0)
{
return;
}
else if(ic == 1)
{
YY->ice_2 = 41;
YY->ice_4 = 0;
printf("\t\t请输入群ID:");
scanf("%d",&id);
YY->q_id = id;
YY->m_id = M_ID;
send(socket_fd,YY, sizeof(XINXI), 0);
read(socket_fd,buf, sizeof(buf));
if(strcmp(buf, "ok") != 0)
{
printf("\t\t%s",buf);
return ;
}
//fflush(stdin);
Q_ID = id;
//pthread_mutex_init(&lock, NULL); //初始化锁
//pthread_cond_init(&cond, NULL); //初始化条件变量
M = 1;
if(pthread_create(&thid, NULL, thread_g, NULL) != 0)
{
//创建失败
printf("\t\t创建线程失败!\n");
return ;
}
else
{
//printf("\t\t创建成功!\n");
}
printf("\t\t\033[31mMINE :");
setbuf(stdin, NULL);
do{
//要监视的描述符集合
fd_set fds;
FD_ZERO(&fds); //清空文件描述符集合
FD_SET(0,&fds); //把标准输入设备加入到集合中
//FD_SET(socket_fd,&fds); //把网络通信文件描述符加入到集合中
ret = select(socket_fd+1,&fds,NULL,NULL,NULL);
if(ret < 0)//错误
{
perror("select fail:");
return ;
}
else if(ret > 0) //有活跃的
{
//判断是否 标准输入设备活跃 假设是则发送数据
if(FD_ISSET(0,&fds))
{
N = 1;
int i = 0;
char Buf[200] = {
0},ch;
setbuf(stdin, NULL);
fgets(Buf,200,stdin);
setbuf(stdin, NULL);
YY->ice_4 = 888;
YY->m_id = M_ID;
strncpy(YY->buf, Buf, sizeof(Buf));
//printf("buf: %s\n", YY->buf);
//printf("ice_1 = %d\n",YY->ice_1);
if(strcmp(YY->buf, "exit\n") == 0)
{
//printf("杀死线程\n");
printf("\033[0m\n");
M = 0;
pthread_join(thid, NULL); //销毁线程
return ;
}
if(strcmp(YY->buf, "\n") != 0)
ret = send(socket_fd, YY, sizeof(XINXI), 0);
//printf("buf = %sA\n", buf);
setbuf(stdin, NULL);
printf("\t\t\033[31mMINE: ");
N = 0;
}
}
//pthread_mutex_unlock(&lock);
}while(1);
}
}
void C_group(XINXI *YY, DENN *XX, int socket_fd)
{
int ret,ic,id;
char ch;
char A[100],name[20], buf[50],qu[200],an[100];
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
do
{
setbuf(stdin, NULL);
printf("\033c");
get_XX(socket_fd);
printf("\t\t*******************************************\n");
printf("\t\t***************0 退出 **********************\n");
printf("\t\t***************1 群的创建,解散***************\n");
printf("\t\t***************2 申请加群,退群***************\n");
printf("\t\t***************3 查看已加群和群成员************\n");
printf("\t\t***************4 查看聊天记录*****************\n");
printf("\t\t***************5 设置管理员*******************\n");
printf("\t\t***************6 踢人 ***********************\n");
printf("\t\t请输入你的选择:");
setbuf(stdin, NULL);
scanf("%d", &ic);
printf("\033c");
if(ic == 0) //退出循环
{
break;
}
else if(ic == 1) //群的创建,解散
{
YY->ice_2 = 31;
printf("\t\t*******************************************\n");
printf("\t\t***************R 退出 **********************\n");
printf("\t\t***************A 创建***********************\n");
printf("\t\t***************B 解散***********************\n");
printf("\t\t请输入你的选择:");
setbuf(stdin, NULL);
scanf("%s", &buf);
printf("\033c");
if(strcmp(buf, "A") == 0)
{
YY->ice_3 = 311;
YY->m_id = M_ID;
printf("\t\t群ID:");
scanf("%d", &id);
YY->q_id = id;
printf("\t\t群名:");
scanf("%s", &name);
//printf("name = %s\n", name);
strncpy(YY->name, name, sizeof(YY->name));
//printf("YY->name = %s\n",YY->name);
printf("\t\t群类型:[A] 加群需要管理员同意加群\n");
printf("\t\t \t[B] 加群需要回答问题加群\n");
printf("\t\t \t[C] 加群无需管理员同意\n");
printf("\t\t \t[R] 退出\n");
printf("\t\t群类型:");
//fflush(stdin);
scanf("%s", &buf);
if(strcmp(buf, "A") == 0)
{
YY->zt = 1;
}
else if(strcmp(buf, "B") == 0)
{
YY->zt = 2;
printf("\t\t问题:");
scanf("%s", &qu);
strncpy(YY->qu, qu, sizeof(qu));
printf("\t\t答案:");
scanf("%s",&an);
strncpy(YY->an, an, sizeof(an));
}
else if(strcmp(buf, "C") == 0)
{
YY->zt = 0;
}
else if(strcmp(buf, "R") == 0)
{
continue;
}
send(socket_fd, YY, sizeof(XINXI), 0);
read(socket_fd, buf, sizeof(buf));
printf("%s\n", buf);
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}
else if(strcmp(buf, "B") == 0)
{
YY->ice_3 = 312;
YY->m_id = M_ID;
printf("\t\t群ID:");
scanf("%d", &id);
YY->q_id = id;
send(socket_fd, YY, sizeof(XINXI),0);
//printf("AAAA\n");
read(socket_fd,buf, sizeof(buf));
printf("\t\t%s\n",buf);
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}
else if(strcmp(buf, "R") == 0)
{
continue;
}
}
else if(ic == 2) //申请加群退群
{
YY->ice_2 = 32;
YY->m_id = M_ID;
printf("\t\t[A] 加群\n");
printf("\t\t[B] 退群\n");
printf("\t\t[R] 退出\n");
printf("\t\t请输入你的选择:");
setbuf(stdin, NULL);
scanf("%s", &buf);
printf("\033c");
if(strcmp(buf, "A") == 0)
{
YY->ice_3 = 321;
printf("\t\t请输入群ID:");
scanf("%d", &id);
YY->q_id = id;
send(socket_fd, YY, sizeof(XINXI),0);
recv(socket_fd, XZ, sizeof(LIAOT), 0);
if(XZ->zt == 100) //回答问题加入
{
printf("\t\t回答正确问题才能加入!!!\n");
printf("\t\t问题:%s\n", XZ->qu);
printf("\t\t答案:");
scanf("%s", &an);
if(strcmp(an, XZ->an) == 0)
{
YY->ice_1 = 3;
YY->ice_4 = 3211;
YY->zt = 1; //1为回答正确
}
else
{
printf("\t\t答案错误!!!\n");
}
}
else
{
printf("\t\t%s\n", XZ->buf);
}
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}
else if(strcmp(buf, "B") == 0)
{
YY->ice_3 = 322;
printf("\t\t请输入群ID:");
scanf("%d", &id);
YY->q_id = id;
send(socket_fd, YY, sizeof(XINXI),0);
recv(socket_fd, buf, sizeof(buf), 0);
printf("\t\t%s\n", buf);
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}
else if(strcmp(buf, "R") == 0)
{
continue;
}
}
else if(ic == 3) //查看已加群和群成员
{
YY->ice_2 = 33;
YY->m_id = M_ID;
do
{
printf("\033c");
printf("\t\t[A] 查看已加群\n");
printf("\t\t[B] 查询群成员\n");
printf("\t\t[R] 退出\n");
printf("\t\t请输入你的选择:");
setbuf(stdin, NULL);
scanf("%s", &buf);
printf("\033c");
if(strcmp(buf, "A") == 0)
{
YY->ice_3 = 331;
send(socket_fd, YY, sizeof(XINXI),0);
recv(socket_fd, buf, sizeof(buf), 0);
if(strcmp(buf, "ok") == 0)
{
do
{
recv(socket_fd, buf, sizeof(buf), 0);
if(strcmp(buf, "over") != 0)
{
printf("\t\t%s\n", buf);
}
else
{
break;
}
}while(1);
}
}
else if(strcmp(buf, "B") == 0)
{
YY->ice_3 = 332;
printf("\t\t群ID:");
scanf("%d", &id);
YY->q_id = id;
send(socket_fd, YY, sizeof(XINXI),0);
//recv(socket_fd, buf, sizeof(buf), 0);
do
{
recv(socket_fd, buf, sizeof(buf), 0);
if(strcmp(buf, "over") != 0)
{
printf("\t\t%s\n", buf);
}
else
{
break;
}
}while(strcmp(buf, "over") != 0);
}
else if(strcmp(buf, "R") == 0)
{
break;
}
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
} while (1);
}
else if(ic == 4) //查看聊天记录
{
YY->ice_2 = 34;
YY->m_id = M_ID;
printf("\t\t群ID:");
scanf("%d",&id);
YY->q_id = id;
send(socket_fd, YY, sizeof(XINXI),0);
int DAY = 0, year, month, day;
recv(socket_fd, XZ, sizeof(LIAOT), 0);
if(strcmp(XZ->buf, "ok") == 0)
{
do
{
recv(socket_fd, XZ, sizeof(LIAOT), 0);
if(XZ->zt == 0)
{
break;
}
if(strcmp(XZ->xinxi,"over") != 0)
{
year = XZ->zt/10000;
month = (XZ->zt - year*10000)/100;
day = XZ->zt - year*10000 - month*100;
if(DAY == 0)
{
DAY = XZ->zt;
printf("\t\t\t\033[31m%d年%d月%d日\n\033[0m", year, month, day);
}
else if(DAY != XZ->zt)
{
DAY = XZ->zt;
printf("\t\t\t\033[31m%d年%d月%d日\n\033[0m", year, month, day);
}
printf("\t\t%s :%s\n\n",XZ->name,XZ->xinxi);
}
}while(strcmp(XZ->xinxi,"over") != 0);
}
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}
else if(ic == 5) //设置管理员
{
YY->ice_2 = 35;
YY->m_id = M_ID;
printf("\t\t群ID:");
scanf("%d", &id);
YY->q_id = id;
printf("\t\t群成员ID:");
scanf("%d", &id);
YY->y_id = id;
send(socket_fd, YY, sizeof(XINXI),0);
recv(socket_fd, buf, sizeof(buf), 0);
printf("\t\t%s\n", buf);
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}
else if(ic == 6) //踢人
{
YY->ice_2 = 36;
YY->m_id = M_ID;
printf("\t\t群ID:");
scanf("%d", &id);
YY->q_id = id;
printf("\t\t要删除的群成员ID:");
setbuf(stdin, NULL);
scanf("%d", &id);
YY->y_id = id;
send(socket_fd, YY, sizeof(XINXI), 0);
recv(socket_fd, buf, sizeof(buf), 0);
printf("\t\t%s\n", buf);
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}
else
{
printf("\t\t无此功能请重新输入!\n");
}
} while (1);
free(XZ);
}
int C_TongZ(XINXI *YY, DENN *XX, int socket_fd)
{
int sum, ret;
char buf[50];
read(socket_fd, &sum, sizeof(int));
do{
printf("\033c");
setbuf(stdin, NULL);
printf("\t\t你一共有%d条通知!!!\n", sum);
printf("\t\t[A] 查看所有通知 [B] 逐个处理所有通知 [R] 退出\n");
printf("\t\t请选择功能:");
setbuf(stdin, NULL);
scanf("%s", buf);
if (strcmp(buf, "A") == 0)
{
write(socket_fd, buf, sizeof(buf));
do
{
read(socket_fd, buf, sizeof(buf));
if(strcmp(buf, "over") != 0)
{
printf("\t\t%s\n", buf);
}
} while (strcmp(buf, "over") != 0);
break;
}
else if (strcmp(buf, "B") == 0)
{
write(socket_fd, buf, sizeof(buf));
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
while(1)
{
printf("\033c");
setbuf(stdin, NULL);
recv(socket_fd, XZ, sizeof(LIAOT), 0);
//printf("%s\n", XZ->buf);
if(strcmp(XZ->buf, "over") == 0)
{
break;
}
else if(XZ->ice == 100)
{
break;
}
else
{
//printf("A%sA\n", XZ->buf);
//printf("YYYYY%dYYYY\n", XZ->zt);
char ch, B[50],beizhu[20];
printf("\t\t%s\n", XZ->buf);
printf("\t\t[Y] 同意 [N] 忽略\n");
printf("\t\t请输入你的选择:");
setbuf(stdin, NULL);
scanf("%s", &B);
setbuf(stdin, NULL);
//write(socket_fd, ch, sizeof(char));
if(strcmp(B, "Y") == 0)
{
if(XZ->zt == 0)
{
printf("\n\t\t请输入备注:");
scanf("%s", beizhu);
strncpy(XZ->buf, B, sizeof(B));
strncpy(XZ->beizhu, beizhu, sizeof(beizhu));
send(socket_fd, XZ, sizeof(LIAOT), 0);
memset(XZ, 0, sizeof(LIAOT));
recv(socket_fd, XZ, sizeof(LIAOT), 0);
if(strcmp(XZ->buf, "over") == 0)
{
break;
}
printf("\t\t%s",XZ->buf);
memset(XZ, 0, sizeof(LIAOT));
}
else if(XZ->zt == 1)
{
strncpy(XZ->buf, B, sizeof(B));
send(socket_fd, XZ, sizeof(LIAOT), 0);
recv(socket_fd, XZ, sizeof(LIAOT), 0);
if(strcmp(XZ->buf, "over") == 0)
{
break;
}
printf("\t\t%s",XZ->buf);
memset(XZ, 0, sizeof(LIAOT));
}
}
else if(strcmp(B, "N") == 0)
{
strncpy(XZ->buf, B, sizeof(B));
send(socket_fd, XZ, sizeof(LIAOT), 0);
}
}
}
break;
}
else if (strcmp(buf, "R") == 0)
{
write(socket_fd, buf, sizeof(buf));
return 0;
}
else
{
printf("\t\t无此选项请重新输入!\n");
}
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdout, NULL);
return 0;
}while(1);
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdout, NULL);
return 0;
}
void C_haoy(XINXI *YY, DENN *XX, int socket_fd)
{
int id,ret;
char beizhu[20];
char buf[50];
do
{
//printf("%d\n", ret);
printf("\033c");
int ic;
setbuf(stdin, NULL);
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
memset(XZ, 0, sizeof(LIAOT));
get_XX(socket_fd);
printf("\t\t**********************************************\n");
printf("\t\t******************0 退出***********************\n");
printf("\t\t**************1 好友的添加,删除,查询************\n");
printf("\t\t******************2 查看好友列表****************\n");
printf("\t\t*****************3 查看好友状态*****************\n");
printf("\t\t******************4 查看聊天记录****************\n");
printf("\t\t****************5 屏蔽好友消息******************\n");
printf("\t\t请输入要选择的功能:");
scanf("%d", &ic);
printf("\033c");
if(ic == 1) //添加,删除,查询好友
{
YY->ice_2 = 21;
YY->m_id = M_ID;
//send(socket_fd, XZ, sizeof(LIAOT), 0);
char buf[50];
char ch;
printf("\t\tA 添加好友\n");
printf("\t\tB 删除好友\n");
printf("\t\tC 查询好友\n");
printf("\t\t请输入你的选择:");
scanf("%s", buf);
//int ret = write(socket_fd,buf, sizeof(buf));
//strncpy(XZ->buf, buf, sizeof(XZ->buf));
//printf("%d\n", ret);
printf("\033c");
if(strcmp(buf, "A") == 0)
{
YY->ice_3 = 211;
printf("\t\t请输入要添加好友的ID:");
scanf("%d",&id);
YY->y_id = id;
printf("\t\t请输入备注:");
scanf("%s",&beizhu);
strncpy(YY->beizhu, beizhu, sizeof(beizhu));
//printf("size = %d\nid = %d\n", sizeof(LIAOT), XZ->id);
ret = send(socket_fd, YY, sizeof(XINXI), 0); //将添加好友信息发送
//printf("%d\n", ret);
ret = read(socket_fd,buf, sizeof(buf));
//printf("%d\n", ret);
printf("\t\t%s",buf);
}
else if(strcmp(buf, "B") == 0)
{
YY->ice_3 = 212;
do
{
int i;
setbuf(stdin, NULL);
printf("\t\t************************************\n");
printf("\t\t**********0 退出 ********************\n");
printf("\t\t**********1 根据ID 删除 **************\n");
printf("\t\t**********2 根据备注删除 **************\n");
printf("\t\t请输入你的选择:");
scanf("%d", &i);
printf("\033c");
YY->zt = i;
if(i == 0)
{
//send(socket_fd, XZ, sizeof(LIAOT), 0);
break;
}
else if(i == 1)
{
printf("\t\t请输入你要删除的好友的ID:");
scanf("%d",&id);
YY->y_id = id;
ret = send(socket_fd, YY, sizeof(XINXI),0);
ret = read(socket_fd, buf, sizeof(buf));
printf("\t\t%s",buf);
}
else if(i == 2)
{
printf("\t\t请输入你要删除好友的备注:");
scanf("%s",beizhu);
strncpy(XZ->beizhu,beizhu,sizeof(beizhu));
ret = send(socket_fd, YY, sizeof(XINXI),0);
ret = read(socket_fd, buf, sizeof(buf));
printf("\t\t%s",buf);
}
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}while(1);
}
else if(strcmp(buf, "C") == 0)
{
YY->ice_3 = 213;
do
{
int i;
setbuf(stdin, NULL);
printf("\t\t************************************\n");
printf("\t\t**********0 退出 ********************\n");
printf("\t\t**********1 根据ID查询备注 **************\n");
printf("\t\t**********2 根据备注查询ID **************\n");
printf("\t\t请输入你的选择:");
scanf("%d", &i);
printf("\033c");
YY->zt = i;
if(i == 0)
{
//ret = send(socket_fd, XZ, sizeof(LIAOT), 0);
break;
}
else if(i == 1)
{
printf("\t\t请输入好友的ID:");
scanf("%d", &id);
YY->y_id = id;
ret = send(socket_fd, YY, sizeof(XINXI),0);
ret = read(socket_fd, buf, sizeof(buf));
printf("\t\t%s",buf);
}
else if(i == 2)
{
printf("\t\t请输入好友备注:");
scanf("%s", beizhu);
strncpy(YY->beizhu, beizhu, sizeof(beizhu));
ret = send(socket_fd, YY, sizeof(XINXI), 0);
ret = read(socket_fd, buf, sizeof(buf));
printf("\t\t%s",buf);
}
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
} while (1);
}
/*
int I;
char B[20];
printf("请输入好友ID:");
scanf("%d", &I);
printf("请输入备注:");
scanf("%s", B);
XZ->id = I; //好友ID
XZ->ice = ic;
strncpy(XZ->beizhu, B, sizeof(B)); //备注名
send(socket_fd, XZ, sizeof(LIAOT),0);
read(socket_fd, buf, sizeof(buf));
printf("%s\n",buf);
*/
}
else if (ic == 2) //查看好友列表
{
YY->ice_2 = 22;
YY->m_id = M_ID;
send(socket_fd, YY, sizeof(XINXI),0);
do
{
read(socket_fd,buf, sizeof(buf));
if(strcmp(buf,"over") != 0)
{
printf("\t\t%s\n",buf);
}
} while (strcmp(buf, "over") != 0);
}
else if(ic == 3) //查看好友状态
{
YY->ice_2 = 23;
YY->m_id = M_ID;
printf("\t\t请输入要查询好友的ID:");
scanf("%d", &id);
YY->y_id = id;
send(socket_fd, YY, sizeof(XINXI), 0);
recv(socket_fd, XZ, sizeof(LIAOT), 0);
//printf("XZ->zt = %d\n", XZ->zt);
if(XZ->zt == 1)
{
printf("\t\t该好友在线!\n");
}
else if(XZ->zt == 0)
{
printf("\t\t该好友不在线!\n");
}
else if(XZ->zt == -1)
{
printf("\t\t你无此ID的好友!\n");
}
}
else if(ic == 4) //查看聊天记录
{
YY->ice_2 = 24;
YY->m_id = M_ID;
printf("\t\t请输入你要查询好友的ID:");
scanf("%d", &id);
YY->y_id = id;
send(socket_fd, YY, sizeof(XINXI), 0);
do
{
setbuf(stdin, NULL);
recv(socket_fd,XZ, sizeof(LIAOT), 0);
if(strcmp(XZ->xinxi, "over") != 0)
{
if(XZ->id == id) //从好友处发来的信息
{
printf("\t\033[34m%s : %s\033[0m\n\n", XZ->beizhu, XZ->xinxi);
}
else
{
printf("\t\033[31mMINE :%s\033[0m\n\n", XZ->xinxi);
}
}
} while (strcmp(XZ->xinxi, "over") != 0);
//printf("end\n");
}
else if(ic == 5) //屏蔽好友消息
{
YY->ice_2 = 25;
YY->m_id = M_ID;
int i;
while(1)
{
setbuf(stdin, NULL);
printf("\033c");
printf("\t\t**************1 屏蔽好友信息***************\n");
printf("\t\t**************2 取消屏蔽 ******************\n");
printf("\t\t**************0 退出 **********************\n");
printf("\t\t请输入你的选择:");
scanf("%d", &i);
if(i == 0)
{
break;
}
else if(i == 1)
{
YY->ice_3 = 251;
printf("\t\t请输入你要屏蔽的好友ID:");
scanf("%d",&id);
YY->y_id = id;
send(socket_fd, YY, sizeof(XINXI),0);
read(socket_fd, buf, sizeof(buf));
printf("\t\t%s\n",buf);
}
else if(i == 2)
{
YY->ice_3 = 252;
printf("\t\t请输入你要取消屏蔽的好友ID:");
scanf("%d",&id);
YY->y_id = id;
send(socket_fd, YY, sizeof(XINXI),0);
read(socket_fd, buf, sizeof(buf));
printf("\t\t%s\n",buf);
}
printf("\t\t请输入任意键继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdout, NULL);
}
}
else if(ic == 0)
{
YY->ice_2 = 20;
return ; //退出循环
}
else
{
printf("\t\t无此功能请重新选择!!!!!!!!!!!!\n");
}
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
} while (1);
}
void C_denn(XINXI *YY, int socket_fd)
{
int I;
char *P,buf[50];
do
{
setbuf(stdin, NULL);
printf("\033c");
printf("\t\tID:");
scanf("%d",&I);
P = getpass("\t\t密码:");
YY->m_id = I;
YY->zt = 1;
strncpy(YY->password, P, 16);
//printf("id = %d,password = %s\n",XX->id,XX->password);
int ret = send(socket_fd, YY, sizeof(XINXI),0);
ret = read(socket_fd,buf, sizeof(buf));
if(ret)
{
printf("\t\t%s\n",buf);
}
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}while(strcmp(buf,"登陆成功") != 0);
//printf("AAAA\n");
}
void C_get(int socket_fd)
{
int n;
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
XINXI *YY = (XINXI*)malloc(sizeof(XINXI));
//pthread_mutex_lock(&lock); //加锁
YY->ice_4 = 666;
YY->ice_1 = 4;
YY->m_id = M_ID;
//printf("mid = %d\tyid = %d\n",M_ID,YY->y_id);
YY->y_id = Y_ID;
send(Socket_fd, YY, sizeof(XINXI), 0);
//printf("JJJJJJJJJ\n");
recv(Socket_fd, XZ, sizeof(LIAOT), 0);
//printf("SSSSSSSSSSSS\n");
n = XZ->zt;
//printf("XZ->zt = %d\n", XZ->zt);
if(XZ->zt > 0)
{
for(int i = 0;i<n;i++)
{
printf("\b\b\b\b\b\b\n");
char buf[1024]={
0};
//read(socket_fd,buf,sizeof(buf));
recv(Socket_fd, XZ, sizeof(LIAOT), 0);
printf("\t\t%s : %s\n", XZ->beizhu, XZ->xinxi);
//printf("MINE: ");
}
}
free(YY);
free(XZ);
//printf("HHHHHHHHHHHHHHH\n");
return ;
}
void *thread(void *arg)
{
int n;
char A[300];
LIAOT XZ;
XINXI YY;
do
{
sleep(3);
//pthread_mutex_lock(&lock); //加锁
YY.ice_4 = 666;
YY.ice_1 = 4;
YY.m_id = M_ID;
//printf("mid = %d\tyid = %d\n",M_ID,YY->y_id);
YY.y_id = Y_ID;
send(Socket_fd, &YY, sizeof(XINXI), 0);
//printf("JJJJJJJJJ\n");
recv(Socket_fd, &XZ, sizeof(LIAOT), 0);
//printf("SSSSSSSSSSSS\n");
n = XZ.zt;
//printf("XZ->zt = %d\n", XZ->zt);
if(XZ.zt > 0)
{
for(int i = 0;i<n;i++)
{
printf("\b\b\b\b\b\b \n");
char buf[1024]={
0};
//read(socket_fd,buf,sizeof(buf));
recv(Socket_fd, &XZ, sizeof(LIAOT), 0);
sprintf(A, "\t\033[34m%s : %s\033[0m", XZ.beizhu, XZ.xinxi);
printf("%s", A);
}
printf("\t\033[31mMINE :");
}
} while (M == 1); //退出线程
}
void C_haoyouliaot(XINXI *YY, DENN *XX, int socket_fd)
{
pthread_t thid;
int id,ret,ic,N = 0;
char beizhu[20],buf[50];
LIAOT *XZ = (LIAOT*)malloc(sizeof(LIAOT));
get_XX(socket_fd);
printf("\t\t**************************************\n");
printf("\t\t*************0 退出 *******************\n");
printf("\t\t*************1 与好友聊天 **************\n");
printf("\t\t请输入你的选择:");
scanf("%d", &ic);
printf("\033c");
if(ic == 0)
{
return;
}
else if(ic == 1)
{
YY->ice_2 = 31;
YY->ice_4 = 0;
YY->m_id = M_ID;
printf("\t\t请输入好友的ID:");
scanf("%d",&id);
YY->y_id = id;
send(socket_fd,YY, sizeof(XINXI), 0);
read(socket_fd,buf, sizeof(buf));
if(strcmp(buf, "OK") != 0)
{
printf("\t\t%s",buf);
}
//fflush(stdin);
Y_ID = id;
//pthread_mutex_init(&lock, NULL); //初始化锁
//pthread_cond_init(&cond, NULL); //初始化条件变量
M = 1;
if(pthread_create(&thid, NULL, thread, NULL) != 0)
{
//创建失败
printf("\t\t创建线程失败!\n");
return ;
}
else
{
//printf("\t\t创建成功!\n");
}
printf("\t\033[31mMINE :");
do{
//pthread_mutex_lock(&lock); //加锁
//pthread_cond_wait(&cond, &lock); //将锁释放(其他地方可以抢到锁),并睡眠等待唤醒
/*
printf("BBBBB\n");
i++;
printf("i = %d\n", i);
if(i == 5)
{
i = 0;
printf("YYYYYYYYYYYYYYYY\n");
C_get(socket_fd);
}
*/
//要监视的描述符集合
fd_set fds;
FD_ZERO(&fds); //清空文件描述符集合
FD_SET(0,&fds); //把标准输入设备加入到集合中
//FD_SET(socket_fd,&fds); //把网络通信文件描述符加入到集合中
ret = select(socket_fd+1,&fds,NULL,NULL,NULL);
if(ret < 0)//错误
{
perror("select fail:");
return ;
}
else if(ret > 0) //有活跃的
{
//判断是否 标准输入设备活跃 假设是则发送数据
if(FD_ISSET(0,&fds))
{
N = 1;
int i = 0;
char Buf[200] = {
0},ch;
setbuf(stdin, NULL);
fgets(Buf,200,stdin);
setbuf(stdin, NULL);
/*
do
{
scanf("%c", &ch);
if(ch != '\n')
{
Buf[i] = ch;
i++;
}
}while(ch != '\n');
*/
YY->ice_4 = 777;
YY->m_id = M_ID;
strncpy(YY->buf, Buf, sizeof(Buf));
//printf("buf: %s\n", YY->buf);
//printf("ice_1 = %d\n",YY->ice_1);
if(strcmp(YY->buf, "exit\n") == 0)
{
printf("\033[0m\n");
M = 0;
pthread_join(thid, NULL); //销毁线程
return ;
}
if(strcmp(Buf, "\n") != 0)
ret = send(socket_fd, YY, sizeof(XINXI), 0);
//printf("buf = %sA\n", buf);
printf("\t\033[31mMINE: ");
N = 0;
}
}
//pthread_mutex_unlock(&lock);
}while(1);
}
}
void C_zhuce(XINXI *YY, int socket_fd)
{
int I;
char P[16],buf[50],name[20],qu[200],an[100],hylb[20];
setbuf(stdin, NULL);
printf("\033c");
printf("\t\tID:");
scanf("%d",&I);
printf("\t\tPassword:");
scanf("%s",P);
printf("\t\tName: ");
scanf("%s",name);
printf("\t\t请设置密保问题:");
scanf("%s",qu);
printf("\t\t请输入答案:");
scanf("%s",an);
YY->m_id = I;
sprintf(hylb, "%shylb", name);
strncpy(YY->password, P, sizeof(P));
strncpy(YY->name, name, sizeof(name));
strncpy(YY->qu, qu, sizeof(qu));
strncpy(YY->an, an, sizeof(an));
strncpy(YY->hylb, hylb, sizeof(hylb)); //好友列表,以name为mysql—table名
send(socket_fd, YY, sizeof(XINXI),0);
//printf("YY->m_id = %d\n",YY->m_id);
read(socket_fd,buf, sizeof(buf));
printf("\t\t%s\n",buf);
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
}
void C_zhaohui(XINXI *YY, int socket_fd)
{
int I;
char an[100],qu[200],buf[50];
//do
//{
setbuf(stdin, NULL);
printf("\033c");
printf("\t\t请输入你的id:");
scanf("%d",&I);
YY->m_id = I;
send(socket_fd, YY, sizeof(XINXI),0); //将ID发送到服务器
read(socket_fd, qu, sizeof(qu)); //读取密保问题
printf("\t\t%s\n", qu);
printf("\t\t答案:");
setbuf(stdin, NULL);
scanf("%s",an);
setbuf(stdin, NULL); //回答问题
strncpy(YY->an, an, sizeof(an));
YY->ice_3 = 131;
send(socket_fd, YY, sizeof(XINXI),0); //将答案发送到服务端
read(socket_fd, buf, sizeof(buf));
printf("\t\t%s\n", buf);
printf("\t\t请输入enter继续!!!\n");
setbuf(stdin, NULL);
getchar();
setbuf(stdin, NULL);
//}while(strcmp(buf, "答案错误") == 0);
}
void face(XINXI *YY)
{
int n;
while(1)
{
setbuf(stdin, NULL);
printf("\033c");
printf("\t\t/*******************************/\n");
printf("\t\t/**************1 登陆**************/\n");
printf("\t\t/**************2 注册**************/\n");
printf("\t\t/************3 找回密码*************/\n");
printf("\t\t/************0 退出程序*************/\n");
printf("\t\t请选择:");
scanf("%d",&n);
if(n == 0)
{
exit(0);
}
if(n > 0 && n <= 3)
{
break;
}
else
{
continue;
}
}
YY->ice_2 = 10+n; //选择功能
}
client.c
#include "cli.h"
int main()
{
int id;
DENN *XX = (DENN*)malloc(sizeof(DENN));
XINXI YY;
char buf[50];
//face(XX);
int port = atoi("9999"); //从命令行获取端口号
if( port<1025 || port>65535 ) //0~1024一般给系统使用,一共可以分配到65535
{
printf("端口号范围应为1025~65535");
return -1;
}
//1 创建tcp通信socket
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd == -1)
{
perror("socket failed!\n");
}
//2 连接服务器
struct sockaddr_in server_addr = {
0};//服务器的地址信息
server_addr.sin_family = AF_INET;//IPv4协议
server_addr.sin_port = htons(port);//服务器端口号
server_addr.sin_addr.s_addr = inet_addr("192.168.30.238"); //设置服务器IP
int ret = connect(socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)); //连接服务器
if(ret == -1)
{
perror("connect failed!\n");
}
memset(&YY, 0, sizeof(XINXI));
Socket_fd = socket_fd;
do{
//*******************************************************************************
YY.ice_1 = 1;
do
{
memset(&YY, 0, sizeof(XINXI));
YY.ice_1 = 1;
printf("\033c");
face(&YY); //1 2 3 0
if(YY.ice_2 == 11) //登陆
{
printf("\033c");
C_denn(&YY, socket_fd);
M_ID = YY.m_id;
break;
}
else if(YY.ice_2 == 12) //注册
{
printf("\033c");
C_zhuce(&YY, socket_fd);
//face(XX);
}
else if(YY.ice_2 == 13) //找回密码
{
printf("\033c");
C_zhaohui(&YY, socket_fd);
//face(XX);
}
else if(YY.ice_2 == 0)
{
exit(0);
}
else
{
printf("无此功能,请重新选择!\n");
}
}while(1);
do{
int ice;
setbuf(stdin, NULL);
printf("\033c");
get_XX(socket_fd);
memset(&YY, 0, sizeof(XINXI));
printf("\t\t*************************************\n");
printf("\t\t***********1 好友管理******************\n");
printf("\t\t***********2 聊天群管理****************\n");
printf("\t\t***********3 好友聊天*****************\n");
printf("\t\t***********4 群聊天*******************\n");
printf("\t\t***********5 查看通知******************\n");
printf("\t\t***********6 文件传输******************\n");
printf("\t\t***********0 退出帐号 *****************\n");
printf("\t\t请输入你的选择:");
scanf("%d", &ice);
printf("\033c");
if(ice == 1) //好友管理
{
YY.ice_1 = 2;
//printf("%d\n", YY.ice);
//int ret = send(socket_fd, &YY, sizeof(YY), 0);
C_haoy(&YY, XX, socket_fd);
}
else if(ice == 2) //聊天群管理
{
YY.ice_1 = 3;
C_group(&YY, XX, socket_fd);
}
else if(ice == 3) //好友聊天
{
YY.ice_1 = 4;
//int ret = send(socket_fd, &YY, sizeof(YY),0);
C_haoyouliaot(&YY, XX, socket_fd);
}
else if(ice == 4) //群聊
{
YY.ice_1 = 5;
C_group_com(&YY, XX, socket_fd);
}
else if(ice == 5) //查看通知
{
YY.ice_1 = 6;
YY.m_id = M_ID;
//printf("%d\n", YY.ice);
int ret = send(socket_fd, &YY, sizeof(YY), 0);
C_TongZ(&YY, XX, socket_fd);
}
else if(ice == 6) //文件传输
{
YY.ice_1 = 7;
YY.m_id = M_ID;
C_document(&YY, socket_fd);
//printf("BBBBBBBB\n");
}
else if(ice == 0) //退出帐号
{
YY.ice_1 = 404;
YY.m_id = M_ID;
send(socket_fd, &YY, sizeof(XINXI), 0);
break;
}
//printf("CCCCCC\n");
}while(1);
}while(1);
//4 关闭通信socket
close(socket_fd);
do{
}while(1);
return 0;
}