#include<stdio.h>
#include"pthread.h"
#include<stdlib.h>
#include<conio.h>
#include<winsock.h>
#include<windows.h>
#include<string.h>
#include<time.h>
#include<math.h>
#define WSAVERS MAKEWORD(1,1)
#define CLS system("cls")
#define PAUSE system("pause>nul")
#define USERNAMELEN 30
#define ONLINEUSER_MAX 500
#define TIMEOUT 1000000 //1000000微秒 = 1秒
#define STATUS int //返回值类型
#define PAGERSIZE 20 //定义每页显示的个数
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define MESLOG "mesrecord.txt"
#define SYSLOG "systemlog.log"
#define USERINF "userinfo.inf"
#define SYSINI "server.ini"
#define USERTEMPINF "userinfotemp.inf"
#define ENTER 1300
#define SELECTED printf(" ◆→") //菜单被选中
#define MainMenuNum 10
#define ShowBox(title,text) MessageBox(NULL,text,title,MB_OK|MB_ICONERROR)
#define YELLOWWHITE system("color 6f")//背景黄字体白
#define YELLOWPURPLE system("color 65")//背景黄字体紫
#define DOUBLEYELLOW system("color 6e")//背景黄字体淡黄
#define BLUEWHITE system("color 17")//背景蓝字体白
typedef struct {
int year;
int month;
int day;
int hour;
int minute;
int second;
char week[10];
}longtime;//定义一个长日期格式的时间结构体别名
struct syslogbuffer{
char syslog[10240];
int logsize;
}syslogbuffer;
typedef char maxmes[1280]; //最多只能发送640个中文字符
u_short portbase = 0;
struct timeval timeout={0,TIMEOUT};
typedef struct{
char despcript[4];
char Susername[USERNAMELEN];//发送源
char Ausername[USERNAMELEN];//接受端
maxmes mes;
}MES;//定义一个把消息拆分成指定信息的结构体;
typedef struct{
char onlinestate;//在线状态 H隐身 O在线 Q请勿打扰
char username[USERNAMELEN];//用户名
SOCKET sock;//这个用户的套接字
}CURUSER;
typedef struct{
char username[USERNAMELEN];
char userpwd[USERNAMELEN];
char usersfzh[USERNAMELEN];
char userlastlogin[USERNAMELEN];
}USERINFO;//定义一个存储用户信息的结构体;
typedef struct{
char username[USERNAMELEN];
char userpwd[USERNAMELEN];
char usersfzh[USERNAMELEN];
longtime time;
}USERTIME;//定义一个存储用户信息的结构体;
struct ini{
char serverflag[100];//服务器配置文件的标识信息
char serverip[20];//服务器的IP
char serverport[6];//服务器监听的端口
char netprotocol[5];//本程序采用的网络协议
};
CURUSER OnlineUserList[ONLINEUSER_MAX];
int CurOnlUserCount = 0;
pthread_mutex_t t1;
int islock = 0;
struct ini ReadConfig()
{
struct ini serverinfo={"","","",""};
FILE *p;
p = fopen(SYSINI,"r");
if(p == NULL)
{
CLS;
printf("\t%s 内没有找到用户信息配置文件!请检查系统日志文件的路径,请确定该用户信息配置文件",SYSINI);
printf("和该程序在同一文件夹内!\n五秒钟后程序将自动关闭...\n");
Sleep(5000);
exit(ERROR);
}
while(!feof(p))
{
fscanf(p,"%s\n",serverinfo.serverflag);
if(strcmp(serverinfo.serverflag,"[连接服务器的配置信息]"))
{
printf("%s文件不是有效的配置文件!请仔细检查配置文件...\n",SYSINI);
Sleep(3000);
exit(ERROR);
}
fscanf(p,"服务器IP地址=%s\n",serverinfo.serverip);
fscanf(p,"服务器端口=%s\n",serverinfo.serverport);
fscanf(p,"网络协议(tcp/udp)=%s",serverinfo.netprotocol);
while(!feof(p))
{
fgetchar();
}
}
return serverinfo;
}
longtime getnowtime()
{
struct tm *datetime;
char timechar[10] = "";
char temp[30] = "";
longtime nowtime={0,0,0,0,0,0,"\0"};
time_t rawtime;
time(&rawtime);
datetime = localtime(&rawtime);
nowtime.year = datetime->tm_year+1900;
nowtime.month = datetime->tm_mon+1;
nowtime.day = datetime->tm_mday;
nowtime.hour = datetime->tm_hour;
nowtime.minute = datetime->tm_min;
nowtime.second = datetime->tm_sec+1;
return nowtime;
}
void addtimetomes(maxmes &buffer)
{
struct tm *datetime;
char timechar[10] = "";
maxmes temp = "";
longtime nowtime={0,0,0,0,0,0,"\0"};
char * week[7] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
time_t rawtime;
time(&rawtime);
datetime = localtime(&rawtime);
nowtime.year = datetime->tm_year+1900;
nowtime.month = datetime->tm_mon+1;
nowtime.day = datetime->tm_mday;
strcpy(nowtime.week,week[datetime->tm_wday]);
nowtime.hour = datetime->tm_hour;
nowtime.minute = datetime->tm_min;
nowtime.second = datetime->tm_sec+1;
itoa(nowtime.year,timechar,10);//年
strcat(temp,timechar);
strcat(temp,&"年");
itoa(nowtime.month,timechar,10);//月
strcat(temp,timechar);
strcat(temp,&"月");
itoa(nowtime.day,timechar,10);//日
strcat(temp,timechar);
strcat(temp,&"日 ");
strcat(temp,nowtime.week);//星期几
strcat(temp,&" ");
itoa(nowtime.hour,timechar,10);//时
strcat(temp,timechar);
strcat(temp,&":");
itoa(nowtime.minute,timechar,10);//分
strcat(temp,timechar);
strcat(temp,&":");
itoa(nowtime.second,timechar,10);//秒
strcat(temp,timechar);
strcat(temp,&"┃\n>>");
strcat(temp,buffer);//复制原来的消息
strcpy(buffer,temp);
}
void SaveSysLog(char * log)
{
FILE *p;
longtime temp = getnowtime();
p = fopen(SYSLOG,"a");
if(p == NULL)
{
printf("\n打开文件错误!\n秒钟后程序将会返回.");
for(int i= 3;i > 0; i--)
{
putchar('.');
Sleep(3000);
}
putchar('\n');
fclose(p);
exit(0);
}
fprintf(p,"%d-%d-%d-%d:%d:%d>>",temp.year,temp.month,temp.day,temp.hour,temp.minute,temp.second);
fprintf(p,"%s\n",log);
fclose(p);
}
void * attimesavelog(void * arg)
{
while(TRUE)
{
Sleep(10000);
if(strlen(syslogbuffer.syslog) > 1)
{
SaveSysLog((char *)arg);
memset(&syslogbuffer.syslog,'\0',sizeof(syslogbuffer));
}
}
}
STATUS RecordMessage(int type,MES * mes)//type等于0表示群消息
{
char path[50]="";
strcpy(path,mes->Ausername);
if(islock)
{
strcat(path,"1.txt");
}
else
{
pthread_mutex_lock(&t1);
strcat(path,".txt");
islock = 1;
}
FILE *p;
p = fopen(path,"a");
if(p == NULL)
{
printf("\n打开文件错误!\n秒钟后程序将会返回.");
for(int i= 3;i > 0; i--)
{
putchar('.');
Sleep(1000);
}
putchar('\n');
exit(0);
}
fprintf(p,"来自【%s】%s┃%s\n",mes->Susername,type==0?"发送的群消息":"的消息",mes->mes);
fclose(p);
strcpy(path,mes->Susername);
if(islock)
{
strcat(path,"1.txt");
}
else
{
pthread_mutex_lock(&t1);
strcat(path,".txt");
islock = 1;
}
p = fopen(path,"a");
if(p == NULL)
{
printf("\n打开文件错误!\n秒钟后程序将会返回.");
for(int i= 3;i > 0; i--)
{
putchar('.');
Sleep(1000);
}
putchar('\n');
exit(0);
}
fprintf(p,"我%s┃%s\n",type==0?"发送的群消息":"说",mes->mes);
fclose(p);
pthread_mutex_unlock(&t1);
return OK;
}
STATUS removeonlineuser(SOCKET s,char * flag,int delnum)
{
int i = 0,rememberi = 0;
if(!strcmp(flag,"NULL"))
{
while(i < CurOnlUserCount)
{
if(OnlineUserList[i].sock == s)
{
rememberi = i;
break;
}
else
{
i++;
}
}
while(i < (CurOnlUserCount - 1))
{
OnlineUserList[i].onlinestate = OnlineUserList[i+1].onlinestate;
OnlineUserList[i].sock = OnlineUserList[i+1].sock;
strcpy(OnlineUserList[i].username,OnlineUserList[i+1].username);
i++;
}
}
else
{
while(delnum < (CurOnlUserCount - 1))
{
OnlineUserList[delnum].onlinestate = OnlineUserList[delnum+1].onlinestate;
OnlineUserList[delnum].sock = OnlineUserList[delnum+1].sock;
strcpy(OnlineUserList[delnum].username,OnlineUserList[delnum+1].username);
delnum++;
}
}
CurOnlUserCount--;
return OK;
}
STATUS isonline(char * name)
{
int i = 0;
while(i < CurOnlUserCount)
{
if(!strcmp(OnlineUserList[i].username,name))
{
return TRUE;
}
}
return FALSE;
}
/*--------------------------------------------------------------
* errexit - 显示出现的错误信息并在3秒钟后退出程序
*--------------------------------------------------------------
*/
void errexit(const char *format, ...)
{
CLS;
va_list args;
int i = 3;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(ar
- 1
- 2
前往页