Linux下 基于TCP协议下的C/S架构的网络聊天室 C程序

一、 实现目标

 

 

一个在 Linux 下可以使用的聊天软件,要求至少实现如下功能:

 

1. 采用 Client/Server 架构

 

2. Client A 登陆聊天服务器前,需要注册自己的 ID 和密码

 

3. 注册成功后,Client A 就可以通过自己的 ID 和密码登陆聊天服务器

 

4. 多个 Client X 可以同时登陆聊天服务器之后,与其他用户进行通讯聊天

 

5. Client A 成功登陆后可以查看当前聊天室内其他在线用户 Client x

 

6. Client A 可以选择发消息给某个特定的 Client X,即”悄悄话”功能

 

7. Client A 可以选择发消息全部的在线用户,即”群发消息”功能

 

8. Client A 在退出时需要保存聊天记录

 

9. Server 端维护一个所有登陆用户的聊天会的记录文件,以便备查

 

 

 

可以选择实现的附加功能:

 

1. Server 可以内建一个特殊权限的账号 admin,用于管理聊天室

 

2. Admin 可以将某个 Client X “提出聊天室”

 

3. Admin 可以将某个 Client X ”设为只能旁听,不能发言”

 

4. Client 端发言增加表情符号,可以设置某些自定义的特殊组合来表达感情.如输入:),则会自动发送”XXX 向大家做了个笑脸

 

5. Client 段增加某些常用话语,可以对其中某些部分进行”姓名替换”,例

 

如,输入/ClientA/welcome,则会自动发送”ClientA 大侠,欢迎你来到咱们的聊天室”





#ifndef TCP_NET_SOCKET_H_

#define TCP_NET_SOCKET_H_


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define SERV_PORT 9000


/*********************注册登录**********************/
#define reg    1         //注册
#define log 2  //登录
#define forget          3  //忘记密码
#define exit            4  //退出
#define existing_acc    5  //账号已存在
#define logged_acc      6         //账号已登录
#define error           7    //账号或密码错误
#define log_success     8  //登录成功
#define reg_success     9  //注册成功
#define Exit 10  //退出
/**************************************************/


/******************聊天室功能**********************/
#define private_chat    11  //私聊
#define group_chat      12         //群聊
#define group_result    13  //群聊接受
#define file_transfer   14  //文件传输
#define online_member   15  //查看在线人数
#define expression      16  //表情
#define phrases         17  //常用语
#define motto 18  //个性签名
#define motto_change    19  //更改个性签名
#define like            20        //点赞
#define Vip             21        //开会员
#define Shutup          22        //禁言
#define lifted          23  //解禁
#define kick            24        //踢人
/**************************************************/




/****************服务器返回结果*******************/
#define vip_success     25  //开会员成功
#define Shutup_success  26  //禁言成功
#define Send_success    27         //发送信息成功
#define Send_error      28         //发送信息失败
#define kick_fail       29        //踢人失败
#define kick_success    30        //踢人成功
#define like_success    31        //点赞成功
#define change_success  32  //更改个性签名成功
/************************************************/


extern int tcp_init();
extern int tcp_accept(int sfd);
extern int tcp_connet();
extern void signalhandler(void);
extern int mygetch();
extern int gettime();
extern char* nowtime();


#endif
/*********************************************************************
File Name:               tcp_net_socket.c
Author:                             date:
Description:            
Fuction List: int tcp_init() //用于初始化操作
int tcp_accept(int sfd) //用于服务器的接收
int tcp_connect(const char* ip) //用于客户端的连接
void signalhandler(void) //用于信号处理,让服务器在按下Ctrl+c或Ctrl+\时不会退出

********************************************************************/


//服务器端

#include "tcp_net_socket.h"
//用于初始化操作
int tcp_init()  
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);     //创建套接字
if(sfd == -1)
{
perror("socket");
return -1;
}

int ret;
struct sockaddr_in serveraddr;


memset(&serveraddr,0,sizeof(struct sockaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERV_PORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);

ret = bind(sfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));           //绑定
if(ret == -1)
{
perror("bind");
return -1;
}

ret = listen(sfd,10);           //监听它,并设置允许最大的连接数为10个
if(ret == -1)
{
perror("listen");
close(sfd);
return -1;
}

return sfd;
}


//用于服务器的接收
int tcp_accept(int sfd)
{
struct sockaddr_in clientaddr;
memset(&clientaddr, 0, sizeof(struct sockaddr));
int addrlen = sizeof(struct sockaddr);

//sfd接受客户端的连接,并创建新的socket为new_fd,将请求连接的客户端的ip、port保存在结构体clientaddr中
int new_fd = accept(sfd, (struct sockaddr*)&clientaddr, &addrlen);       
if(new_fd == -1)
{
perror("accept");
close(sfd);
return -1;
}
printf("%s %d success connet...\n", 
inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));

return new_fd;
}


//用于客户端的连接
int tcp_connect(const char* ip)
{
int ret;
int sfd = socket(AF_INET, SOCK_STREAM, 0);     //申请新的socket
if(sfd == -1)
{
perror("socket");
return -1;
}

struct sockaddr_in serveraddr;

memset(&serveraddr, 0,sizeof(struct sockaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERV_PORT);
serveraddr.sin_addr.s_addr = inet_addr(ip);    

ret = connect(sfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));       //将sfd连接至指定的服务器网络地址 serveraddr
if(ret == -1)
{
perror("connect");
close(sfd);
return -1;
}

return sfd;
}


//用于信号处理,让服务器在按下Ctrl+c或Ctrl+\时不会退出
void signalhandler(void)
{
sigset_t sigSet;
sigemptyset(&sigSet);
sigaddset(&sigSet,SIGINT);
sigaddset(&sigSet,SIGQUIT);
sigprocmask(SIG_BLOCK,&sigSet,NULL);
}


//用于将密码数字转换为*
int mygetch( )
{
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}


//获取当前时间 
int gettime()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "%s",asctime (timeinfo) );
}


//获取当前时间   指针 用于消息记录
char* nowtime()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );

return asctime (timeinfo);

}



struct send
{
char name[20]; //名字
char toname[20]; //接收人
char account[20]; //账号
char passward[20]; //密码
int  likes; //点赞数
int  vip; //是否是会员
char moto[300]; //个性签名
int  cmd; //提取操作符
char msg[200]; //发送、接收消息
char file_name[20]; //文件名
char file[2048]; //发送文件存的数据
char question[50]; //密保问题
char answer[50]; //密保答案
char e_s; //确认发送的表情
char p_s; //确认发送的常用语
};


