头文件:
#ifndef _CHATROOM1_H_
#define _CHATROOM1_H_
//************库函数
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <sqlite3.h>
#include <time.h>
#include <strings.h>
#include <semaphore.h>
#include <termios.h>
#include <signal.h>
//************宏定义
#define PORT 8888
#define SUCCESS 10000000
#define FAILURE 10000001
#define RESIGER 1
#define LOGIN 2
#define OK 3 //发送成功
#define ERROR 4
#define FIND 5
#define SAME 6
#define SEND 7 //发送消息
#define UN 8 //不在线
#define MSG 9 //收到悄悄话
#define NUM 10 //查看在线人数
#define EXIT 11
#define GROUP 12
#define ERR 13 //群聊没人在线
#define ES 14 //表情
#define PS 15 //常用语
#define VIP 16
#define TALK 17 //解禁
#define SHUTUP 18 //禁言
#define OKK 19
#define OUTLINE 20
#define OKKK 21
#define SENDFILE 22
#define UNRE 23
#define REOK 24
//**************线程
pthread_t tid = 0;
//*************结构体
struct online // 在线链表
{
int sockfd;
char name[20];
struct online* next;
};
typedef struct online* LinkList;
struct senduser // 用户信息表
{
char id[20];
char name[20];
char toname[20];
char password[20];
char code[20]; //密保
char msg[200];
char onlinenum[30][30]; //在线的人
char filename[20];
char filemsg[200];
int e_s;
int p_s;
int vip; //1是vip 0则不是
int state; //1是被禁言 0则不是
int number; //人数
int cmd;
};
#endif
客户端:
#include "chatroom1.h"
struct senduser User;
int OUT = 0;
int gettime()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "%s\n",asctime (timeinfo) );
}
void SCAN_N()
{
char ch;
while((getchar())!='\n'&&ch!=EOF);
}
void show()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* 欢迎来到聊天室! *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t******************************\n");
sleep(1);
}
void mainshow()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n\n");
printf("\t\t\t\t 1.登录\n");
printf("\t\t\t\t 2.注册\n");
printf("\t\t\t\t 3.忘记密码\n");
printf("\t\t\t\t 4.退出\n\n\n");
printf("\t\t\t\t******************************\n");
printf("\t\t\t\t*********输入你的选择*********\n");
}
void logging()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n\n");
printf("\t\t\t\t 正在登录中.... \n\n\n");
printf("\t\t\t\t******************************\n");
}
void sending()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n\n");
printf("\t\t\t\t 正在发送中.... \n\n\n");
printf("\t\t\t\t******************************\n");
}
//*******************注册***********************
int enroll(int sockfd)
{
int ret;
struct senduser User1;
char password1[20];
char password2[20];
User1.vip = 0;
User1.state = 0;
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n");
printf("\t\t\t\t请输入账号:\n");
printf("\t\t\t\t");
scanf("%s", User1.id);
SCAN_N();
while(1)
{
printf("\t\t\t\t请输入密码:\n");
printf("\t\t\t\t");
scanf("%s", password1);
SCAN_N();
printf("\t\t\t\t再次输入密码:\n");
printf("\t\t\t\t");
scanf("%s", password2);
SCAN_N();
if(strcmp(password1, password2) == 0)
{
printf("\t\t\t\t输入密码成功!\n");
strcpy(User1.password, password1);
break;
}
else
{
printf("\t\t\t\t两次输入不相同,请重新输入!\n");
}
}
printf("\t\t\t