一个基于linux的聊天软件(并发服务器,sqlite3数据库,有注释)
SC.h:#include#include#include#include#include#include#include#include#include#include#include#include#define ADMIN_SHOW 1#define ADMIN_BAN 2#define ADMIN_LIFT 3#define AD
·
SC.h:
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <sqlite3.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define ADMIN_SHOW 1
#define ADMIN_BAN 2
#define ADMIN_LIFT 3
#define ADMIN_KICK 4
#define ADMIN_EXIT 5
typedef struct myinfo
{
int flt;
char name[20];
char sec[20];
}MYINFO;
typedef struct message
{
int flt;
char myname[20];
char name[20];
char message[200];
}MES;
typedef struct rev
{
char myname[20];
char message[200];
int fd;
}REV;
typedef struct online
{
int num;
char name[8][20];
}ONLINE;
extern void s_chat(int,sqlite3 *,MES *,REV *);
extern void s_view(int,sqlite3 *,MES *);
extern void s_file(int,sqlite3 *,MES *,REV *);
extern void s_getfile(int);
packet.c:
#include "../../include/SC.h"
int Socket(int family,int type,int protocol)
{
int sockfd;
if((sockfd = socket(family,type,protocol)) < 0)
{
perror("socket error!\n");
exit(-1);
}
return sockfd;
}
int Bind(int sockfd,const struct sockaddr * myaddr,socklen_t addrlen)
{
int ret;
if((ret = bind(sockfd,myaddr,addrlen)) < 0)
{
perror("bind error!\n");
exit(-1);
}
return ret;
}
int Accept(int sockfd,struct sockaddr *cliaddr,socklen_t *addrlen)
{
int ret;
return (ret = accept(sockfd,cliaddr,addrlen));
}
int Connect(int sockfd,const struct sockaddr *servaddr,socklen_t addrlen)
{
int ret;
if((ret = connect(sockfd,servaddr,addrlen)) == -1)
{
perror("connect error!\n");
exit(-1);
}
return ret;
}
pid_t Fork(void)
{
pid_t pid;
if((pid = fork()) < 0)
{
perror("fork error!\n");
exit(-1);
}
return pid;
}
int Shmget(key_t key,int size,int shmflg)
{
int ret;
if((ret = shmget(key,size,shmflg)) < 0)
{
perror("shmget error!\n");
exit(-1);
}
return ret;
}
void *Shmat(int shmid,const void *shmaddr,int shmflg)
{
void *shmptr = NULL;
if((shmptr = shmat(shmid,shmaddr,shmflg)) == (void *)(-1))
{
perror("shmat error!\n");
exit(-1);
}
return (void *)shmptr;
}
int Open(const char *pathname,int flags,mode_t mode)
{
int fd;
if((fd = open(pathname,flags,mode)) == -1)
{
perror("open error!\n");
}
return fd;
}
int Close(int fildes)
{
int ret;
if((ret = close(fildes)) == -1)
{
perror("close error!\n");
exit(-1);
}
return ret;
}
ssize_t Write(int fd,const void *buf,size_t count)
{
ssize_t ret;
if((ret = write(fd,buf,count)) == -1)
{
perror("write error!\n");
exit(-1);
}
return ret;
}
ssize_t Read(int fd,void *buf,size_t count)
{
ssize_t ret;
if((ret = read(fd,buf,count)) == -1)
{
perror("read error!\n");
exit(-1);
}
return ret;
}
void *Malloc(size_t size)
{
void *ret;
if((ret = malloc(size)) == NULL)
{
perror("malloc error!\n");
exit(-1);
}
return ret;
}
void Free(void *memblock)
{
free(memblock);
}
server.c:
/**********************************************************
ZYP Instant Messager
Filename:server.c
Author:zyp
Description:server main
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:int main()
Return:int
Argument:null
Description:main
***********************************************************/
int main()
{
int sockfd,bindret,i,result,nRow,nCoulum;
sqlite3 *db;
char *errmsg = NULL;
char **dbresult;
char **dbresults;
char **dbresultr;
REV *shmptr = NULL; //shmaddr
int shmid;
char buf[64]; //for transfer file
struct sockaddr_in my_addr;
int flt = 0;
memset((&my_addr),0,sizeof(my_addr)); //set zero
memset(buf,0,64);
result = sqlite3_open("../myserever.db",&db);
if(result != SQLITE_OK)
{
printf("open data base failure!\n");
exit(-1);
}
sqlite3_exec(db,"drop table online",NULL,NULL,&errmsg);
sqlite3_exec(db,"create table online(name text primary key unique,fd integer,flt integer)",NULL,NULL,&errmsg); //create table for online
sockfd = Socket(AF_INET,SOCK_STREAM,0);
fcntl(sockfd,F_SETFL,O_NONBLOCK); //set socket NONBLOCK
if(sockfd < 0)
{
perror("sockfd error!\n");
exit(-1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(6666);
my_addr.sin_addr.s_addr = INADDR_ANY; //auto bind
Bind(sockfd,(struct sockaddr *)(&my_addr),sizeof(my_addr));
listen(sockfd,8);
shmid = Shmget(IPC_PRIVATE,sizeof(REV),0666);
shmptr = (REV *)Shmat(shmid,0,0);
system("clear");
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t Welcomed the use of ZYP Instant Messenger\n");
printf("\n\n\n\n\n\n\n\n\n\n\n"); // welcome screen
sleep(2);
system("clear");
printf("Connecting to server now......\n");
sleep(1);
system("clear");
printf("connect to server now!\n");
while(1)
{
int listenfd = -1;
while(listenfd < 0) //wait accept and wait send message
{
listenfd = Accept(sockfd,NULL,NULL);
if(shmptr->fd != 0)
{
Write(shmptr->fd,shmptr,200);
shmptr->fd = 0;
}
}
if(listenfd < 0)
{
perror("accept error!\n");
exit(-1);
}
else
{
pid_t pid = fork();
if(pid < 0)
{
perror("fork error!\n");
exit(-1);
}
if(pid > 0) //father process
{
;
}
if(pid == 0) //sub process
{
s_login(listenfd,db,shmptr);//login
MES *come = (MES *)malloc(sizeof(MES));
while(1)
{
sqlite3_exec(db,"create table chat(time text,sendname text,message text,toname text)",NULL,NULL,&errmsg); //create table for chat
if(Read(listenfd,come,sizeof(MES)) == 0)
{
char *str = sqlite3_mprintf("select name from online where fd='%d'",listenfd);
sqlite3_get_table(db,str,&dbresult,&nRow,&nCoulum,&errmsg); //search name
printf("%s Mandatory referrals\n",dbresult[1]);
str = sqlite3_mprintf("delete from online where fd='%d'",listenfd);
sqlite3_exec(db,str,0,0,&errmsg);
sleep(1);
break;
}
else if(strncmp(come->message,"file",4) == 0) //transfer file
{
s_file(listenfd,db,come,shmptr);
}
else if(strncmp(come->message,"getfile",7) == 0) //download file
{
s_getfile(listenfd);
}
else if(strncmp(come->message,"view",4) == 0) //view user online
{
s_view(listenfd,db,come);
}
else // chat
{
s_chat(listenfd,db,come,shmptr);
}
}
Close(listenfd);
}
}
}
sqlite3_close(db);
return 0;
}
s_chat.c:
/**********************************************************
ZYP Instant Messager
Filename:s_chat.c
Author:zyp
Description:for chat
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:void s_chat(int ,sqlite3 *,MES *,REV *)
Return:null
Agrument:int listenfd,sqlite3 *,MES *,REV *
Description:for chat
***********************************************************/
void s_chat(int listenfd,sqlite3 *db,MES *come,REV *shmptr)
{
int i,result,nRow,nRows,nCoulum,nCoulums;
char *errmsg = NULL;
char **dbresult;
char **dbresults;
char **dbresultr;
if(come->flt == 1) //offline
{
char *sql = sqlite3_mprintf("select name from online where fd='%d'",listenfd);
sqlite3_get_table(db,sql,&dbresults,&nRows,&nCoulums,&errmsg); //search fd
printf("%s is offline\n",dbresults[1]);
sql = sqlite3_mprintf("delete from online where fd='%d'",listenfd);
sqlite3_exec(db,sql,0,0,&errmsg);
sleep(1);
exit(-1);
}
else //chat
{
if(strcmp(come->name,"all") == 0) //send to all user online
{
char *str = sqlite3_mprintf("select flt from online where name='%s'",come->myname);
sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg); //is banned?
int flt = atoi(dbresultr[1]);
if(flt == 1) //not ban
{
char *str = sqlite3_mprintf("select fd from online");
sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);
int allfd;
for(i = 1;i <= nRow;i++)
{
allfd = atoi(dbresultr[i]);
if(allfd != listenfd)
{
REV *shmsend = (REV *)malloc(sizeof(REV));
shmsend->fd = allfd;
strcpy(shmsend->myname,come->myname);
strcpy(shmsend->message,come->message);
memcpy(shmptr,shmsend,sizeof(REV));
int delay = 10000000;
while(delay--);
Free(shmsend);
shmsend = NULL;
}
}
time_t timer = time(NULL);
char *tme = (char *)ctime(&timer);
str = sqlite3_mprintf("insert into chat values('%s','%s','%s','%s')",tme,come->myname,come->message,come->name);
sqlite3_exec(db,str,0,0,&errmsg);//save chat
sqlite3_free_table(dbresultr);
dbresultr = NULL;
}
else //banned
{
REV *jin = (REV *)malloc(sizeof(REV));
memset(jin,0,sizeof(REV));
strcpy(jin->myname,"Administrator");
strcpy(jin->message,"Youbanned");
Write(listenfd,jin,sizeof(REV));
Free(jin);
jin = NULL;
}
}
else //to sb online
{
char *str = sqlite3_mprintf("select flt from online where name='%s'",come->myname);
sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);
int flt = atoi(dbresultr[1]);
if(flt == 1)
{
char *str = sqlite3_mprintf("select fd from online where name='%s'",come->name);
sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);
int sendfd = atoi(dbresultr[1]);
REV *shmsend = (REV *)malloc(sizeof(REV));
shmsend->fd = sendfd;
strcpy(shmsend->myname,come->myname);
strcpy(shmsend->message,come->message);
memcpy(shmptr,shmsend,sizeof(REV));
time_t timer = time(NULL);
char *tme = (char *)ctime(&timer);
str = sqlite3_mprintf("insert into chat values('%s','%s','%s','%s')",tme,come->myname,come->message,come->name);
sqlite3_exec(db,str,0,0,&errmsg); //save chat record
Free(shmsend);
shmsend = NULL;
}
else //banned
{
REV *jin = (REV *)malloc(sizeof(REV));
memset(jin,0,sizeof(REV));
strcpy(jin->myname,"Administrator");
strcpy(jin->message,"Youbanned");
Write(listenfd,jin,sizeof(REV));
Free(jin);
jin = NULL;
}
}
}
}
s_login.c:
/**********************************************************
ZYP Instant Messager
Filename:s_login.c
Author:zyp
Description:for login
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:void s_login(int ,sqlite3 * ,REV *)
Return:null
Argument:int listenfd,sqlite3 *db,REV *shmptr
***********************************************************/
void s_login(int listenfd,sqlite3 *db,REV *shmptr)
{
int i,result,nRow,nRows,nCoulum,nCoulums;
int flt = 0;
char *errmsg = NULL;
char **dbresult;
char **dbresults;
char **dbresultr;
char *sql = NULL;
MYINFO *myinfo = (MYINFO *)malloc(sizeof(MYINFO));
memset(myinfo,0,sizeof(MYINFO));
while(read(listenfd,myinfo,sizeof(MYINFO)) == 0);
if(0) //login failure and login again
{
loop4:
while(Read(listenfd,myinfo,sizeof(MYINFO)) == 0);
goto loop5;
}
loop5:
if(myinfo->flt == 1) //registratoin
{
sql = sqlite3_mprintf("select name from peopleinfo where name='%s'",myinfo->name);
sqlite3_get_table(db,sql,&dbresult,&nRow,&nCoulum,&errmsg);
if(nRow == 0) //ok
{
char *str1 = sqlite3_mprintf("insert into peopleinfo values('%s','%s')",myinfo->name,myinfo->sec);
char *str2 = sqlite3_mprintf("insert into online values('%s','%d','%d')",myinfo->name,listenfd,1);
sqlite3_exec(db,str1,0,0,&errmsg);
sqlite3_exec(db,str2,0,0,&errmsg);
char *back = "yes";
flt = 1;
Write(listenfd,back,sizeof(back));
}
else //repeat
{
char *back = "haved";
Write(listenfd,back,sizeof(back));
}
}
else //login
{
sql = sqlite3_mprintf("select * from peopleinfo where name='%s'",myinfo->name);
sqlite3_get_table(db,sql,&dbresult,&nRow,&nCoulum,&errmsg);
if(nRow != 0) //exist
{
sql = sqlite3_mprintf("select * from online where name='%s'",myinfo->name);
sqlite3_get_table(db,sql,&dbresults,&nRows,&nCoulums,&errmsg);
if(nRows != 0)//online,can not repeat login
{
char *back = "login";
Write(listenfd,back,sizeof(back));
}
else // offline
{
if(strcmp(myinfo->sec,dbresult[3]) == 0) //password right
{
sql = sqlite3_mprintf("insert into online values('%s','%d','%d')",myinfo->name,listenfd,1);
sqlite3_exec(db,sql,0,0,&errmsg);
if(strcmp(myinfo->name,"admin") == 0) //administrator
{
char *back = "ad";
Write(listenfd,back,sizeof(back));
goto loop7; //goto adminstrator scree
}
char *back = "yes";
flt = 1;
Write(listenfd,back,sizeof(back));
}
else // password error
{
char *back = "secerror";
Write(listenfd,back,sizeof(back));
}
}
}
else //not exist
{
char *back = "nothave";
Write(listenfd,back,sizeof(back));
}
}
if(flt != 1) //login failure
{
goto loop4;
}
printf("%s on the line\n",myinfo->name);
if(0) //administrator
{
loop7:
printf("Administrator on the line\n");
char *back = (char *)malloc(10);
memset(back,0,10);
while(Read(listenfd,back,sizeof(back)) == 0);
if(strncmp(back,"1",1) == 0)
{
goto loop12; //login as an ordinary user
}
else
{
while(1)
{
while(Read(listenfd,back,sizeof(back)) == 0);
if(strncmp(back,"a",1) == 0) //view
{
sql = sqlite3_mprintf("select name from online");
sqlite3_get_table(db,sql,&dbresults,&nRows,&nCoulums,&errmsg);
int *backnum = ((int*)nRow);
ONLINE allname;
allname.num = (int)nRows;
for(i = 1;i <= nRows;i++)
{
strcpy(allname.name[i-1],dbresults[i]);
}
Write(listenfd,&allname,sizeof(allname));
}
else if(strncmp(back,"b",1) == 0)//ban
{
memset(back,0,10);
read(listenfd,back,10);
sql = sqlite3_mprintf("update online set flt=0 where name='%s'",back);
sqlite3_exec(db,sql,NULL,NULL,&errmsg);
}
else if(strncmp(back,"c",1) == 0)//lift ban
{
memset(back,0,10);
read(listenfd,back,10);
sql = sqlite3_mprintf("update online set flt=1 where name='%s'",back);
sqlite3_exec(db,sql,NULL,NULL,&errmsg);
}
else if(strncmp(back,"d",1) == 0)//kick out
{
memset(back,0,10);
Read(listenfd,back,10);
sql = sqlite3_mprintf("select fd from online where name='%s'",back);
sqlite3_get_table(db,sql,&dbresult,&nRow,&nCoulum,&errmsg);
REV *ti = (REV *)malloc(sizeof(REV));
strcpy(ti->myname,"Administrator");
strcpy(ti->message,"exit");
ti->fd = atoi(dbresult[1]);
memcpy(shmptr,ti,sizeof(REV));
}
else //exit
{
printf("Administrator is offline\n");
close(listenfd);
exit(0);
}
}
}
}
loop12:
printf("");
}
/**********************************************************
ZYP Instant Messager
Filename:s_file.c
Author:zyp
Description:for transfer file
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:void s_file(int ,sqlite3 *,MES * ,REV *)
Return:null
Argument:int listenfd,sqlite3 *db,MES *come,REV *shmptr
Description:for transfer file
***********************************************************/
void s_file(int listenfd,sqlite3 *db,MES *come,REV *shmptr)
{
int ret;
int result,nRows,nCoulums;
char ** dbresults;
char *errmsg = NULL;
REV *file = (REV *)malloc(sizeof(REV));
char *filename = (char *)malloc(60);
char buffer[128];
memset(filename,0,60);
file->fd = listenfd;
strcpy(file->myname,come->myname);
strcpy(file->message,"file");
Write(listenfd,file,sizeof(REV)); //send message to client
while((Read(listenfd,filename,60) == 0) || (strcmp(filename,"") == 0));
if(strncmp(filename,"failure",7) == 0)
{
printf("transfer failure");
;
}
else
{
int fp = Open("./scopy.c",O_RDWR | O_CREAT | O_APPEND,0777);
if(fp == -1)
{
printf("open file failure!\n");
}
else
{
do //transfer file
{
memset(buffer,0,128);
ret = Read(listenfd,buffer,128);
Write(fp,buffer,ret);
}while(ret == 128);
}
Close(fp);
char *str = sqlite3_mprintf("select fd from online where name='%s'",come->name);
sqlite3_get_table(db,str,&dbresults,&nRows,&nCoulums,&errmsg);
int tofd = ((dbresults[1][0]) - '0');
file->fd = tofd; //send message to target client to download
strcpy(file->myname,"getfile");
strcpy(file->message,filename);
memcpy(shmptr,file,sizeof(REV));
}
}
s_getfile.c:
/**********************************************************
ZYP Instant Messager
Filename:s_getfile.c
Author:zyp
Description:for target client to download file
***********************************************************/
#include "../../include/SC.h"
/*********************************************************
Function:s_getfile(int)
Return:null
Argument:int listenfd
Description:for target client to download file
**********************************************************/
void s_getfile(int listenfd)
{
int ret;
int fp = Open("scopy.c",O_RDONLY,0777);
if(fp == -1)
{
printf("open file failure!\n");
}
else
{
char buffer[128]; //for transfer file
do //transfer file
{
memset(buffer,0,128);
ret = Read(fp,buffer,128);
Write(listenfd,buffer,ret);
}while(ret == 128);
}
system("rm -rf scopy.c"); //remove file
}
client.c:
/**********************************************************
ZYP INstant Messager
Filename:client.c
Author:zyp
Description:socket connect
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:main()
Description:main function
***********************************************************/
int main(int argc,char *argv[])
{
if(argc != 2)
{
printf("format: ./client serverIP\n");
exit(-1);
}
int sockfd;
char *name = (char *)Malloc(20);
struct sockaddr_in my_addr;
memset((&my_addr),0,sizeof(my_addr)); //set zero
sockfd = Socket(AF_INET,SOCK_STREAM,0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(6666); //Port
my_addr.sin_addr.s_addr = inet_addr(argv[1]); //server IP
Connect(sockfd,(struct sockaddr *)(&my_addr),sizeof(my_addr));
name = (char *)c_login(sockfd); //save local user name
pid_t pid = Fork();
if(pid == 0) //Sub process receives the message
{
c_revmesg(sockfd);
}
if(pid > 0) //send process send the message
{
sendmesg(sockfd,name,pid);
}
Close(sockfd);
Free(name);
name = NULL;
return 0;
}
c_login.c:
/**********************************************************
ZYP Instant Messager
Filename:c_login.c
Author:zyp
Description:For the registration and landing
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:char *c_login(int)
Return:local user name
Argument:int sockfd
Description:For the registration and landing
***********************************************************/
char * c_login(int sockfd)
{
MES *message = (MES *)Malloc(sizeof(MES)); //Data packet
MYINFO *myinfo = (MYINFO *)Malloc(sizeof(MYINFO)); //Personal information data packet
int i;
char *nameptr = message->name;
char *back = (char *)Malloc(10);
char *give = (char *)Malloc(sizeof(char));
char yn;
system("clear"); //clear screen
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t Welcomed the use of ZYP Instant Messenger\n");
printf("\n\n\n\n\n\n\n\n\n\n\n");
sleep(2);
system("clear");
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\t\t\t Register now ?\n");
loop1:
printf("\t Register(Y)/I have already registered,Login now(N)\n");
printf("\n\n\n\n\n\n\n\n\n\n\n");
setbuf(stdin,0);
scanf("%c",&yn);
if(yn == 'Y') //register
{
loop2:
system("clear");
printf("/***************** Register ***************/\n");
printf("Please input the name:");
scanf("%s",myinfo->name);
printf("Please enter your password: ");
scanf("%s",myinfo->sec);
myinfo->flt = 1;
Write(sockfd,myinfo,sizeof(MYINFO)); //send personal information to server
Read(sockfd,back,sizeof(back)); //return information
if(strcmp(back,"have") == 0) //already exists
{
printf("\nThe name you register is existing!\n");
printf("Please fill in again!\n");
sleep(1);
goto loop2;
}
printf("\nRegistraation success!\n");
printf("Welcome to the frist logining!\n");
sleep(1);
system("clear");
}
else if(yn == 'N') //login
{
loop3:
system("clear");
printf("/***************** Login ***************/\n");
printf("Please input the name:");
scanf("%s",myinfo->name);
printf("Please enter your password:");
scanf("%s",myinfo->sec);
myinfo->flt = 0;
Write(sockfd,myinfo,sizeof(MYINFO));
Read(sockfd,back,sizeof(back));
if(strcmp(back,"noth") == 0) //do not exist
{
printf("\nUser does not exist!\n");
printf("Please fill in again!\n");
sleep(1);
goto loop3;
}
else if(strcmp(back,"sece") == 0)//password error
{
printf("\nPassword is not correct!\n");
printf("Please fill in again!\n");
sleep(1);
goto loop3;
}
else if(strcmp(back,"logi") == 0)//repeat login
{
printf("\nUser is logged in!\n");
printf("Please fill in again!\n");
sleep(1);
goto loop3;
}
else if(strcmp(back,"ad") == 0)//user is administrator
{
printf("\nHello! Administrator!\n");
sleep(1);
goto loop8;
}
printf("\nLogin Successful!\n");
sleep(1);
system("clear");
}
else
{
printf("\nInvalid input!Please input again!\n");
goto loop1;
}
if(0) //for administrator
{
loop8:
system("clear");
printf("/************** Administrator ************/\n");
printf("/******** 1.Ordinary User Login **********/\n");
printf("/******** 2.Administrator Login **********/\n");
loop9:
printf("Please select:");
setbuf(stdin,0);
int choose;
scanf("%d",&choose);
if((choose < 1) || (choose > 2))
{
printf("Invalid input!Please input again!\n");
goto loop9;
}
if(choose == 1) //login as an ordinary user
{
memset(give,0,sizeof(give));
strcpy(give,"1");
Write(sockfd,give,sizeof(char));
goto loop10;
}
else //login as administrator
{
memset(give,0,sizeof(give));
strcpy(give,"2");
Write(sockfd,give,sizeof(char));
while(1)
{
system("clear");
printf("/************** Administrator ************/\n");
printf("/********** 1.Show User Is Online ********/\n");
printf("/********** 2.Banned Words ********/\n");
printf("/********** 3.Lift The Ban ********/\n");
printf("/********** 4.Kick Sb Out ********/\n");
printf("/********** 5.Exit ********/\n");
loop11:
printf("Please select:");
scanf("%d",&choose);
if((choose < 1) || (choose > 5))
{
printf("Invalid input!Please input again!\n");
goto loop11;
}
if(choose == ADMIN_SHOW) // show user online
{
memset(give,0,sizeof(give));
strcpy(give,"a");
Write(sockfd,give,sizeof(give));
ONLINE allname;
Read(sockfd,&allname,sizeof(allname));
printf("List of user is online:\n");
printf("--------------\n");
for(i = 0;i < (allname.num);i++)
{
printf("%d:%s\n",i+1,allname.name[i]);
}
printf("--------------\n");
sleep(2);
}
else if(choose == ADMIN_BAN) //ban words
{
memset(give,0,sizeof(give));
strcpy(give,"b");
Write(sockfd,give,sizeof(give));
printf("Please input the name of silenced people:");
char * adname = (char *)malloc(10);
scanf("%s",adname);
Write(sockfd,adname,10);
printf("%s is forbiddon to speck!\n",adname);
sleep(1);
}
else if(choose == ADMIN_LIFT) //lift the ban
{
memset(give,0,sizeof(give));
strcpy(give,"c");
Write(sockfd,give,sizeof(char));
printf("Please input the name you want to lift ban:");
char * adname = (char *)malloc(10);
scanf("%s",adname);
Write(sockfd,adname,10);
printf("%s have lift ban\n",adname);
sleep(1);
}
else if(choose == ADMIN_KICK) //kick sb out
{
memset(give,0,sizeof(give));
strcpy(give,"d");
Write(sockfd,give,sizeof(char));
printf("Please input the name you want to kick out:");
char * adname = (char *)malloc(10);
scanf("%s",adname);
Write(sockfd,adname,10);
printf("%s have been kick out!\n",adname);
sleep(1);
}
else //exit
{
memset(give,0,sizeof(give));
strcpy(give,"e");
Write(sockfd,give,sizeof(char));
printf("ByeBye Administrator!\n");
Close(sockfd);
exit(0);
}
}
}
}
loop10:
printf("");
Free(message);
message = NULL;
Free(back);
back = NULL;
Free(give);
give = NULL;
return (char *)(myinfo->name);
}
c_revmessage.c:
/**********************************************************
ZYP Instant Messager
Filename:c_revmessage.c
Author:zyp
Description:for revice message
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:void c_revmesg(int sockfd)
Return:null
Argument:int sockfd
Description:for rveice message
***********************************************************/
void c_revmesg(int sockfd)
{
REV *revinfo = (REV *)malloc(sizeof(REV));
int fp,ret,i;
char buffer[128]; //for send file
while(1)
{
if((Read(sockfd,revinfo,sizeof(REV))) == 0) //server exit
{
printf("The server have to exit!\n");
exit(-1);
}
else
{
if((strncmp(revinfo->message,"exit",4) == 0))//kick out
{
printf("You have been kicked out!\n");
kill(getppid(),9);
exit(-1);
}
else if(strncmp(revinfo->myname,"view",4) == 0)//show user is online
{
ONLINE allname;
memset(&allname,0,sizeof(ONLINE));
Read(sockfd,&allname,sizeof(ONLINE)); //revice allname
printf("List of user is online:\n");
printf("------------------\n");
for(i = 0;i < allname.num;i++)
{
printf("%d:%s\n",i+1,allname.name[i]);
}
printf("------------------\n");
}
else if(strncmp(revinfo->myname,"getfile",7) == 0)//download file
{
fp = open("./copy.c"/*revinfo->message*/,O_WRONLY | O_CREAT | O_APPEND,0777);
if(fp == -1)
{
printf("open failure!\n");
}
else
{
MES *getfile = (MES *)malloc(sizeof(MES));
getfile->flt = 0;
strcpy(getfile->myname,"zzzz");
strcpy(getfile->message,"getfile");
strcpy(getfile->name,"zzzz");
Write(sockfd,getfile,sizeof(MES));
do //download
{
memset(buffer,0,128);
ret = read(sockfd,buffer,128);
Write(fp,buffer,ret);
}while(ret == 128);
printf("revice a file!\n");
}
close(fp);
}
else if(strncmp(revinfo->message,"file",4) == 0)
{
char *filename = (char *)malloc(60);
strcpy(filename,revinfo->myname);
printf("Transferring file %s\n",filename);
if((fp = Open(filename,O_RDONLY,0777)) == -1)
{
//printf("open file failure!\n");
strcpy(filename,"failure");
Write(sockfd,filename,60); //send filename
}
else
{
Write(sockfd,filename,60);
sleep(1);
do
{
memset(buffer,0,128);
ret = Read(fp,buffer,128);
Write(sockfd,buffer,ret);
}while(ret == 128);
printf("Transfer file %s success!\n",filename);
}
close(fp);
}
else
{
if((strcmp(revinfo->myname,"") == 0) || (strcmp(revinfo->message,"") == 0))
{
;
}
else
{
time_t timer = time(NULL); //time show
struct tm *local = localtime(&timer);
printf("\n%s %02d:%02d:%02d\n",revinfo->myname,local->tm_hour,local->tm_min,local->tm_sec);
puts(revinfo->message);
}
}
}
}
}
c_sendmessgae.c:
/**********************************************************
ZYP Instant Messager
Filename:c_sendmessage.c
Author:zyp
Description:for send message
***********************************************************/
#include "../../include/SC.h"
/**********************************************************
Function:void sendmesg(int,char *,int)
Return:null
Argument:int sockfd,char *name,int pid
***********************************************************/
void sendmesg(int sockfd,char *name,int pid)
{
MES *message = (MES *)malloc(sizeof(MES));//Data packet
char *nameptr = message->name;
while(1)
{
strcpy(message->myname,name);
if(message->name == nameptr) //conncet to sb
{
loop6:
printf("Please enter the contact:");
setbuf(stdin,0);
scanf("%s",message->name);
setbuf(stdin,0);
nameptr = NULL;
}
setbuf(stdin,0);
fgets(message->message,200,stdin);
setbuf(stdin,0);
message->flt = 0;
if(!strncmp(message->message,"quit",4)) //offline
{
message->flt = 1;
Write(sockfd,message,200);
close(sockfd);
kill(pid,9);
exit(-1);
}
if(!(strncmp(message->message,"change",6)))//change chat
{
goto loop6;
}
if(!(strncmp(message->message,"file",4))) //transfer file
{
system("clear");
system("ls");
printf("Please input filename to transfer:");
scanf("%s",message->myname);
}
if(!(strncmp(message->message,"/hello",6)))
{
strcpy(message->message,"^_^");
}
Write(sockfd,message,200);
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)