struct recv
{
char from_name[20]; //发信人
char to_name[20]; //名字
int  result; //返回操作结果
int  online_num; //在线人数
char num[20][20]; //在线人名
char msg[200]; //发送、接收消息
char file_name[20]; //文件名
char file[2048]; //发送文件存的数据
char question[50]; //密保问题
char answer[50]; //密保答案
char passward[20]; //密码
char moto[300]; //个性签名
int  likes; //点赞数
int  vip; //是否是会员
char e_s; //确认发送的表情
char p_s; //确认发送的常用语
};


typedef struct node
{
int  socket;
char name[20];
struct node* next;
}UMge;
typedef struct node* PUMge;


struct send userInfo;            //客户端用户信息结构体
struct recv userBack;            //服务器用户信息结构体
sqlite3 * db = NULL; //基础信息存放数据库
int i;
int ret;
PUMge head;                   //在线用户表


//保存用户
void save_user()
{

char *errmsg = NULL;
char auff[200] = {0}; 
char cuff[200] = {0};
sprintf(auff, "insert into save_user values('%s','%s','%s','%s',%d,%d)",userInfo.account, userInfo.passward, userInfo.name, userInfo.moto, userInfo.likes, userInfo.vip);

ret = sqlite3_exec(db, auff, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
printf("insert fail:%d(%s)\n", ret, errmsg);
userBack.result = existing_acc;   //账号已存在
printf("%s is insert error...\n",userInfo.name);
return;
}
printf("sqlite save_user insert success...\n");

sprintf(cuff,"insert into question values('%s','%s','%s','%s')", userInfo.account, userInfo.passward, userInfo.question, userInfo.answer);
ret = sqlite3_exec(db, cuff, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
printf("insert fail:%d(%s)\n", ret, errmsg);
return;
}

userBack.result = reg_success;                 //注册成功
}


//登录检查表和客户端发来的数据对比
void deal_log(int cfd)
{
char **resultp = NULL;          //用二重指针存储数据
int nrow;                       //查到满足条件的总行数
int ncolumn;                     //存储列
int ret;
int i;
char *errmsg = NULL;
char cuff[200];

sprintf(cuff, "select account,passward,name,moto,likes,vip from save_user where account = '%s' and passward = '%s'", userInfo.account, userInfo.passward);

ret = sqlite3_get_table(db, cuff, &resultp, &nrow, &ncolumn, &errmsg);
if (ret != SQLITE_OK)
{
printf ("log error : %d(%s)!\n", ret, errmsg);
return;
}

printf("%s is logging...\n", resultp[8]);


if(nrow == 1)
{
PUMge temp = head->next;
while(temp != head)
{
if(strcmp(temp->name, resultp[8]) == 0)
{
userBack.result = logged_acc;
printf("%s logged error because him has logged...\n", userInfo.name);
return;
}
temp = temp->next;
}

userBack.result = log_success;    //登录成功
strcpy(userBack.from_name, resultp[8]);
strcpy(userBack.moto, resultp[9]);

userBack.likes = *(resultp[10]) - 48;
userBack.vip = *(resultp[11]) - 48;

printf("%s logged success...\n", resultp[8]);

PUMge p = (PUMge)malloc(sizeof(UMge)/sizeof(char));
if(p == NULL)
{
perror("malloc");
return;
}
p->socket = cfd;
strcpy(p->name, resultp[8]);
printf("%s 's socket is %d...\n", p->name, p->socket);
p->next = head->next;
head->next = p;
}
else
{
userBack.result = error;          //账号密码错误
printf("%s 's passward is error...\n", resultp[8]);
}

ret = write(cfd, &userBack, sizeof(userBack));
if(ret == -1)
{
perror("write");
return;
}
}


//忘记密码
void deal_forget(int cfd)
{
char auff[100];
char **resultp = NULL;
int nrow;
int ncolumn;
int ret;
char *errmsg = NULL;

printf("i am select from question...\n");

sprintf(auff, "select passward, question, answer from question where account = '%s'", userInfo.account);
ret = sqlite3_get_table(db, auff,  &resultp, &nrow, &ncolumn, &errmsg);
if(ret != SQLITE_OK)
{
printf("select error fail:%d(%s)\n", ret, errmsg);
}

strcpy(userBack.passward, resultp[3]);
strcpy(userBack.question, resultp[4]);
strcpy(userBack.answer, resultp[5]);

write(cfd, &userBack, sizeof(userBack));

}


//处理私聊请求
int deal_pchar(int cfd)
{
int flag = 0;

PUMge temp = head->next; 
while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0 && temp->socket != cfd)
{
flag = 1;
strcpy(userBack.msg, userInfo.msg);
strcpy(userBack.from_name, userInfo.name);
userBack.result = private_chat;
printf("%s(%d) send %s(%d) ...\n", userInfo.name, cfd,  userInfo.toname, temp->socket);
write(temp->socket, &userBack, sizeof(userBack));

break;
}
temp = temp->next;
}

if(flag)
{
userBack.result = Send_success;
write(cfd,&userBack, sizeof(userBack));

printf("%s send a message to %s...\n", userInfo.name, userInfo.toname);
}
else
{
userBack.result = Send_error;
write(cfd,&userBack, sizeof(userBack));

printf("%s send error...\n", userInfo.name);
}
}


//处理群聊请求
int deal_groupchat(int cfd)
{
int flag = 0;

PUMge temp = head->next;
while(temp != head)
{
if(temp->socket != cfd)
{
flag = 1;
strcpy(userBack.from_name, userInfo.name);
strcpy(userBack.msg, userInfo.msg);
userBack.result = group_chat;

write(temp->socket, &userBack, sizeof(userBack));
}
temp = temp->next;
}
printf("%s send a msg to everyone...\n",userInfo.name);

if(flag)
{
userBack.result = Send_success;
write(cfd,&userBack, sizeof(userBack));
}
else
{
userBack.result = Send_error;
write(cfd,&userBack, sizeof(userBack));

printf("%s send grep error...\n", userInfo.name);
}
}


//处理查看当前人数
int deal_member(int cfd)
{
userBack.online_num = 0;
int i = 0;
PUMge temp = head->next;
while(temp != head)
{
userBack.online_num++;
strcpy(userBack.num[i], temp->name);
i++;
temp = temp->next;
}
userBack.result = online_member;

write(cfd,&userBack,sizeof(userBack));

printf("show online_member success...\n");
}


