在linux系统下实现了一个多人聊天工具的服务器端和客户端。该工具支持显示在线用户
源代码在线查看: command.c
#include "global.h"
command cmd_list[MAX_CMD]={
{"/quit",5,1},
{"/log",4,2}
};
int check_cmd()
{
int i;
for(i = 0; i < MAX_CMD; i++) {
if(!strncmp(cmd_list[i].cmd,key_buffer,cmd_list[i].length))
return cmd_list[i].num;
}
return 0;
}
void do_cmd_log()
{
int i = 4;
int k = 0;
while(key_buffer[i]==' '||key_buffer[i]
=='\t')
i++;
if(key_buffer[i] == '\0'||key_buffer[i] == '\n')
strcpy(filename,"scc.log");
else {
while(key_buffer[i]!=' '&&key_buffer[i]
!='\t'&& key_buffer[i]!='\n' &&
key_buffer[i]!='\0'&&k filename[k] = key_buffer[i];
i++;
k++;
}
filename[k] = '\0';
}
enable_log = 1;
}
void do_cmd(int c)
{
switch(c) {
case 2:
do_cmd_log();
break;
default:
break;
}
}
int handle_cmd()
{
int c;
/*if the string is not a command or can't
* be handle by client*/
if(!(c=check_cmd()))
return -1;
/*if the command is "quit"*/
else if(c == 1)
return 0;
do_cmd(c);
return 1;
}
void do_log(char *mes)
{
FILE * fp;
if((fp = fopen(filename,"a")) == NULL) {
fprintf(stderr,"log file open fail!\n");
return;
}
fprintf(fp,"%s\n",mes);
fclose(fp);
}