SC.h:
[csharp] viewplain copy print ?
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #defineADMIN_SHOW 1
- #define ADMIN_BAN 2
- #defineADMIN_LIFT 3
- #define ADMIN_KICK 4
- #defineADMIN_EXIT 5
- typedef structmyinfo
- {
- int flt;
- charname[20];
- charsec[20];
- }MYINFO;
- typedef structmessage
- {
- int flt;
- charmyname[20];
- charname[20];
- charmessage[200];
- }MES;
- typedef structrev
- {
- charmyname[20];
- charmessage[200];
- int fd;
- }REV;
- typedef structonline
- {
- int num;
- charname[8][20];
- }ONLINE;
- extern voids_chat(int,sqlite3 *,MES *,REV*);
- extern voids_view(int,sqlite3 *,MES*);
- extern voids_file(int,sqlite3 *,MES *,REV*);
- extern voids_getfile(int);
#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 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:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- intSocket(intfamily,int type,intprotocol)
- {
- intsockfd;
- if((sockfd =socket(family,type,protocol)) < 0)
- {
- perror("socketerror!\n");
- exit(-1);
- }
- returnsockfd;
- }
- int Bind(intsockfd,const struct sockaddr *myaddr,socklen_t addrlen)
- {
- int ret;
- if((ret =bind(sockfd,myaddr,addrlen)) < 0)
- {
- perror("bind error!\n");
- exit(-1);
- }
- returnret;
- }
- intAccept(intsockfd,struct sockaddr*cliaddr,socklen_t *addrlen)
- {
- int ret;
- return (ret =accept(sockfd,cliaddr,addrlen));
- }
- intConnect(intsockfd,const struct sockaddr*servaddr,socklen_t addrlen)
- {
- int ret;
- if((ret =connect(sockfd,servaddr,addrlen)) == -1)
- {
- perror("connecterror!\n");
- exit(-1);
- }
- returnret;
- }
- pid_t Fork(void)
- {
- pid_t pid;
- if((pid = fork()) <0)
- {
- perror("fork error!\n");
- exit(-1);
- }
- returnpid;
- }
- int Shmget(key_tkey,int size,intshmflg)
- {
- int ret;
- if((ret =shmget(key,size,shmflg)) < 0)
- {
- perror("shmgeterror!\n");
- exit(-1);
- }
- returnret;
- }
- void*Shmat(intshmid,const void*shmaddr,intshmflg)
- {
- 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_tmode)
- {
- int fd;
- if((fd =open(pathname,flags,mode)) == -1)
- {
- perror("openerror!\n");
- }
- returnfd;
- }
- intClose(intfildes)
- {
- int ret;
- if((ret = close(fildes)) ==-1)
- {
- perror("closeerror!\n");
- exit(-1);
- }
- returnret;
- }
- 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);
- }
- returnret;
- }
- ssize_t Read(intfd,void *buf,size_tcount)
- {
- ssize_t ret;
- if((ret =read(fd,buf,count)) == -1)
- {
- perror("readerror!\n");
- exit(-1);
- }
- returnret;
- }
- void *Malloc(size_tsize)
- {
- void*ret;
- if((ret = malloc(size)) ==NULL)
- {
- perror("mallocerror!\n");
- exit(-1);
- }
- returnret;
- }
- voidFree(void*memblock)
- {
- free(memblock);
- }
#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:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- intmain()
- {
- intsockfd,bindret,i,result,nRow,nCoulum;
- sqlite3 *db;
- char *errmsg =NULL;
- char**dbresult;
- char**dbresults;
- char**dbresultr;
- REV *shmptr = NULL; //shmaddr
- intshmid;
- char buf[64];//for transferfile
- struct sockaddr_inmy_addr;
- int flt =0;
- memset((&my_addr),0,sizeof(my_addr));//setzero
- memset(buf,0,64);
- result = sqlite3_open("../myserever.db",&db);
- if(result !=SQLITE_OK)
- {
- printf("opendata base failure!\n");
- exit(-1);
- }
- sqlite3_exec(db,"drop tableonline",NULL,NULL,&errmsg);
- sqlite3_exec(db,"create table online(name text primary key unique,fdinteger,fltinteger)",NULL,NULL,&errmsg);//create table foronline
- sockfd =Socket(AF_INET,SOCK_STREAM,0);
- fcntl(sockfd,F_SETFL,O_NONBLOCK); //set socket NONBLOCK
- if(sockfd <0)
- {
- perror("sockfderror!\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 InstantMessenger\n");
- printf("\n\n\n\n\n\n\n\n\n\n\n");// welcomescreen
- sleep(2);
- system("clear");
- printf("Connecting to servernow......\n");
- sleep(1);
- system("clear");
- printf("connect to servernow!\n");
- while(1)
- {
- int listenfd =-1;
- while(listenfd <0) //wait acceptand wait send message
- {
- listenfd = Accept(sockfd,NULL,NULL);
- if(shmptr->fd !=0)
- {
- Write(shmptr->fd,shmptr,200);
- shmptr->fd = 0;
- }
- }
- if(listenfd <0)
- {
- perror("accepterror!\n");
- exit(-1);
- }
- else
- {
- pid_t pid = fork();
- if(pid <0)
- {
- perror("forkerror!\n");
- exit(-1);
- }
- if(pid > 0)//fatherprocess
- {
- ;
- }
- if(pid == 0)//subprocess
- {
- s_login(listenfd,db,shmptr);//login
- MES *come = (MES *)malloc(sizeof(MES));
- while(1)
- {
- sqlite3_exec(db,"create table chat(timetext,sendname text,message text,tonametext)",NULL,NULL,&errmsg);//create table forchat
- if(Read(listenfd,come,sizeof(MES))== 0)
- {
- char *str =sqlite3_mprintf("select name from online wherefd='%d'",listenfd);
- sqlite3_get_table(db,str,&dbresult,&nRow,&nCoulum,&errmsg);//searchname
- printf("%sMandatoryreferrals\n",dbresult[1]);
- str = sqlite3_mprintf("delete from online wherefd='%d'",listenfd);
- sqlite3_exec(db,str,0,0,&errmsg);
- sleep(1);
- break;
- }
- else if(strncmp(come->message,"file",4)== 0) //transferfile
- {
- s_file(listenfd,db,come,shmptr);
- }
- else if(strncmp(come->message,"getfile",7)== 0) //downloadfile
- {
- s_getfile(listenfd);
- }
- else if(strncmp(come->message,"view",4)== 0) //viewuser online
- {
- s_view(listenfd,db,come);
- }
- else // chat
- {
- s_chat(listenfd,db,come,shmptr);
- }
- }
- Close(listenfd);
- }
- }
- }
- sqlite3_close(db);
- return 0;
- }
#include "../../include/SC.h"
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:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- voids_chat(int listenfd,sqlite3 *db,MES*come,REV *shmptr)
- {
- inti,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 wherefd='%d'",listenfd);
- sqlite3_get_table(db,sql,&dbresults,&nRows,&nCoulums,&errmsg);//search fd
- printf("%s isoffline\n",dbresults[1]);
- sql = sqlite3_mprintf("delete from online wherefd='%d'",listenfd);
- sqlite3_exec(db,sql,0,0,&errmsg);
- sleep(1);
- exit(-1);
- }
- else //chat
- {
- if(strcmp(come->name,"all")== 0) //send toall user online
- {
- char *str =sqlite3_mprintf("select flt from online wherename='%s'",come->myname);
- sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);//isbanned?
- int flt =atoi(dbresultr[1]);
- if(flt == 1)//not ban
- {
- char *str =sqlite3_mprintf("select fd fromonline");
- sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);
- intallfd;
- 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 chatvalues('%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 wherename='%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 wherename='%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 chatvalues('%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;
- }
- }
- }
- }
#include "../../include/SC.h"
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:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- voids_login(int listenfd,sqlite3 *db,REV*shmptr)
- {
- inti,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 loginagain
- {
- loop4:
- while(Read(listenfd,myinfo,sizeof(MYINFO))== 0);
- gotoloop5;
- }
- loop5:
- if(myinfo->flt ==1) //registratoin
- {
- sql = sqlite3_mprintf("select name from peopleinfo wherename='%s'",myinfo->name);
- sqlite3_get_table(db,sql,&dbresult,&nRow,&nCoulum,&errmsg);
- if(nRow == 0)//ok
- {
- char *str1 =sqlite3_mprintf("insert into peopleinfovalues('%s','%s')",myinfo->name,myinfo->sec);
- char *str2 =sqlite3_mprintf("insert into onlinevalues('%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 wherename='%s'",myinfo->name);
- sqlite3_get_table(db,sql,&dbresult,&nRow,&nCoulum,&errmsg);
- if(nRow != 0)//exist
- {
- sql = sqlite3_mprintf("select * from online wherename='%s'",myinfo->name);
- sqlite3_get_table(db,sql,&dbresults,&nRows,&nCoulums,&errmsg);
- if(nRows !=0)//online,cannot repeat login
- {
- char *back ="login";
- Write(listenfd,back,sizeof(back));
- }
- else // offline
- {
- if(strcmp(myinfo->sec,dbresult[3])== 0) //passwordright
- {
- sql = sqlite3_mprintf("insert into onlinevalues('%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 adminstratorscree
- }
- 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)//loginfailure
- {
- gotoloop4;
- }
- printf("%s ontheline\n",myinfo->name);
- if(0) //administrator
- {
- loop7:
- printf("Administrator on theline\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 ordinaryuser
- }
- else
- {
- while(1)
- {
- while(Read(listenfd,back,sizeof(back))== 0);
- if(strncmp(back,"a",1)== 0) //view
- {
- sql = sqlite3_mprintf("select name fromonline");
- 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 wherename='%s'",back);
- sqlite3_exec(db,sql,NULL,NULL,&errmsg);
- }
- else if(strncmp(back,"c",1)== 0)//liftban
- {
- memset(back,0,10);
- read(listenfd,back,10);
- sql = sqlite3_mprintf("update online set flt=1 wherename='%s'",back);
- sqlite3_exec(db,sql,NULL,NULL,&errmsg);
- }
- else if(strncmp(back,"d",1)== 0)//kickout
- {
- memset(back,0,10);
- Read(listenfd,back,10);
- sql = sqlite3_mprintf("select fd from online wherename='%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 isoffline\n");
- close(listenfd);
- exit(0);
- }
- }
- }
- }
- loop12:
- printf("");
- }
#include "../../include/SC.h"
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("");
}
s_file.c
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- voids_file(int listenfd,sqlite3 *db,MES*come,REV *shmptr)
- {
- int ret;
- intresult,nRows,nCoulums;
- char **dbresults;
- char *errmsg =NULL;
- REV *file = (REV *)malloc(sizeof(REV));
- char *filename =(char*)malloc(60);
- charbuffer[128];
- memset(filename,0,60);
- file->fd = listenfd;
- strcpy(file->myname,come->myname);
- strcpy(file->message,"file");
- Write(listenfd,file,sizeof(REV));//send message toclient
- while((Read(listenfd,filename,60)== 0) || (strcmp(filename,"") == 0));
- if(strncmp(filename,"failure",7)== 0)
- {
- printf("transferfailure");
- ;
- }
- else
- {
- int fp =Open("./scopy.c",O_RDWR | O_CREAT |O_APPEND,0777);
- if(fp ==-1)
- {
- printf("open filefailure!\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 wherename='%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));
- }
- }
#include "../../include/SC.h"
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:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- voids_getfile(intlistenfd)
- {
- int ret;
- int fp =Open("scopy.c",O_RDONLY,0777);
- if(fp ==-1)
- {
- printf("openfile failure!\n");
- }
- else
- {
- char buffer[128];//for transferfile
- do //transfer file
- {
- memset(buffer,0,128);
- ret = Read(fp,buffer,128);
- Write(listenfd,buffer,ret);
- }while(ret ==128);
- }
- system("rm -rfscopy.c"); //remove file
- }
#include "../../include/SC.h"
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:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- int main(intargc,char*argv[])
- {
- if(argc !=2)
- {
- printf("format: ./clientserverIP\n");
- exit(-1);
- }
- intsockfd;
- char *name =(char*)Malloc(20);
- struct sockaddr_inmy_addr;
- memset((&my_addr),0,sizeof(my_addr));//setzero
- 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]);//serverIP
- 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 receivesthe message
- {
- c_revmesg(sockfd);
- }
- if(pid > 0)//send process send themessage
- {
- sendmesg(sockfd,name,pid);
- }
- Close(sockfd);
- Free(name);
- name = NULL;
- return 0;
- }
#include "../../include/SC.h"
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:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- char *c_login(intsockfd)
- {
- MES *message = (MES *)Malloc(sizeof(MES));//Datapacket
- MYINFO *myinfo = (MYINFO*)Malloc(sizeof(MYINFO));//Personal informationdata 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 InstantMessenger\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\tRegister now ?\n");
- loop1:
- printf("\tRegister(Y)/I have already registered,Loginnow(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("\n");
- printf("Please input thename:");
- scanf("%s",myinfo->name);
- printf("Please enter your password:");
- scanf("%s",myinfo->sec);
- myinfo->flt = 1;
- Write(sockfd,myinfo,sizeof(MYINFO));//send personalinformation to server
- Read(sockfd,back,sizeof(back));//returninformation
- if(strcmp(back,"have")== 0) //alreadyexists
- {
- printf("\nThename you register isexisting!\n");
- printf("Please fill inagain!\n");
- sleep(1);
- gotoloop2;
- }
- printf("\nRegistraationsuccess!\n");
- printf("Welcome to the fristlogining!\n");
- sleep(1);
- system("clear");
- }
- else if(yn == 'N')//login
- {
- loop3:
- system("clear");
- printf("\n");
- printf("Please input thename:");
- scanf("%s",myinfo->name);
- printf("Please enter yourpassword:");
- scanf("%s",myinfo->sec);
- myinfo->flt = 0;
- Write(sockfd,myinfo,sizeof(MYINFO));
- Read(sockfd,back,sizeof(back));
- if(strcmp(back,"noth")== 0) //do notexist
- {
- printf("\nUserdoes not exist!\n");
- printf("Please fill inagain!\n");
- sleep(1);
- gotoloop3;
- }
- else if(strcmp(back,"sece")== 0)//passworderror
- {
- printf("\nPassword is notcorrect!\n");
- printf("Pleasefill in again!\n");
- sleep(1);
- gotoloop3;
- }
- else if(strcmp(back,"logi")== 0)//repeatlogin
- {
- printf("\nUseris logged in!\n");
- printf("Please fill inagain!\n");
- sleep(1);
- gotoloop3;
- }
- else if(strcmp(back,"ad")== 0)//user isadministrator
- {
- printf("\nHello!Administrator!\n");
- sleep(1);
- gotoloop8;
- }
- printf("\nLoginSuccessful!\n");
- sleep(1);
- system("clear");
- }
- else
- {
- printf("\nInvalid input!Please inputagain!\n");
- gotoloop1;
- }
- if(0) //for administrator
- {
- loop8:
- system("clear");
- printf("\n");
- printf("\n");
- printf("\n");
- loop9:
- printf("Pleaseselect:");
- setbuf(stdin,0);
- intchoose;
- scanf("%d",&choose);
- if((choose < 1) ||(choose > 2))
- {
- printf("Invalid input!Please inputagain!\n");
- gotoloop9;
- }
- if(choose == 1)//login as an ordinaryuser
- {
- memset(give,0,sizeof(give));
- strcpy(give,"1");
- Write(sockfd,give,sizeof(char));
- gotoloop10;
- }
- else //login asadministrator
- {
- memset(give,0,sizeof(give));
- strcpy(give,"2");
- Write(sockfd,give,sizeof(char));
- while(1)
- {
- system("clear");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- loop11:
- printf("Please select:");
- scanf("%d",&choose);
- if((choose < 1) ||(choose > 5))
- {
- printf("Invalid input!Please inputagain!\n");
- gotoloop11;
- }
- 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 isonline:\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) //banwords
- {
- memset(give,0,sizeof(give));
- strcpy(give,"b");
- Write(sockfd,give,sizeof(give));
- printf("Please input the name of silencedpeople:");
- char * adname =(char*)malloc(10);
- scanf("%s",adname);
- Write(sockfd,adname,10);
- printf("%s is forbiddon tospeck!\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 liftban:");
- char * adname =(char*)malloc(10);
- scanf("%s",adname);
- Write(sockfd,adname,10);
- printf("%s have liftban\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 kickout:");
- char * adname =(char*)malloc(10);
- scanf("%s",adname);
- Write(sockfd,adname,10);
- printf("%s have been kickout!\n",adname);
- sleep(1);
- }
- else //exit
- {
- memset(give,0,sizeof(give));
- strcpy(give,"e");
- Write(sockfd,give,sizeof(char));
- printf("ByeByeAdministrator!\n");
- Close(sockfd);
- exit(0);
- }
- }
- }
- }
- loop10:
- printf("");
- Free(message);
- message = NULL;
- Free(back);
- back = NULL;
- Free(give);
- give = NULL;
- return (char*)(myinfo->name);
- }
#include "../../include/SC.h"
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("\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("\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("\n");
printf("\n");
printf("\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("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\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:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- voidc_revmesg(intsockfd)
- {
- REV *revinfo = (REV *)malloc(sizeof(REV));
- intfp,ret,i;
- char buffer[128];//for sendfile
- while(1)
- {
- if((Read(sockfd,revinfo,sizeof(REV)))== 0) //serverexit
- {
- printf("The server have toexit!\n");
- exit(-1);
- }
- else
- {
- if((strncmp(revinfo->message,"exit",4)== 0))//kickout
- {
- printf("Youhave been kicked out!\n");
- kill(getppid(),9);
- exit(-1);
- }
- else if(strncmp(revinfo->myname,"view",4)== 0)//show useris online
- {
- ONLINE allname;
- memset(&allname,0,sizeof(ONLINE));
- Read(sockfd,&allname,sizeof(ONLINE));//reviceallname
- printf("List of user isonline:\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)//downloadfile
- {
- fp = open("./copy.c",O_WRONLY | O_CREAT |O_APPEND,0777);
- if(fp ==-1)
- {
- printf("openfailure!\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("revicea 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 %ssuccess!\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%sd:d:d\n",revinfo->myname,local->tm_hour,local->tm_min,local->tm_sec);
- puts(revinfo->message);
- }
- }
- }
- }
- }
#include "../../include/SC.h"
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",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 d:d:d\n",revinfo->myname,local->tm_hour,local->tm_min,local->tm_sec);
puts(revinfo->message);
}
}
}
}
}
c_sendmessgae.c:
[csharp] viewplain copy print ?
- #include "../../include/SC.h"
- voidsendmesg(intsockfd,char*name,int pid)
- {
- MES *message = (MES *)malloc(sizeof(MES));//Datapacket
- char *nameptr =message->name;
- while(1)
- {
- strcpy(message->myname,name);
- if(message->name ==nameptr) //conncet to sb
- {
- loop6:
- printf("Please enter thecontact:");
- 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)))//changechat
- {
- gotoloop6;
- }
- if(!(strncmp(message->message,"file",4)))//transferfile
- {
- system("clear");
- system("ls");
- printf("Please input filename totransfer:");
- scanf("%s",message->myname);
- }
- if(!(strncmp(message->message,"/hello",6)))
- {
- strcpy(message->message,"^_^");
- }
- Write(sockfd,message,200);
- }
- }