//文件传输
int deal_file_transfer(int cfd)
{
PUMge temp = head->next;
int flag = 0;
while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0 && temp->socket != cfd)
{
flag = 1;
userBack.result = file_transfer;
strcpy(userBack.from_name, userInfo.name);
strcpy(userBack.file, userInfo.file);
strcpy(userBack.file_name, userInfo.file_name);

write(temp->socket, &userBack, sizeof(userBack));

break;
}

temp = temp->next;
}

if(flag)
{
userBack.result = Send_success;
ret = write(cfd,&userBack, sizeof(userBack));
if(ret == -1)
{
perror("write");
return -1;
}
printf("%s send a file to %s...\n", userInfo.name, userInfo.toname);
}
else
{
userBack.result = Send_error;
write(cfd,&userBack, sizeof(userBack));

printf("%s send file error...\n", userInfo.name);
}
}


//发送表情
int deal_expression(int cfd)
{
PUMge temp = head->next;
int flag = 0;
while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0 && temp->socket != cfd)
{
flag = 1;
userBack.result = expression;
strcpy(userBack.from_name, userInfo.name);
userBack.e_s = userInfo.e_s;

write(temp->socket, &userBack, sizeof(userBack));

break;
}

temp = temp->next;
}

if(flag)
{
userBack.result = Send_success;
write(cfd,&userBack, sizeof(userBack));

printf("%s send a expression to %s...\n", userInfo.name, userInfo.toname);
}
else
{
userBack.result = Send_error;
write(cfd,&userBack, sizeof(userBack));

printf("%s send expression error...\n", userInfo.name);
}
}


//发送常用语
int deal_phrases(int cfd)
{
PUMge temp = head->next;
int flag = 0;

while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0 && temp->socket != cfd)
{
flag = 1;
userBack.result = phrases;
strcpy(userBack.from_name, userInfo.name);
userBack.p_s = userInfo.p_s;

write(temp->socket, &userBack, sizeof(userBack));

break;
}

temp = temp->next;
}

if(flag)
{
userBack.result = Send_success;
write(cfd,&userBack, sizeof(userBack));

printf("%s send a phrases to %s...\n", userInfo.name, userInfo.toname);
}
else
{
userBack.result = Send_error;
write(cfd,&userBack, sizeof(userBack));

printf("%s send phrases error...\n", userInfo.name);
}
}


//处理点赞请求
int deal_like(int cfd)
{
int ret;
int i;
char *errmsg = NULL;
char **resultp = NULL;
int nrow;
int ncolumn;
char cuff[200];

sprintf(cuff, "select likes from save_user where name = '%s'", userInfo.toname);

ret = sqlite3_get_table(db, cuff, &resultp, &nrow, &ncolumn, &errmsg);
if(ret != SQLITE_OK)
{
printf("select fail:%d(%s)\n", ret, errmsg);
return -1;
}

if(nrow == 1)
{
sprintf(cuff, "update save_user set likes = %d where name = '%s'",*(resultp[1]) - 47, userInfo.toname);
ret = sqlite3_exec(db, cuff, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
printf("update fail:%d(%s)\n", ret, errmsg);
return -1;
}

PUMge temp = head->next;

while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0)
{
userBack.likes = *(resultp[1]) - 47;
userBack.result = like;
strcpy(userBack.from_name,userInfo.name);
write(temp->socket, &userBack, sizeof(userBack));

break;
}
temp = temp->next;
}
userBack.result = like_success;
write(cfd, &userBack, sizeof(userBack));

}
else
{
userBack.result = Send_error;
write(cfd, &userBack, sizeof(userBack));
}
}


//处理更改个签
int deal_motochange(int cfd)
{
char xcf[200];
char *errmsg = NULL;


sprintf(xcf, "update save_user set moto = '%s' where name = '%s'",userInfo.moto,userInfo.name);

ret = sqlite3_exec(db,xcf,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("update fail:%d(%s)\n", ret, errmsg);
return -1;
}

strcpy(userBack.moto, userInfo.moto);
userBack.result = change_success;
write(cfd, &userBack,sizeof(userBack));
printf("%s change the moto...\n", userInfo.name);

return 0;
}


//处理注册会员
int deal_vip(int cfd)
{
char guff[200];
char *errmsg = NULL;
sprintf(guff, "update save_user set vip = %d where name = '%s';", userInfo.vip, userInfo.name);

ret = sqlite3_exec(db,guff,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("update fail:%d(%s)\n", ret, errmsg);
return -1;
}
userBack.vip = 1;
userBack.result = vip_success;

write(cfd, &userBack,sizeof(userBack));

printf("%s become vip...\n", userInfo.name);

return 0;
}


//处理禁言
int deal_Shutup(int cfd)
{
PUMge temp = head->next;
int flag = 0;
while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0)
{
flag = 1;
userBack.result = Shutup;
strcpy(userBack.from_name, userInfo.name);
write(temp->socket, &userBack, sizeof(userBack));

}

temp = temp->next;
}

if(flag)
{
userBack.result = Shutup_success;
write(cfd, &userBack, sizeof(userBack));

}
else
{
userBack.result = Send_error;
write(cfd, &userBack, sizeof(userBack));

}
}


//处理踢人
int deal_kick(int cfd)
{
PUMge temp = head->next;
int flag = 0;
while(temp != head)
{
if(strcmp(temp->name, userInfo.toname) == 0)
{
flag = 1;
userBack.result = kick;
strcpy(userBack.from_name, userInfo.name);
write(temp->socket, &userBack, sizeof(userBack));

break;
}

temp = temp->next;
}

if(flag)
{
userBack.result = kick_success;
write(cfd, &userBack, sizeof(userBack));

}
else
{
userBack.result = Send_error;
write(cfd, &userBack, sizeof(userBack));

}
}


//处理退出请求
int deal_exit(int cfd)
{
PUMge temp = head;

while(temp->next != head)
{
if(strcmp(temp->next->name, userInfo.name) == 0)
{
PUMge p = temp->next;
temp->next = p->next;
free(p);
printf("%s logged out...\n", userInfo.name);
break;
}
temp = temp->next;
}

return 0;
}


void *server_requst(int const cfd)


