这篇博客是上一篇的另外一半,客户端的实现,还是有些bug没有调试好,这两天在重新写链表管理的服务器,我打算重新考虑一下客户端的界面显示,收发消息实现的机制。
/*********************************************************************
File Name: tcp_net_socket.h
Author: date:
Description:
Fuction List:
********************************************************************/
#ifndef TCP_NET_SOCKET_H_
#define TCP_NET_SOCKET_H_
#include <stdio.h>
#include <sqlite3.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include <termios.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SERV_PORT 9000
#define reg 1 //注册
#define log 2 //登录
#define group_chat 5 //群聊
#define group_result -6 //群聊接受
#define private_chat 10 //私聊
#define file_transfer 15 //文件传输对象
#define file_confire 80 //文件询问确认接受
#define file_yes 81 //接受
#define file_no 82 //不接受
#define file_select 18 //文件选择
#define file_recv 83 //文件接收
#define online_member 20 //查看在线人数
#define online_result 21 //在线人数结果
#define expression 25 //表情
#define e_select 26 //发送表情选择
#define phrases 29 //常用语
#define p_select 31 //发送常用语选择
#define motto 35 //个性签名
#define like 40 //点赞
#define Vip 38 //开会员
#define Shutup 45 //禁言
#define Shutuped 46 //被禁言
#define lifted 50 //解禁
#define kick 55 //踢人
#define kicked 56 //被踢下线
#define exit 100 //退出
#define existing_acc -1 //账号已存在
#define logged_acc -2 //账号已登录
#define error -3 //账号或密码错误
#define error1 -4 //重复登录
#define log_success 3 //登录成功
#define reg_success 4 //注册成功
#define Send_success 6 //发送信息成功
#define Send_error 7 //发送信息失败
#define send_msg 8 //发送信息
#define recv_msg 9 //接受信息
#define send_result 11 //发送结果
#define Exit 12 //退出
#define kick_fail 16 //踢人失败
#define kick_success 17 //踢人成功
#define like_success 13 //点赞成功
#define file_success 14 //文件传输成功
#define motto_change 19 //更改个性签名
#define forget 60 //忘记密码
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);
}
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* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(100000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(200000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(150000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(150000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(150000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(150000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(150000);
system("clear");
printf("\n\n\n\n\t\t*************************************************\n");
printf("\t\t* 2017-%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:hjf *\n");
printf("\t\t*************************************************\n");
usleep(150000);
sleep(2);
}
#include "tcp_net_socket.h"
struct send
{
int socket;
char account[20]; //账号
char passward[20]; //密码
char name[20]; //名字
char moto[256]; //个性签名
int likes; //点赞数
char question[100]; //密保问题
char answer[100]; //密保答案
char toname[20]; //接收人
int cmd; //提取操作符
char buff[2048]; //发送、接收文件 消息
int state; //是否处于禁言状态
int vip; //是否是会员
char e_s; //确认发送的表情
char p_s; //确认发送的常用语
char file[2048]; //发送文件存的数据
char file_name[30]; //文件名
};
struct recv
{
char from_name[20]; //发信人
char name[20]; //名字
char moto[256]; //个性签名
int result; //返回操作结果
int online_num; //在线人数
char num[20][20]; //在线人名
char cuff[2048]; //发送、接收文件 消息
int cmd_back; //提取操作符
int likes; //点赞数
int state; //是否处于禁言状态
int vip; //是否是会员
int msg_back; //确认是接收方还是发送方
char e_s; //确认发送的表情
char p_s; //确认发送的常用语
char file[2048]; //发送文件存的数据
char file_name[30]; //文件名
char question[100]; //密保问题
char answer[100]; //密保答案
char passward[20]; //密码
};
struct send userInfo;
struct recv userBack;
char Name[20];
pthread_mutex_t mutex;
char fan[20]; //输入任意键返回
char ecf[12]; //输入选择
FILE *stream;
//主界面显示
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");
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);
userInfo.state = 0;
strcpy(userInfo.moto, "我想回家(T_T)");
printf("\n\n\t\t\t\t请输入密保问题:");
scanf("%s",userInfo.question);
printf("\n\n\t\t\t\t请输入密保答案:");
scanf("%s",userInfo.answer);
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");
sleep(2);
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, userBack.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_send_expr(int sfd)
{
char e[10]; //输入表情选项
int ret;
system("clear");
printf("\n\n\n\n");
if(userBack.state == Shutup)
{
printf("\n\n\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
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);
userInfo.e_s = e[0];
ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
}
//发送常用语的选择
int deal_phrases(int sfd)
{
char p[10]; //输入常用语选项
int ret;
system("clear");
printf("\n\n\n\n");
if(userBack.state == Shutup)
{
printf("\n\n\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
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);
userInfo.p_s = p[0];
ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
}
//发送文件的选择
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(userBack.state == Shutup)
{
printf("\n\n\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
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);
userInfo.cmd = file_transfer;
fd = open(userInfo.file_name, O_RDONLY, S_IRUSR | S_IWUSR);
if(fd < 0)
{
printf("\n\n\t\t\t\t没有此文件!\n");
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;
}
system("clear");
printf("\n\n\n\n");
printf("\n\n\n\t\t恭喜您发送文件成功!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
}
//对聊天等需求的处理
int deal_log(int sfd)
{
int ret;
char x[2]; //用于支付确认
char xzf[40];
char czf[2048];
while(1)
{
show_chat();
scanf("%s",ecf);
switch (ecf[0])
{
case 'a': //私聊
{
system("clear");
printf("\n\n\n\n");
int i = 0;
int fd1;
char a;
if(userBack.state == Shutup)
{
printf("\n\n\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
break;
}
memset(czf,0,strlen(czf));
memset(xzf,0,strlen(xzf));
printf("\t\t\t您想和谁聊天呢?请输入对方名字:");
scanf("%s", userInfo.toname);
a = getchar();
printf("\n\n\t\t\t请输入您想要说的话:");
scanf("%[^\n]", userInfo.buff);
userInfo.cmd = private_chat;
strcpy(userInfo.name,Name);
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.buff,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;
char a;
if(userBack.state == Shutup)
{
printf("\n\n\n\n\n\n\t\t您处于被禁言状态!无法发言!开通会员可以解禁`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
break;
}
a = getchar();
printf("\n\n\n\n\n\t\t\t请输入您想要说的话:");
scanf("%[^\n]", userInfo.buff);
userInfo.cmd = group_chat;
strcpy(userInfo.name,Name);
ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
printf("\n\n\t\t\t......\n");
sleep(2);
break;
}
case 'c': //文件传输
{
deal_sendfile(sfd);
break;
}
case 'd': //查看在线人数
{
system("clear");
printf("\n\n\n\n\n\n\t\t...\n");
sleep(2);
userInfo.cmd = online_member;
ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
break;
}
case 'e': //发送表情
{
deal_send_expr(sfd);
system("clear");
printf("\n\n\n\n");
printf("\n\n\n\t\t恭喜您发送表情成功!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
break;
}
case 'f': //常用语
{
deal_phrases(sfd);
system("clear");
printf("\n\n\n\n");
sleep(2);
printf("\n\n\n\t\t恭喜您发送常用语成功!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
break;
}
case 'g': //点赞
{
userInfo.cmd = like;
system("clear");
printf("\n\n\n\n\n\n\t\t您比较欣赏谁呢?`(*∩_∩*)′请告诉我:");
scanf("%s", userInfo.toname);
strcpy(userInfo.name, Name);
ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
printf("\n\n\n\t\t恭喜您点赞成功!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
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);
strcpy(userInfo.name, Name);
ret = write(sfd, &userInfo,sizeof(userInfo));
if(ret == -1)
{
perror("write");
return -1;
}
printf("\n\n\n\t\t恭喜您更改个性签名成功!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
break;
}
case 'i': //注册会员
{
if(userBack.vip == 1)
{
system("clear");
printf("\n\n\n\n\n\n\t\t\t您已经是尊贵的黄金会员了!`(*∩_∩*)′\n");
sleep(2);
}
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);
sleep(2);
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;
}
system("clear");
printf("\n\n\n\n\n\n\t\t恭喜您成为尊贵的黄金会员!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
}
}
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输入任意键返回:");
scanf("%s",fan);
}
else if(userBack.vip == 1)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您想让谁闭嘴呢?`(*∩_∩*)′请告诉我:");
scanf("%s", userInfo.toname);
userInfo.cmd = Shutup;
strcpy(userInfo.name,Name);
ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return;
}
printf("\n\n\n\t\t恭喜您禁言成功!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
}
break;
}
case 'k': //解禁
{
if(userBack.state == 0)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您没有被禁言!尽情聊天吧`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
}
else if(userBack.state == Shutup)
{
if(userBack.vip == 0)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您还没有开通会员,请先开通会员!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
}
else if(userBack.vip == 1)
{
system("clear");
printf("\n\n\n\n\n\n\t\t您想现在解禁吗!(y/n):");
scanf("%s",x);
if(x[0] == 'y')
{
userBack.state = 0;
system("clear");
printf("\n\n\n\n\n\n\t\t恭喜您成功解禁!尽情去嗨皮啊!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
}
}
}
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输入任意键返回:");
scanf("%s",fan);
}
else
{
system("clear");
printf("\n\n\n\n\n\n\t\t您看谁不顺眼呢?`(*∩_∩*)′请告诉我:");
scanf("%s", userInfo.toname);
strcpy(userInfo.name, Name);
userInfo.cmd = kick; //vip踢人信号
ret = write(sfd, &userInfo, sizeof(userInfo));
if(ret == -1)
{
perror("write");
return;
}
system("clear");
printf("\n\n\n\n\n\n\t\t恭喜您!对方已成功下线!`(*∩_∩*)′\n");
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
}
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);
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\n\t\t\t您与%s还没有聊天记录!快去聊天吧`(*∩_∩*)′\n",n);
sleep(2);
system("clear");
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输入任意键返回:");
scanf("%s",fan);
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;
}
default :
{
system("clear");
printf("\n\n\n\n");
printf("\n\n\t\t请输入a--n之间的选择!\n");
sleep(2);
break;
}
}
}
}
//接受表情信号分析
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\n\t\t%s\n",er);
sleep(2);
break;
}
case '2' :
{
strcpy(er, "= =#");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",er);
sleep(2);
break;
}
case '3' :
{
strcpy(er, "*\(^_^)/*");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",er);
sleep(2);
break;
}
case '4' :
{
strcpy(er, "╮( ̄▽  ̄)╭");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",er);
sleep(2);
break;
}
case '5' :
{
strcpy(er, "づ ̄ 3 ̄)づ");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",er);
sleep(2);
break;
}
case '6' :
{
strcpy(er, "(T_T)");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",er);
sleep(2);
break;
}
case '7' :
{
strcpy(er, "b( ̄▽ ̄)d");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",er);
sleep(2);
break;
}
case '8' :
{
strcpy(er, "(︶︿︶)");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",er);
sleep(2);
break;
}
case '9' :
{
strcpy(er, ">3<");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一个表情给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",er);
sleep(2);
break;
}
default :
{
system("clear");
printf("\n\n\n\n\t\t抱歉!输入错误!`(*∩_∩*)′/n");
sleep(2);
break;
}
}
}
//接受常用语信号分析
int back_ph()
{
char ph[2000];
switch (userBack.p_s)
{
case '1' :
{
strcpy(ph, "はじめまして ( 初次见面 )");
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一句话给您!", userBack.from_name);
printf("\n\n\n\t\t%s\n",ph);
sleep(2);
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);
sleep(2);
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);
sleep(2);
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);
sleep(2);
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);
sleep(2);
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);
sleep(2);
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);
sleep(2);
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);
sleep(2);
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);
sleep(2);
break;
}
default :
{
system("clear");
printf("\n\n\n\n\t\t抱歉!输入错误!`(*∩_∩*)′/n");
sleep(2);
break;
}
}
}
//接受文件
int back_file()
{
int ret;
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恭喜您接收文件成功!`(*∩_∩*)′");
sleep(2);
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
}
//线程 用于接受信息
void *Read(int sfd)
{
int i;
int ret;
while(1)
{
ret = read(sfd, &userBack,sizeof(userBack));
if(ret == -1)
{
perror("read");
return;
}
switch (userBack.msg_back)
{
case recv_msg :
{
system("clear");
printf("\n\n\n\n\n\n\t\t%s 对您说了一句悄悄话:%s\n",userBack.from_name, userBack.cuff);
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
case send_result:
{
if(userBack.result == Send_error)
{
system("clear");
printf("\n\n\n\n\n\n\t\t对不起,对方暂时没有登录!\n");
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
else
{
system("clear");
printf("\n\n\n\n\n\n\t\t发送成功!\n");
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
}
case kick:
{
system("clear");
printf("\n\n\n\n\n\n\t\t很抱歉,您已被%s用户强制下线!开会员可以把他踢下去哦!`(*∩_∩*)′\n",userBack.from_name);
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
pthread_exit(NULL);
// break;
}
case kick_fail:
{
system("clear");
printf("\n\n\n\n\n\n\t\t抱歉对方没有上线!`(*∩_∩*)′\n");
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
case kick_success:
{
system("clear");
printf("\n\n\n\n\n\n\t\t恭喜您,对方已成功被您踢下线!`(*∩_∩*)′\n");
sleep(5);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
case like_success:
{
system("clear");
printf("\n\n\n\n\n\n\t\t%s用户给你点了一个赞!`(*∩_∩*)′\n", userBack.from_name);
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
case Shutup:
{
system("clear");
printf("\n\n\n\n\n\n\t\t%s用户把您禁言了!开通会员可以解禁并反击他哦`(*∩_∩*)′\n", userBack.from_name);
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
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]);
}
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
case group_chat:
{
system("clear");
printf("\n\n\n\n\t\t%s用户发送了一条信息:%s\n", userBack.from_name, userBack.cuff);
sleep(2);
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
case e_select:
{
back_ep();
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
case p_select:
{
back_ph();
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
case file_transfer:
{
back_file();
memset(&userBack.msg_back, 0, sizeof(userBack.msg_back));
break;
}
}
}
}
int main(int argc, char** argv)
{
if(argc != 2)
{
printf("Usage:./client ip\n");
return -1;
}
int sfd;
int ret;
char bcf[2];
pthread_t ReadId;
sfd = tcp_connect(argv[1]);
while(1)
{
Welecome();
show();
scanf("%s",bcf);
switch(bcf[0])
{
case '1' :
{
userInfo.cmd = reg;
userInfo.likes = 0;
userInfo.vip = 0;
if(show_reg() == -9) //两次密码输入不一样
{
break;
}
else
{
ret = write(sfd, &userInfo, sizeof(struct send));
if(ret == -1)
{
perror("write");
return -1;
}
memset(&userInfo,0,sizeof(userInfo));
memset(&userBack,0,sizeof(userBack));
ret = read(sfd, &userBack, sizeof(struct recv));
if(ret == -1)
{
perror("read");
return -1;
}
if(userBack.result == reg_success)
{
printf("\n\n\t\t\t\t恭喜您注册成功!\n");
sleep(2);
}
else if(userBack.result == existing_acc)
{
printf("\n\n\t\t\t\t该账户已被注册!请重新注册!\n");
sleep(2);
continue;
}
break;
}
}
case '2':
{
userInfo.cmd = log;
show_log(&userInfo);
ret = write(sfd, &userInfo, sizeof(struct send));
if(ret == -1)
{
perror("write");
return -1;
}
ret = read(sfd, &userBack,sizeof(userBack));
if(ret == -1)
{
perror("read");
return -1;
}
if(userBack.result == log_success)
{
strcpy(Name, userBack.name);
printf("\n\n\t\t\t\t恭喜您登陆成功!\n");
sleep(2);
ret = pthread_create(&ReadId, NULL, (void*)Read, (void*)sfd);
if(ret == -1)
{
perror("pthread_create");
return -1;
}
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 == error1)
{
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);
if(strcmp(ans, userBack.answer) == 0)
{
printf("\n\n\t\t您的密码是:%s 请妥善保管您的密码`(*∩_∩*)′\n",userBack.passward);
printf("\n\n\n\t\t输入任意键返回:");
scanf("%s",fan);
break;
}
else
{
system("clear");
printf("\n\n\n\n\n\n\t\t抱歉,您的答案不对!\n");
sleep(3);
break;
}
}
case '4':
{
system("clear");
printf("\n\n\n\n\n\n\n\t\t\t希望您下次再来聊天!\n");
sleep(2);
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;
}