{
printf("i am dealing...\n");
char *errmsg = NULL;

while(1)
{
ret = read(cfd, &userInfo, sizeof(userInfo));
if(ret == 0)
{
pthread_exit(NULL);
}


switch (userInfo.cmd)
{
case (reg) : //注册
{
save_user(db);
write(cfd, &userBack,sizeof(userBack));
break;
}
case (log): //登陆
{
deal_log(cfd);
break;
}
case (forget): //忘记密码
{
deal_forget(cfd);
break;
}
case (private_chat): //私聊
{
deal_pchar(cfd);
break;
}
case (group_chat): //群聊
{
deal_groupchat(cfd);
break;
}
case (file_transfer): //文件传输
{
deal_file_transfer(cfd);
break;
}
case (online_member): //查看在线人数
{
deal_member(cfd);
break;
}
case (expression): //发送表情
{
deal_expression(cfd);
break;
}
case (phrases): //发送常用语
{
deal_phrases(cfd);
break;
}
case (like): //点赞
{
deal_like(cfd);
break;
}
case (motto_change): //更改个性签名
{
deal_motochange(cfd);
break;
}
case (Vip): //注册会员
{
deal_vip(cfd);
break;
}
case (Shutup): //禁言
{
deal_Shutup(cfd);
break;
}
case (exit): //退出
{
deal_exit(cfd);
break;
}
case(kick):    //踢人
{
deal_kick(cfd);
break;
}
}
}
}


int main()
{
int sfd;

head = (PUMge) malloc (sizeof(UMge)/sizeof(char));            //创建在线用户环形链
if(head == NULL)
{
printf("malloc error\n");
}
head->next = head;

sfd = tcp_init();
// signalhandler();

char *errmsg = NULL;
ret = sqlite3_open("user.db",&db);
if(ret != SQLITE_OK)
{
perror("open user.db error");
return -1;
}
printf("open user.db success...\n");

ret = sqlite3_exec(db, "create table if not exists save_user(account TEXT, passward TEXT, name TEXT, moto TEXT, likes INTEGER, vip INTEGER, primary key(account))",NULL,NULL,&errmsg);       //primary key(成员,成员,...)  主键   确保数据的唯一性
if(ret != SQLITE_OK)
{
printf("create fail:%d(%s)\n", ret, errmsg);
return -1;
}
printf("create save_user success...\n");

ret = sqlite3_exec(db, "create table if not exists question(account TEXT, passward TEXT, question TEXT, answer TEXT, primary key(account))", NULL, NULL, &errmsg);
if(ret !=SQLITE_OK)
{
printf("creat fail:%d(%s)\n",ret,errmsg);
return -1;
}
printf("create question success...\n");

pthread_t clientid;

while(1)
{
int cfd;
cfd = tcp_accept(sfd);
if(cfd == -1)
{
perror("accept");
return -1;
}

ret = pthread_create(&clientid, NULL, (void*)server_requst, (void*)cfd);
if(ret == -1)
{
perror("pthread create");
return -1;
}

pthread_detach(clientid); // 线程分离
pthread_join(clientid, NULL);
}

sqlite3_close(db);

return 0;

}


//客户端

#include "tcp_net_socket.h"


//用于初始化操作
int tcp_init()  
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);     //创建套接字
if(sfd == -1)
{
perror("socket");
return -1;
}

int ret;
struct sockaddr_in serveraddr;


memset(&serveraddr,0,sizeof(struct sockaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERV_PORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);

ret = bind(sfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));
if(ret == -1)
{
perror("bind");
return -1;
}

ret = listen(sfd,10);           //监听它,并设置允许最大的连接数为10个
if(ret == -1)
{
perror("listen");
close(sfd);
return -1;
}

return sfd;
}


//用于服务器的接收
int tcp_accept(int sfd)
{
struct sockaddr_in clientaddr;
memset(&clientaddr, 0, sizeof(struct sockaddr));
int addrlen = sizeof(struct sockaddr);

//sfd接受客户端的连接,并创建新的socket为new_fd,将请求连接的客户端的ip、port保存在结构体clientaddr中
int new_fd = accept(sfd, (struct sockaddr*)&clientaddr, &addrlen);       
if(new_fd == -1)
{
perror("accept");
close(sfd);
return -1;
}
printf("%s %d success connet...\n", 
inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));

return new_fd;
}


//用于客户端的连接
int tcp_connect(const char* ip)
{
int ret;
int sfd = socket(AF_INET, SOCK_STREAM, 0);     //申请新的socket
if(sfd == -1)
{
perror("socket");
return -1;
}

struct sockaddr_in serveraddr;

memset(&serveraddr, 0,sizeof(struct sockaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERV_PORT);
serveraddr.sin_addr.s_addr = inet_addr(ip);    

ret = connect(sfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));       //将sfd连接至指定的服务器网络地址 serveraddr
if(ret == -1)
{
perror("connect");
close(sfd);
return -1;
}

return sfd;
}


//用于信号处理,让服务器在按下Ctrl+c或Ctrl+\时不会退出
void signalhandler(void)
{
sigset_t sigSet;
sigemptyset(&sigSet);
sigaddset(&sigSet,SIGINT);
sigaddset(&sigSet,SIGQUIT);
sigprocmask(SIG_BLOCK,&sigSet,NULL);
}


//用于将密码数字转换为*
int mygetch( )
{
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}


//获取当前时间 
int gettime()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "%s",asctime (timeinfo) );
}


//获取当前时间   指针 用于消息记录
char* nowtime()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
return asctime (timeinfo);
}


//清除scanf后的缓存  回车
void SCAN_N()
{
char ch;
while((getchar()) != '\n' && ch != EOF);
}


int Get_time(void)//六位数组
{
/* int *temp =p; */
time_t now ;
struct tm *t ;
time(&now) ;
t = localtime(&now);
/* *(temp++) = t->tm_year+1900;
*(temp++) = t->tm_mon+1;
*(temp++) = t->tm_mday;
*(temp++) = t->tm_hour;
*(temp++) = t->tm_min;
*temp = t->tm_sec; */
return (t->tm_mon+1)*1000000+t->tm_mday*10000+t->tm_hour*100+t->tm_min;
}


void Welecome(void)
{
system("clear");
int a =Get_time();
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         W                                     *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         We                                    *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Wel                                   *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welc                                  *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welco                                 *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcom                                *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome t                             *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to                            *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to o                          *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to ou                         *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our                        *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our c                      *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our ch                     *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our cha                    *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chat                   *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chatt                  *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chatti                 *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chattin                *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chatting               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(150000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chatting r             *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(150000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chatting ro            *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(150000);

system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chatting roo           *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(150000);

system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chatting room          *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(150000);

system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Welcome to our chatting room!         *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(150000);

sleep(2);
}




void Byebye(void)
{
system("clear");
int a =Get_time();
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         H                                     *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Ho                                    *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hop                                   *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope                                  *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope t                                *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to s                             *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to se                            *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see                           *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see y                         *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(100000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see yo                        *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see you                       *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see you a                     *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see you ag                    *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see you aga                   *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see you agai                  *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see you again                 *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2018-%02d-%02d-%02d-%02d                              *\n",a/1000000,a%1000000/10000,a%10000/100,a%100);
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*         Hope to see you again!                *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                               *\n");
printf("\t\t*                                  Author:ljw   *\n");
printf("\t\t*************************************************\n");
usleep(200000);


sleep(2);
}




struct send
{
char name[20]; //名字
char toname[20]; //接收人
char account[20]; //账号
char passward[20]; //密码
int  likes; //点赞数
int  vip; //是否是会员
char moto[300]; //个性签名
int  cmd; //提取操作符
char msg[200]; //发送、接收消息
char file_name[20]; //文件名
char file[2048]; //发送文件存的数据
char question[50]; //密保问题
char answer[50]; //密保答案
char e_s; //确认发送的表情
char p_s; //确认发送的常用语
};


struct recv
{
char from_name[20]; //发信人
char to_name[20]; //名字
int  result; //返回操作结果
int  online_num; //在线人数
char num[20][20]; //在线人名
char msg[200]; //发送、接收消息
char file_name[20]; //文件名
char file[2048]; //发送文件存的数据
char question[50]; //密保问题
char answer[50]; //密保答案
char passward[20]; //密码
char moto[300]; //个性签名
int  likes; //点赞数
int  vip; //是否是会员
char e_s; //确认发送的表情
char p_s; //确认发送的常用语
};




struct send userInfo;
struct recv userBack;
int ret;
char fan[20]; //输入任意键返回
char Name[20];
int READEN = 0;
int State;
int LIKES = 0;
int OUT = 0;


//主界面显示
void show()
{
system("clear");
printf("\n\n\n");
printf("\t\t\t\t  欢迎来到聊天室!祝您聊天愉快!");
printf("\n\n\n\n\n");
printf("\t\t\t\t\t1  注册\n");
printf("\t\t\t\t\t2  登录\n");
printf("\t\t\t\t\t3  忘记密码\n");
printf("\t\t\t\t\t4  退出\n");
printf("\n\n\t\t\t请输入选择:");
}


//注册界面显示及处理
int show_reg()
{
char pass1[20];
char pass2[20];
char temp;
int i = 0;
int j = 0;
system("clear");
printf("\n\n\n\n");
State = 0; //禁言标志位
userInfo.cmd = reg;
userInfo.likes = 0;
userInfo.vip = 0;
printf("\t\t\t\t请输入账号:");
scanf("%s",userInfo.account);

printf("\n\t\t\t\t请输入密码:");

while(i < 20)
{
pass1[i] = mygetch();
if(pass1[i] == 10)
{
if(j == 0) ////防止输入账号的最后一个回车影响 
{
j++;
continue;
}
break;
}
printf("*");
i++;
}
pass1[i] = '\0';

i = 0;
printf("\n\n\t\t\t\t请确认您的密码:");
while(i < 20)
{
pass2[i] = mygetch();
if(pass2[i] == 10)
{
break;
}
printf("*");
i++;
}
pass2[i] = '\0';

printf("\n\n\t\t\t\t请设计您的昵称:");
scanf("%s",userInfo.name);

strcpy(userInfo.moto, "来一场说走就走的旅行");

printf("\n\n\t\t\t\t请输入密保问题:");
scanf("%s",userInfo.question);
SCAN_N();

printf("\n\n\t\t\t\t请输入密保答案:");
scanf("%s",userInfo.answer);
SCAN_N();

system("clear");
printf("\n\n\n\n\n\n\n\t\t\t正在注册......\n");
sleep(2);

if(strcmp(pass1, pass2) != 0)
{
printf("\n\n\t\t\t\t密码输入有误!请重新注册!\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
return -9;
}
else
{
strcpy(userInfo.passward, pass1);
}
}


//登录界面显示
int show_log()
{
char pass1[20];
int i = 0;
int j = 0;
system("clear");
printf("\n\n\n\n\n\n\n");
printf("\t\t\t\t请输入账号:");
scanf("%s",userInfo.account);

printf("\n\t\t\t\t请输入密码:");

while(i < 20)
{
pass1[i] = mygetch();
if(pass1[i] == 10)
{
if(j == 0) ////防止输入账号的最后一个回车影响 
{
j++;
continue;
}
break;
}
printf("*");
i++;
}
pass1[i] = '\0';

strcpy(userInfo.passward, pass1);

system("clear");
printf("\n\n\n\n\n\n\n\t\t\t正在登陆......\n");
sleep(2);
}


//登陆之后界面显示及处理
int show_chat()
{
system("clear");
gettime();
printf("\n%s   个性签名:%s   点赞数:%d", Name, userBack.moto, LIKES);
if(userBack.vip == 1)
{
printf("\t\t尊贵的黄金会员");
}
else
{
printf("\t\t普通用户");
}
printf("\n\n\t\t\t\t\t a 私聊\n");
printf("\t\t\t\t\t b 群聊\n");
printf("\t\t\t\t\t c 文件传输\n");
printf("\t\t\t\t\t d 查看在线人数\n");
printf("\t\t\t\t\t e 发送表情\n");
printf("\t\t\t\t\t f 常用语\n");
printf("\t\t\t\t\t g 点赞\n");
printf("\t\t\t\t\t h 更改个性签名\n");
printf("\t\t\t\t\t i 注册会员\n");
printf("\t\t\t\t\t j 禁言(需开通会员)\n");
printf("\t\t\t\t\t k 解禁(需开通会员)\n");
printf("\t\t\t\t\t l 踢人(需开通会员)\n");
printf("\t\t\t\t\t m 查看聊天记录\n");
printf("\t\t\t\t\t n 退出\n");
printf("\n\t\t请输入您的选择:");
}


//文件传输发送
int deal_sendfile(int sfd)
{
char bs;
int ret;
int fd;
ssize_t num;
int i = 0;

system("clear");
printf("\n\n\n\n");

if(State == Shutup)
{
printf("\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
return Shutup;
}

printf("\n\t\t\t您想给谁发文件呢?:");
scanf("%s", userInfo.toname);

strcpy(userInfo.name, Name);

printf("\n\n\t\t\t请告诉我本文件夹下要发送的文件名`(*∩_∩*)′:");
scanf("%s", userInfo.file_name);
SCAN_N();

userInfo.cmd = file_transfer;

if(strcmp(Name, userInfo.toname) == 0)
{
sleep(1);
system("clear");
printf("\n\n\n\n\n\n\t\t不可以给自己发送文件哦`(*∩_∩*)′");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
return -1;
}

fd = open(userInfo.file_name, O_RDONLY, S_IRUSR | S_IWUSR);
if(fd < 0)
{
printf("\n\n\t\t\t\t没有此文件!\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
return -1;
}

while(1)
{
memset(&bs, 0, sizeof(bs));
ssize_t read_bytes = read(fd, &bs, sizeof(bs));
if(read_bytes == -1)
{
perror("read");
return -1;
}
if(read_bytes == 0)
{
break;
}
userInfo.file[i] = bs;
i++;
}
userInfo.file[i] = '\0';

ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}

printf("\n\n\t\t\t正在发送...\n");
sleep(2);
}


//收文件
int back_file()
{
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一份文件给您,请确认接收!\n", userBack.from_name);
sleep(2);

int fd;
fd = open(userBack.file_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if(fd < 0)
{
perror("open");
return -1;
}

ret = write(fd, userBack.file, strlen(userBack.file));
if(ret == -1)
{
perror("write");
return -1;
}

printf("\n\n\t\t恭喜您接收文件成功!`(*∩_∩*)′");
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
}


//发送表情选择
int deal_send_expr(int sfd)
{
char e[10];     //输入表情选项

system("clear");
printf("\n\n\n\n");
if(State == Shutup)
{
printf("\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
return Shutup;
}

printf("\n\t\t\t您想发表情给谁呢?:");
scanf("%s", userInfo.toname);
strcpy(userInfo.name, Name);
userInfo.cmd = expression;

printf("\n\n\t\t\t\t1 开心       :-D\n");
printf("\t\t\t\t2 生气       = =#\n");
printf("\t\t\t\t3 为你加油   *\(^_^)/*\n");
printf("\t\t\t\t4 两手一摊   ╮( ̄▽  ̄)╭\n");
printf("\t\t\t\t5 飞吻       づ ̄ 3 ̄)づ\n");
printf("\t\t\t\t6 哭泣       (T_T)\n");
printf("\t\t\t\t7 竖起大拇指 b( ̄▽ ̄)d\n");
printf("\t\t\t\t8 不满       (︶︿︶)\n");
printf("\t\t\t\t9 亲亲       >3<\n\n");
printf("\t\t请输入您想发送的表情:");
scanf("%s", e);
SCAN_N();

userInfo.e_s = e[0];

if(strcmp(Name, userInfo.toname) == 0)
{
sleep(1);
system("clear");
printf("\n\n\n\n\n\n\t\t不可以给自己发送表情哦`(*∩_∩*)′");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
return -1;
}

ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
system("clear");
printf("\n\n\n\n\n\n\t\t正在发送...\n");
sleep(2);
}


//接受表情信号分析
int back_ep()
{
char er[20];
switch (userBack.e_s)
{
case '1' :
{
strcpy(er, ":-D");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '2' :
{
strcpy(er, "= =#");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '3' :
{
strcpy(er, "*\(^_^)/*");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '4' :
{
strcpy(er, "╮( ̄▽  ̄)╭");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '5' :
{
strcpy(er, "づ ̄ 3 ̄)づ");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '6' :
{
strcpy(er, "(T_T)");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '7' :
{
strcpy(er, "b( ̄▽ ̄)d");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '8' :
{
strcpy(er, "(︶︿︶)");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '9' :
{
strcpy(er, ">3<");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\t\t%s\n",er);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
default :
{
system("clear");
printf("\n\n\n\n\t\t抱歉!输入错误!/n");
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
}
}


//发送常用语选择
int deal_phrases(int sfd)
{
char p[10];     //输入常用语选项

system("clear");
printf("\n\n\n\n");
if(State == Shutup)
{
printf("\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
return Shutup;
}

printf("\n\t\t\t您想给谁发信息呢?:");
scanf("%s", userInfo.toname);
strcpy(userInfo.name, Name);
userInfo.cmd = phrases;

printf("\n\t\t\t1 はじめまして            (  初次见面 )\n");
printf("\t\t\t2 どうぞよろしく          (  请多关照 )\n");
printf("\t\t\t3 そうですか              (  是吗?   )\n");
printf("\t\t\t4 はい                    (  是的     )\n");
printf("\t\t\t5 よくいらっしゃいました  (欢迎,欢迎 )\n");
printf("\t\t\t6 どういたしまして        (  不用客气 )\n");
printf("\t\t\t7 おはよございます        (   早上好  )\n");
printf("\t\t\t8 こんにちは              (  你好!   )\n");
printf("\t\t\t9 こんばんは              (   晚上好!)\n");
printf("\n\t\t请输入您想发送的常用语:");
scanf("%s", p);
SCAN_N();

userInfo.p_s = p[0];

if(strcmp(Name, userInfo.toname) == 0)
{
sleep(1);
system("clear");
printf("\n\n\n\n\n\n\t\t不可以给自己发送常用语哦`(*∩_∩*)′");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
return -1;
}

ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
system("clear");
printf("\n\n\n\n\n\n\t\t正在发送...\n");
sleep(2);
}


//接受常用语
int back_ph()
{
char ph[60];
switch (userBack.p_s)
{
case '1' :
{
strcpy(ph, "はじめまして  ( 初次见面 )");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '2' :
{
strcpy(ph, "どうぞよろしく  ( 请多关照 )");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '3' :
{
strcpy(ph, "そうですか    ( 是吗?)");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '4' :
{
strcpy(ph, "はい    ( 是的 )");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '5' :
{
strcpy(ph, "よくいらっしゃいました  ( 欢迎,欢迎 )");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '6' :
{
strcpy(ph, "どういたしまして ( 不用客气 )");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '7' :
{
strcpy(ph, "おはよございます   ( 早上好 )");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '8' :
{
strcpy(ph, "こんにちは ( 你好!)");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
case '9' :
{
strcpy(ph, "こんばんは ( 晚上好!)");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
default :
{
system("clear");
printf("\n\n\n\n\t\t抱歉!输入错误!/n");
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
break;
}
}
}


//读线程
void* Read_from_server(int sfd)
{
int i;
while(1)
{    
ret = read(sfd, &userBack, sizeof(userBack));
if(ret == -1)
{
perror("read");
return;
}
READEN = 1;
switch (userBack.result)
{
case (private_chat):
{
system("clear");
printf("\n\n\n\n\n\n\t\t%s 对您说了一句悄悄话:%s",userBack.from_name, userBack.msg);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
case (group_chat):
{
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一条信息:%s\n", userBack.from_name, userBack.msg);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
case (online_member):
{
system("clear");
printf("\n\n\n\n\t\t当前在线人数:%d\n", userBack.online_num);
for(i = 0; i < userBack.online_num; i++)
{
printf("\n\n\t\t%s\n",userBack.num[i]);
}
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
case (file_transfer):
{
back_file();
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
case (expression):
{
back_ep();
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
case (phrases):
{
back_ph();
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
case (like):
{
LIKES = userBack.likes;
system("clear");
printf("\n\n\n\n\n\n\t\t%s用户给你点了一个赞!`(*∩_∩*)′\n", userBack.from_name);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
case (Shutup):
{
State = Shutup;
system("clear");
printf("\n\n\n\n\n\n\t\t%s用户把您禁言了!开通会员可以解禁并反击他哦`(*∩_∩*)′\n", userBack.from_name);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
case (kick):
{
OUT = 1;
system("clear");
printf("\n\n\n\n\n\n\t\t很抱歉,您已被%s用户强制下线!开会员可以把他踢下去哦!`(*∩_∩*)′\n",userBack.from_name);
printf("\n\n\n\n\t\t输入ENTER键返回`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
break;
}
}
}
}


//处理登录后的请求
int deal_log(int sfd)
{
char ecf[12];     //输入选择
char x[2];                           //用于支付确认  
char xzf[40]; //聊天文本名字
char czf[2048]; //聊天记录

while(1)
{
if(READEN == 1)
{
READEN = 0;
switch(userBack.result)
{
case (Send_success):
{
system("clear");
printf("\n\n\n\n\n\n\t\t发送成功!\n");
memset(&userBack.result, 0, sizeof(userBack.result));
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}
case (like_success):
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t恭喜您点赞成功!`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}
case (change_success):
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t恭喜您更改个性签名成功!`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;

case (vip_success):
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t恭喜您成为尊贵的黄金会员!`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}
case (Shutup_success):
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t恭喜您对方已成功被您禁言!`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
printf("\n\n\n\t\t输入ENTER键离开:");
fgets(fan, 20, stdin);
break;
}
case (kick_success):
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t恭喜您对方已成功被您踢下线!`(*∩_∩*)′\n");
memset(&userBack.result, 0, sizeof(userBack.result));
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}
case (Send_error):
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t对不起,对方暂时没有登录!\n");
memset(&userBack.result, 0, sizeof(userBack.result));
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}
}
}

show_chat();

fgets(ecf, 12, stdin);

if(OUT == 1)
{
OUT--;
return Exit; 
}

switch(ecf[0])
{
case 'a': //私聊
{
int fd1;
system("clear");
printf("\n\n\n\n");
if(State == Shutup)
{
printf("\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}

memset(czf,0,strlen(czf));
memset(xzf,0,strlen(xzf));

printf("\t\t\t您想和谁聊天呢?请输入对方名字:");
scanf("%s", userInfo.toname);
SCAN_N();


printf("\n\n\t\t\t请输入您想要说的话:");
scanf("%[^\n]", userInfo.msg);
SCAN_N();

userInfo.cmd = private_chat;

strcpy(userInfo.name,Name);

if(strcmp(Name, userInfo.toname) == 0)
{
sleep(1);
system("clear");
printf("\n\n\n\n\n\n\t\t不可以给自己发信息哦`(*∩_∩*)′");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}

ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}

sprintf(xzf, "%s与%s的聊天记录.txt", Name, userInfo.toname);
fd1 = open(xzf, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR);
if(fd1 == -1)
{
perror("open");
return -1;
}

sprintf(czf, "%s 对 %s 发送了一句悄悄话:%s\t%s", Name, userInfo.toname, userInfo.msg,nowtime());
ret = write(fd1, czf, strlen(czf));
if(ret == -1)
{
perror("write");
return -1;
}
printf("\n\n\t\t\t正在发送...\n");
sleep(2);

break;
}
case 'b': //群聊
{
system("clear");
printf("\n\n\n\n");
int i = 0;

if(State == Shutup)
{
printf("\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}

printf("\n\n\n\n\n\t\t\t请输入您想要说的话:");
scanf("%[^\n]", userInfo.msg);
SCAN_N();

userInfo.cmd = group_chat;

strcpy(userInfo.name,Name);

ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
system("clear");
printf("\n\n\n\n\n\t\t\t正在发送...\n");
sleep(2);

break;
}
case 'c': //文件传输
{
deal_sendfile(sfd);
break;
}
case 'd': //查看在线人数
{
userInfo.cmd = online_member;

ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}

system("clear");
printf("\n\n\n\n\n\n\t\t...\n");
break;
}
case 'e': //发送表情
{
deal_send_expr(sfd);
break;
}
case 'f': //发送常用语
{
deal_phrases(sfd);
break;
}
case 'g': //点赞
{
userInfo.cmd = like;
system("clear");
printf("\n\n\n\n\n\n\t\t您比较欣赏谁呢?`(*∩_∩*)′请告诉我:");
scanf("%s", userInfo.toname);
SCAN_N();
strcpy(userInfo.name, Name);

if(strcmp(Name, userInfo.toname) == 0)
{
sleep(1);
system("clear");
printf("\n\n\n\n\n\n\t\t不可以给自己点赞哦`(*∩_∩*)′");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}

ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
printf("\n\n\t\t请稍等...\n");
sleep(2);
break;
}
case 'h': //更改个性签名
{
userInfo.cmd = motto_change;
system("clear");
printf("\n\n\n\n\n\n\t\t您想要一个什么样的个性签名呢?\n\n\n\t\t\t\t");
scanf("%s", userInfo.moto);
SCAN_N();

strcpy(userInfo.name, Name);

ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
printf("\n\n\t\t请稍等...\n");
sleep(2);
break;
}
case 'i': //注册会员
{
if(userBack.vip == 1)
{
system("clear");
printf("\n\n\n\n\n\n\t\t\t您已经是尊贵的黄金会员了!`(*∩_∩*)′\n");
printf("\n\n\n\t\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
}
else if(userBack.vip == 0)
{
system("clear");
printf("\n\n\n\t您好,请先支付200元,然后就能永久享受vip权限了!`(*∩_∩*)′\n");
printf("\n\n\n\n\n\t确认支付吗?(y/n)");
scanf("%s",x);
SCAN_N();

if(x[0] == 'y')
{
strcpy(userInfo.name,Name);
userInfo.vip = 1;
userInfo.cmd = Vip;

ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
printf("\n\n\t\t请稍等...\n");
sleep(2);
}
}
break;
}
case 'j': //禁言
{
if(userBack.vip == 0)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您还没有开通会员,请先开通会员!`(*∩_∩*)′\n");
printf("\n\n\n\t\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
}
else if(userBack.vip == 1)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您想让谁闭嘴呢?`(*∩_∩*)′请告诉我:");
scanf("%s", userInfo.toname);
SCAN_N();
userInfo.cmd = Shutup;
strcpy(userInfo.name,Name);

if(strcmp(Name, userInfo.toname) == 0)
{
sleep(1);
system("clear");
printf("\n\n\n\n\n\n\t\t不可以给自己禁言哦`(*∩_∩*)′");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}

ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return;
}
printf("\n\n\t\t请稍等...\n");
sleep(2);
}
break;
}
case 'k': //解禁
{
if(State == 0)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您没有被禁言!尽情聊天吧`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
}
else if(State == Shutup)
{
if(userBack.vip == 0)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您还没有开通会员,请先开通会员!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
}
else if(userBack.vip == 1)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您想现在解禁吗!(y/n):");
scanf("%s",x);
SCAN_N();

if(x[0] == 'y')
{
State = 0;
system("clear");
printf("\n\n\n\n\n\n\t\t恭喜您成功解禁!尽情去嗨皮啊!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
}
}
}
break;
}
case 'l': //踢人
{
if(userBack.vip == 0)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您还没有开通会员,请先开通会员!`(*∩_∩*)′\n");
printf("\n\n\n\t\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
}
else
{
system("clear");
printf("\n\n\n\n\n\n\t\t您看谁不顺眼呢?`(*∩_∩*)′请告诉我:");
scanf("%s", userInfo.toname);
SCAN_N();

strcpy(userInfo.name, Name);
userInfo.cmd = kick;                      //vip踢人信号

if(strcmp(Name, userInfo.toname) == 0)
{
sleep(1);
system("clear");
printf("\n\n\n\n\n\n\t\t不可以踢自己哦`(*∩_∩*)′");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}

ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return;
}
printf("\n\n\t\t请稍等...\n");
sleep(2);
}
break;
}
case 'm': //查看聊天记录
{
system("clear");
char n[20];
char mcf[60];
char ncf[2048];
char kl;
int fd2;
int i = 0;
printf("\n\n\n\n\n\n\t\t您想看和谁的聊天记录呢?");
scanf("%s",n);
SCAN_N();
sprintf(mcf, "%s与%s的聊天记录.txt", Name, n);
fd2 = open(mcf,O_RDONLY,S_IRUSR | S_IWUSR);
if(fd2 == -1)
{
system("clear");
printf("\n\n\n\n\n\n\n\t\t\t您与%s还没有聊天记录!快去聊天吧`(*∩_∩*)′\n",n);
printf("\n\n\n\t\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}

while(1)
{
memset(&kl, 0, sizeof(kl));
ssize_t read_bytes = read(fd2, &kl, sizeof(kl));
if(read_bytes == -1)
{
perror("read");
return -1;
}
if(read_bytes == 0)
{
break;
}
ncf[i] = kl; 
i++;
}
ncf[i] = '\0';

printf("\n%s\n", ncf);

printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}
case 'n': //退出
{
userInfo.cmd = exit;
strcpy(userInfo.name, Name);

ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
return Exit;
}
case 10: //ENTER键返回刷新界面
{
break;
}
default:
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t请输入a--n之间的选择!\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}
}

}

}


int main(int argc, char** argv)
{
if(argc != 2)
{
printf("Usage: ./cilent IP\n");
return -1;
}

int sfd;
char bcf[2];
pthread_t ReadId;
sfd = tcp_connect(argv[1]);

Welecome();

while(1)
{
show();
scanf("%s", bcf);

switch (bcf[0])
{
case '1': //注册
{
if(show_reg() == -9)
{
break;
}
else
{
write(sfd, &userInfo, sizeof(userInfo));
memset(&userInfo,0,sizeof(userInfo));

memset(&userBack,0,sizeof(userBack));
read(sfd, &userBack, sizeof(userBack));

if(userBack.result == reg_success)
{
printf("\n\n\t\t\t\t恭喜您注册成功!\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
}
else if(userBack.result == existing_acc)
{
printf("\n\n\t\t\t\t该账户已被注册!请重新注册!\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
}
break;
}
}
case '2': //登录
{
userInfo.cmd = log;
show_log(&userInfo);
write(sfd, &userInfo, sizeof(struct send));

read(sfd, &userBack,sizeof(userBack));

if(userBack.result == log_success)
{
strcpy(Name, userBack.from_name);
LIKES = userBack.likes;
printf("\n\n\t\t\t\t恭喜您登陆成功!\n");
sleep(2);

pthread_create(&ReadId, NULL, (void*)Read_from_server, (void*)sfd);
pthread_detach(ReadId);

ret = deal_log(sfd);
if(ret == Exit)
{
break;
}
}
else if(userBack.result == error)
{
printf("\n\n\t\t\t\t账号密码错误!请重新登陆\n");
sleep(2);
}
else if(userBack.result == logged_acc)
{
printf("\n\n\t\t\t\t此账号已被登录!\n");
sleep(2);
}

break;
}
case '3': //忘记密码
{
char ans[100];
userInfo.cmd = forget;
system("clear");
printf("\n\n\n\n\n\n\t\t请输入您的账号:");
scanf("%s", userInfo.account);

ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}

ret = read(sfd, &userBack, sizeof(userBack));
if(ret == -1)
{
perror("read");
return -1;
}

printf("\n\t\t   问题:%s",userBack.question);
printf("\n\n\t\t请输入答案:");
scanf("%s",ans);
SCAN_N();

if(strcmp(ans, userBack.answer) == 0)
{
printf("\n\n\t\t您的密码是:%s 请妥善保管您的密码`(*∩_∩*)′\n",userBack.passward);

printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}
else
{
system("clear");
printf("\n\n\n\n\n\n\t\t抱歉,您的答案不对!\n");
printf("\n\n\n\t\t输入ENTER键返回:");
fgets(fan, 20, stdin);
break;
}

}
case '4': //退出
{
Byebye();
system("clear");
return 0;
}
default :
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t请输入1--3之间的选择!\n");
sleep(2);
break;
}
}
}

return 0;
}

你可能感兴趣的:(Linux下 基于TCP协议下的C/S架构的网络聊天室 C程序)