关闭

嵌入式 一个基于linux的聊天软件(并发服务器,sqlite3数据库,有注释)

740人阅读 评论(0) 收藏 举报
分类:

 

SC.h:

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. #include
  11. #include
  12. #include
  13. #defineADMIN_SHOW 1
  14. #define ADMIN_BAN 2
  15. #defineADMIN_LIFT 3
  16. #define ADMIN_KICK 4
  17. #defineADMIN_EXIT 5
  18. typedef structmyinfo
  19. {
  20. int flt;
  21. charname[20];
  22. charsec[20];
  23. }MYINFO;
  24. typedef structmessage
  25. {
  26. int flt;
  27. charmyname[20];
  28. charname[20];
  29. charmessage[200];
  30. }MES;
  31. typedef structrev
  32. {
  33. charmyname[20];
  34. charmessage[200];
  35. int fd;
  36. }REV;
  37. typedef structonline
  38. {
  39. int num;
  40. charname[8][20];
  41. }ONLINE;
  42. extern voids_chat(int,sqlite3 *,MES *,REV*);
  43. extern voids_view(int,sqlite3 *,MES*);
  44. extern voids_file(int,sqlite3 *,MES *,REV*);
  45. 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:

  1. #include "../../include/SC.h"
  2. intSocket(intfamily,int type,intprotocol)
  3. {
  4. intsockfd;
  5. if((sockfd =socket(family,type,protocol)) < 0)
  6. {
  7. perror("socketerror!\n");
  8. exit(-1);
  9. }
  10. returnsockfd;
  11. }
  12. int Bind(intsockfd,const struct sockaddr *myaddr,socklen_t addrlen)
  13. {
  14. int ret;
  15. if((ret =bind(sockfd,myaddr,addrlen)) < 0)
  16. {
  17. perror("bind error!\n");
  18. exit(-1);
  19. }
  20. returnret;
  21. }
  22. intAccept(intsockfd,struct sockaddr*cliaddr,socklen_t *addrlen)
  23. {
  24. int ret;
  25. return (ret =accept(sockfd,cliaddr,addrlen));
  26. }
  27. intConnect(intsockfd,const struct sockaddr*servaddr,socklen_t addrlen)
  28. {
  29. int ret;
  30. if((ret =connect(sockfd,servaddr,addrlen)) == -1)
  31. {
  32. perror("connecterror!\n");
  33. exit(-1);
  34. }
  35. returnret;
  36. }
  37. pid_t Fork(void)
  38. {
  39. pid_t pid;
  40. if((pid = fork()) <0)
  41. {
  42. perror("fork error!\n");
  43. exit(-1);
  44. }
  45. returnpid;
  46. }
  47. int Shmget(key_tkey,int size,intshmflg)
  48. {
  49. int ret;
  50. if((ret =shmget(key,size,shmflg)) < 0)
  51. {
  52. perror("shmgeterror!\n");
  53. exit(-1);
  54. }
  55. returnret;
  56. }
  57. void*Shmat(intshmid,const void*shmaddr,intshmflg)
  58. {
  59. void *shmptr =NULL;
  60. if((shmptr =shmat(shmid,shmaddr,shmflg)) == (void*)(-1))
  61. {
  62. perror("shmat error!\n");
  63. exit(-1);
  64. }
  65. return (void*)shmptr;
  66. }
  67. int Open(const char*pathname,int flags,mode_tmode)
  68. {
  69. int fd;
  70. if((fd =open(pathname,flags,mode)) == -1)
  71. {
  72. perror("openerror!\n");
  73. }
  74. returnfd;
  75. }
  76. intClose(intfildes)
  77. {
  78. int ret;
  79. if((ret = close(fildes)) ==-1)
  80. {
  81. perror("closeerror!\n");
  82. exit(-1);
  83. }
  84. returnret;
  85. }
  86. ssize_t Write(int fd,const void*buf,size_t count)
  87. {
  88. ssize_t ret;
  89. if((ret =write(fd,buf,count)) == -1)
  90. {
  91. perror("write error!\n");
  92. exit(-1);
  93. }
  94. returnret;
  95. }
  96. ssize_t Read(intfd,void *buf,size_tcount)
  97. {
  98. ssize_t ret;
  99. if((ret =read(fd,buf,count)) == -1)
  100. {
  101. perror("readerror!\n");
  102. exit(-1);
  103. }
  104. returnret;
  105. }
  106. void *Malloc(size_tsize)
  107. {
  108. void*ret;
  109. if((ret = malloc(size)) ==NULL)
  110. {
  111. perror("mallocerror!\n");
  112. exit(-1);
  113. }
  114. returnret;
  115. }
  116. voidFree(void*memblock)
  117. {
  118. free(memblock);
  119. }
#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:

  1. #include "../../include/SC.h"
  2. intmain()
  3. {
  4. intsockfd,bindret,i,result,nRow,nCoulum;
  5. sqlite3 *db;
  6. char *errmsg =NULL;
  7. char**dbresult;
  8. char**dbresults;
  9. char**dbresultr;
  10. REV *shmptr = NULL; //shmaddr
  11. intshmid;
  12. char buf[64];//for transferfile
  13. struct sockaddr_inmy_addr;
  14. int flt =0;
  15. memset((&my_addr),0,sizeof(my_addr));//setzero
  16. memset(buf,0,64);
  17. result = sqlite3_open("../myserever.db",&db);
  18. if(result !=SQLITE_OK)
  19. {
  20. printf("opendata base failure!\n");
  21. exit(-1);
  22. }
  23. sqlite3_exec(db,"drop tableonline",NULL,NULL,&errmsg);
  24. sqlite3_exec(db,"create table online(name text primary key unique,fdinteger,fltinteger)",NULL,NULL,&errmsg);//create table foronline
  25. sockfd =Socket(AF_INET,SOCK_STREAM,0);
  26. fcntl(sockfd,F_SETFL,O_NONBLOCK); //set socket NONBLOCK
  27. if(sockfd <0)
  28. {
  29. perror("sockfderror!\n");
  30. exit(-1);
  31. }
  32. my_addr.sin_family = AF_INET;
  33. my_addr.sin_port = htons(6666);
  34. my_addr.sin_addr.s_addr = INADDR_ANY; //auto bind
  35. Bind(sockfd,(struct sockaddr*)(&my_addr),sizeof(my_addr));
  36. listen(sockfd,8);
  37. shmid = Shmget(IPC_PRIVATE,sizeof(REV),0666);
  38. shmptr = (REV *)Shmat(shmid,0,0);
  39. system("clear");
  40. printf("\n\n\n\n\n\n\n\n\n\n\n");
  41. printf("\t\t Welcomed the use of ZYP InstantMessenger\n");
  42. printf("\n\n\n\n\n\n\n\n\n\n\n");// welcomescreen
  43. sleep(2);
  44. system("clear");
  45. printf("Connecting to servernow......\n");
  46. sleep(1);
  47. system("clear");
  48. printf("connect to servernow!\n");
  49. while(1)
  50. {
  51. int listenfd =-1;
  52. while(listenfd <0) //wait acceptand wait send message
  53. {
  54. listenfd = Accept(sockfd,NULL,NULL);
  55. if(shmptr->fd !=0)
  56. {
  57. Write(shmptr->fd,shmptr,200);
  58. shmptr->fd = 0;
  59. }
  60. }
  61. if(listenfd <0)
  62. {
  63. perror("accepterror!\n");
  64. exit(-1);
  65. }
  66. else
  67. {
  68. pid_t pid = fork();
  69. if(pid <0)
  70. {
  71. perror("forkerror!\n");
  72. exit(-1);
  73. }
  74. if(pid > 0)//fatherprocess
  75. {
  76. ;
  77. }
  78. if(pid == 0)//subprocess
  79. {
  80. s_login(listenfd,db,shmptr);//login
  81. MES *come = (MES *)malloc(sizeof(MES));
  82. while(1)
  83. {
  84. sqlite3_exec(db,"create table chat(timetext,sendname text,message text,tonametext)",NULL,NULL,&errmsg);//create table forchat
  85. if(Read(listenfd,come,sizeof(MES))== 0)
  86. {
  87. char *str =sqlite3_mprintf("select name from online wherefd='%d'",listenfd);
  88. sqlite3_get_table(db,str,&dbresult,&nRow,&nCoulum,&errmsg);//searchname
  89. printf("%sMandatoryreferrals\n",dbresult[1]);
  90. str = sqlite3_mprintf("delete from online wherefd='%d'",listenfd);
  91. sqlite3_exec(db,str,0,0,&errmsg);
  92. sleep(1);
  93. break;
  94. }
  95. else if(strncmp(come->message,"file",4)== 0) //transferfile
  96. {
  97. s_file(listenfd,db,come,shmptr);
  98. }
  99. else if(strncmp(come->message,"getfile",7)== 0) //downloadfile
  100. {
  101. s_getfile(listenfd);
  102. }
  103. else if(strncmp(come->message,"view",4)== 0) //viewuser online
  104. {
  105. s_view(listenfd,db,come);
  106. }
  107. else // chat
  108. {
  109. s_chat(listenfd,db,come,shmptr);
  110. }
  111. }
  112. Close(listenfd);
  113. }
  114. }
  115. }
  116. sqlite3_close(db);
  117. return 0;
  118. }
#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:

  1. #include "../../include/SC.h"
  2. voids_chat(int listenfd,sqlite3 *db,MES*come,REV *shmptr)
  3. {
  4. inti,result,nRow,nRows,nCoulum,nCoulums;
  5. char *errmsg =NULL;
  6. char**dbresult;
  7. char**dbresults;
  8. char**dbresultr;
  9. if(come->flt == 1)//offline
  10. {
  11. char *sql =sqlite3_mprintf("select name from online wherefd='%d'",listenfd);
  12. sqlite3_get_table(db,sql,&dbresults,&nRows,&nCoulums,&errmsg);//search fd
  13. printf("%s isoffline\n",dbresults[1]);
  14. sql = sqlite3_mprintf("delete from online wherefd='%d'",listenfd);
  15. sqlite3_exec(db,sql,0,0,&errmsg);
  16. sleep(1);
  17. exit(-1);
  18. }
  19. else //chat
  20. {
  21. if(strcmp(come->name,"all")== 0) //send toall user online
  22. {
  23. char *str =sqlite3_mprintf("select flt from online wherename='%s'",come->myname);
  24. sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);//isbanned?
  25. int flt =atoi(dbresultr[1]);
  26. if(flt == 1)//not ban
  27. {
  28. char *str =sqlite3_mprintf("select fd fromonline");
  29. sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);
  30. intallfd;
  31. for(i = 1;i <=nRow;i++)
  32. {
  33. allfd = atoi(dbresultr[i]);
  34. if(allfd !=listenfd)
  35. {
  36. REV *shmsend = (REV *)malloc(sizeof(REV));
  37. shmsend->fd = allfd;
  38. strcpy(shmsend->myname,come->myname);
  39. strcpy(shmsend->message,come->message);
  40. memcpy(shmptr,shmsend,sizeof(REV));
  41. int delay =10000000;
  42. while(delay--);
  43. Free(shmsend);
  44. shmsend = NULL;
  45. }
  46. }
  47. time_t timer = time(NULL);
  48. char *tme =(char*)ctime(&timer);
  49. str = sqlite3_mprintf("insert into chatvalues('%s','%s','%s','%s')",tme,come->myname,come->message,come->name);
  50. sqlite3_exec(db,str,0,0,&errmsg);//save chat
  51. sqlite3_free_table(dbresultr);
  52. dbresultr = NULL;
  53. }
  54. else //banned
  55. {
  56. REV *jin = (REV *)malloc(sizeof(REV));
  57. memset(jin,0,sizeof(REV));
  58. strcpy(jin->myname,"Administrator");
  59. strcpy(jin->message,"Youbanned");
  60. Write(listenfd,jin,sizeof(REV));
  61. Free(jin);
  62. jin = NULL;
  63. }
  64. }
  65. else //to sb online
  66. {
  67. char *str =sqlite3_mprintf("select flt from online wherename='%s'",come->myname);
  68. sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);
  69. int flt =atoi(dbresultr[1]);
  70. if(flt ==1)
  71. {
  72. char *str =sqlite3_mprintf("select fd from online wherename='%s'",come->name);
  73. sqlite3_get_table(db,str,&dbresultr,&nRow,&nCoulum,&errmsg);
  74. int sendfd =atoi(dbresultr[1]);
  75. REV *shmsend = (REV *)malloc(sizeof(REV));
  76. shmsend->fd = sendfd;
  77. strcpy(shmsend->myname,come->myname);
  78. strcpy(shmsend->message,come->message);
  79. memcpy(shmptr,shmsend,sizeof(REV));
  80. time_t timer = time(NULL);
  81. char *tme =(char*)ctime(&timer);
  82. str = sqlite3_mprintf("insert into chatvalues('%s','%s','%s','%s')",tme,come->myname,come->message,come->name);
  83. sqlite3_exec(db,str,0,0,&errmsg); //save chat record
  84. Free(shmsend);
  85. shmsend = NULL;
  86. }
  87. else //banned
  88. {
  89. REV *jin = (REV *)malloc(sizeof(REV));
  90. memset(jin,0,sizeof(REV));
  91. strcpy(jin->myname,"Administrator");
  92. strcpy(jin->message,"Youbanned");
  93. Write(listenfd,jin,sizeof(REV));
  94. Free(jin);
  95. jin = NULL;
  96. }
  97. }
  98. }
  99. }
#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:

  1. #include "../../include/SC.h"
  2. voids_login(int listenfd,sqlite3 *db,REV*shmptr)
  3. {
  4. inti,result,nRow,nRows,nCoulum,nCoulums;
  5. int flt =0;
  6. char *errmsg =NULL;
  7. char**dbresult;
  8. char**dbresults;
  9. char**dbresultr;
  10. char *sql =NULL;
  11. MYINFO *myinfo = (MYINFO *)malloc(sizeof(MYINFO));
  12. memset(myinfo,0,sizeof(MYINFO));
  13. while(read(listenfd,myinfo,sizeof(MYINFO))== 0);
  14. if(0) //login failure and loginagain
  15. {
  16. loop4:
  17. while(Read(listenfd,myinfo,sizeof(MYINFO))== 0);
  18. gotoloop5;
  19. }
  20. loop5:
  21. if(myinfo->flt ==1) //registratoin
  22. {
  23. sql = sqlite3_mprintf("select name from peopleinfo wherename='%s'",myinfo->name);
  24. sqlite3_get_table(db,sql,&dbresult,&nRow,&nCoulum,&errmsg);
  25. if(nRow == 0)//ok
  26. {
  27. char *str1 =sqlite3_mprintf("insert into peopleinfovalues('%s','%s')",myinfo->name,myinfo->sec);
  28. char *str2 =sqlite3_mprintf("insert into onlinevalues('%s','%d','%d')",myinfo->name,listenfd,1);
  29. sqlite3_exec(db,str1,0,0,&errmsg);
  30. sqlite3_exec(db,str2,0,0,&errmsg);
  31. char *back ="yes";
  32. flt = 1;
  33. Write(listenfd,back,sizeof(back));
  34. }
  35. else //repeat
  36. {
  37. char *back ="haved";
  38. Write(listenfd,back,sizeof(back));
  39. }
  40. }
  41. else //login
  42. {
  43. sql = sqlite3_mprintf("select * from peopleinfo wherename='%s'",myinfo->name);
  44. sqlite3_get_table(db,sql,&dbresult,&nRow,&nCoulum,&errmsg);
  45. if(nRow != 0)//exist
  46. {
  47. sql = sqlite3_mprintf("select * from online wherename='%s'",myinfo->name);
  48. sqlite3_get_table(db,sql,&dbresults,&nRows,&nCoulums,&errmsg);
  49. if(nRows !=0)//online,cannot repeat login
  50. {
  51. char *back ="login";
  52. Write(listenfd,back,sizeof(back));
  53. }
  54. else // offline
  55. {
  56. if(strcmp(myinfo->sec,dbresult[3])== 0) //passwordright
  57. {
  58. sql = sqlite3_mprintf("insert into onlinevalues('%s','%d','%d')",myinfo->name,listenfd,1);
  59. sqlite3_exec(db,sql,0,0,&errmsg);
  60. if(strcmp(myinfo->name,"admin")== 0) //administrator
  61. {
  62. char *back ="ad";
  63. Write(listenfd,back,sizeof(back));
  64. goto loop7;//goto adminstratorscree
  65. }
  66. char *back ="yes";
  67. flt = 1;
  68. Write(listenfd,back,sizeof(back));
  69. }
  70. else // password error
  71. {
  72. char *back ="secerror";
  73. Write(listenfd,back,sizeof(back));
  74. }
  75. }
  76. }
  77. else //not exist
  78. {
  79. char *back ="nothave";
  80. Write(listenfd,back,sizeof(back));
  81. }
  82. }
  83. if(flt != 1)//loginfailure
  84. {
  85. gotoloop4;
  86. }
  87. printf("%s ontheline\n",myinfo->name);
  88. if(0) //administrator
  89. {
  90. loop7:
  91. printf("Administrator on theline\n");
  92. char *back =(char*)malloc(10);
  93. memset(back,0,10);
  94. while(Read(listenfd,back,sizeof(back))== 0);
  95. if(strncmp(back,"1",1)== 0)
  96. {
  97. goto loop12;//login as an ordinaryuser
  98. }
  99. else
  100. {
  101. while(1)
  102. {
  103. while(Read(listenfd,back,sizeof(back))== 0);
  104. if(strncmp(back,"a",1)== 0) //view
  105. {
  106. sql = sqlite3_mprintf("select name fromonline");
  107. sqlite3_get_table(db,sql,&dbresults,&nRows,&nCoulums,&errmsg);
  108. int *backnum =((int*)nRow);
  109. ONLINE allname;
  110. allname.num = (int)nRows;
  111. for(i = 1;i <=nRows;i++)
  112. {
  113. strcpy(allname.name[i-1],dbresults[i]);
  114. }
  115. Write(listenfd,&allname,sizeof(allname));
  116. }
  117. else if(strncmp(back,"b",1)== 0)//ban
  118. {
  119. memset(back,0,10);
  120. read(listenfd,back,10);
  121. sql = sqlite3_mprintf("update online set flt=0 wherename='%s'",back);
  122. sqlite3_exec(db,sql,NULL,NULL,&errmsg);
  123. }
  124. else if(strncmp(back,"c",1)== 0)//liftban
  125. {
  126. memset(back,0,10);
  127. read(listenfd,back,10);
  128. sql = sqlite3_mprintf("update online set flt=1 wherename='%s'",back);
  129. sqlite3_exec(db,sql,NULL,NULL,&errmsg);
  130. }
  131. else if(strncmp(back,"d",1)== 0)//kickout
  132. {
  133. memset(back,0,10);
  134. Read(listenfd,back,10);
  135. sql = sqlite3_mprintf("select fd from online wherename='%s'",back);
  136. sqlite3_get_table(db,sql,&dbresult,&nRow,&nCoulum,&errmsg);
  137. REV *ti = (REV *)malloc(sizeof(REV));
  138. strcpy(ti->myname,"Administrator");
  139. strcpy(ti->message,"exit");
  140. ti->fd = atoi(dbresult[1]);
  141. memcpy(shmptr,ti,sizeof(REV));
  142. }
  143. else //exit
  144. {
  145. printf("Administrator isoffline\n");
  146. close(listenfd);
  147. exit(0);
  148. }
  149. }
  150. }
  151. }
  152. loop12:
  153. printf("");
  154. }
#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
  1. #include "../../include/SC.h"
  2. voids_file(int listenfd,sqlite3 *db,MES*come,REV *shmptr)
  3. {
  4. int ret;
  5. intresult,nRows,nCoulums;
  6. char **dbresults;
  7. char *errmsg =NULL;
  8. REV *file = (REV *)malloc(sizeof(REV));
  9. char *filename =(char*)malloc(60);
  10. charbuffer[128];
  11. memset(filename,0,60);
  12. file->fd = listenfd;
  13. strcpy(file->myname,come->myname);
  14. strcpy(file->message,"file");
  15. Write(listenfd,file,sizeof(REV));//send message toclient
  16. while((Read(listenfd,filename,60)== 0) || (strcmp(filename,"") == 0));
  17. if(strncmp(filename,"failure",7)== 0)
  18. {
  19. printf("transferfailure");
  20. ;
  21. }
  22. else
  23. {
  24. int fp =Open("./scopy.c",O_RDWR | O_CREAT |O_APPEND,0777);
  25. if(fp ==-1)
  26. {
  27. printf("open filefailure!\n");
  28. }
  29. else
  30. {
  31. do //transfer file
  32. {
  33. memset(buffer,0,128);
  34. ret = Read(listenfd,buffer,128);
  35. Write(fp,buffer,ret);
  36. }while(ret ==128);
  37. }
  38. Close(fp);
  39. char *str =sqlite3_mprintf("select fd from online wherename='%s'",come->name);
  40. sqlite3_get_table(db,str,&dbresults,&nRows,&nCoulums,&errmsg);
  41. int tofd =((dbresults[1][0]) - '0');
  42. file->fd = tofd; //send message to target client to download
  43. strcpy(file->myname,"getfile");
  44. strcpy(file->message,filename);
  45. memcpy(shmptr,file,sizeof(REV));
  46. }
  47. }
#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:

  1. #include "../../include/SC.h"
  2. voids_getfile(intlistenfd)
  3. {
  4. int ret;
  5. int fp =Open("scopy.c",O_RDONLY,0777);
  6. if(fp ==-1)
  7. {
  8. printf("openfile failure!\n");
  9. }
  10. else
  11. {
  12. char buffer[128];//for transferfile
  13. do //transfer file
  14. {
  15. memset(buffer,0,128);
  16. ret = Read(fp,buffer,128);
  17. Write(listenfd,buffer,ret);
  18. }while(ret ==128);
  19. }
  20. system("rm -rfscopy.c"); //remove file
  21. }
#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:

  1. #include "../../include/SC.h"
  2. int main(intargc,char*argv[])
  3. {
  4. if(argc !=2)
  5. {
  6. printf("format: ./clientserverIP\n");
  7. exit(-1);
  8. }
  9. intsockfd;
  10. char *name =(char*)Malloc(20);
  11. struct sockaddr_inmy_addr;
  12. memset((&my_addr),0,sizeof(my_addr));//setzero
  13. sockfd =Socket(AF_INET,SOCK_STREAM,0);
  14. my_addr.sin_family = AF_INET;
  15. my_addr.sin_port = htons(6666); //Port
  16. my_addr.sin_addr.s_addr = inet_addr(argv[1]);//serverIP
  17. Connect(sockfd,(struct sockaddr*)(&my_addr),sizeof(my_addr));
  18. name = (char*)c_login(sockfd); //save local user name
  19. pid_t pid = Fork();
  20. if(pid == 0)//Sub process receivesthe message
  21. {
  22. c_revmesg(sockfd);
  23. }
  24. if(pid > 0)//send process send themessage
  25. {
  26. sendmesg(sockfd,name,pid);
  27. }
  28. Close(sockfd);
  29. Free(name);
  30. name = NULL;
  31. return 0;
  32. }
#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:

  1. #include "../../include/SC.h"
  2. char *c_login(intsockfd)
  3. {
  4. MES *message = (MES *)Malloc(sizeof(MES));//Datapacket
  5. MYINFO *myinfo = (MYINFO*)Malloc(sizeof(MYINFO));//Personal informationdata packet
  6. int i;
  7. char *nameptr =message->name;
  8. char *back =(char*)Malloc(10);
  9. char *give =(char*)Malloc(sizeof(char));
  10. char yn;
  11. system("clear"); //clear screen
  12. printf("\n\n\n\n\n\n\n\n\n\n\n");
  13. printf("\t\t Welcomed the use of ZYP InstantMessenger\n");
  14. printf("\n\n\n\n\n\n\n\n\n\n\n");
  15. sleep(2);
  16. system("clear");
  17. printf("\n\n\n\n\n\n\n\n\n\n");
  18. printf("\t\t\tRegister now ?\n");
  19. loop1:
  20. printf("\tRegister(Y)/I have already registered,Loginnow(N)\n");
  21. printf("\n\n\n\n\n\n\n\n\n\n\n");
  22. setbuf(stdin,0);
  23. scanf("%c",&yn);
  24. if(yn == 'Y')//register
  25. {
  26. loop2:
  27. system("clear");
  28. printf("\n");
  29. printf("Please input thename:");
  30. scanf("%s",myinfo->name);
  31. printf("Please enter your password:");
  32. scanf("%s",myinfo->sec);
  33. myinfo->flt = 1;
  34. Write(sockfd,myinfo,sizeof(MYINFO));//send personalinformation to server
  35. Read(sockfd,back,sizeof(back));//returninformation
  36. if(strcmp(back,"have")== 0) //alreadyexists
  37. {
  38. printf("\nThename you register isexisting!\n");
  39. printf("Please fill inagain!\n");
  40. sleep(1);
  41. gotoloop2;
  42. }
  43. printf("\nRegistraationsuccess!\n");
  44. printf("Welcome to the fristlogining!\n");
  45. sleep(1);
  46. system("clear");
  47. }
  48. else if(yn == 'N')//login
  49. {
  50. loop3:
  51. system("clear");
  52. printf("\n");
  53. printf("Please input thename:");
  54. scanf("%s",myinfo->name);
  55. printf("Please enter yourpassword:");
  56. scanf("%s",myinfo->sec);
  57. myinfo->flt = 0;
  58. Write(sockfd,myinfo,sizeof(MYINFO));
  59. Read(sockfd,back,sizeof(back));
  60. if(strcmp(back,"noth")== 0) //do notexist
  61. {
  62. printf("\nUserdoes not exist!\n");
  63. printf("Please fill inagain!\n");
  64. sleep(1);
  65. gotoloop3;
  66. }
  67. else if(strcmp(back,"sece")== 0)//passworderror
  68. {
  69. printf("\nPassword is notcorrect!\n");
  70. printf("Pleasefill in again!\n");
  71. sleep(1);
  72. gotoloop3;
  73. }
  74. else if(strcmp(back,"logi")== 0)//repeatlogin
  75. {
  76. printf("\nUseris logged in!\n");
  77. printf("Please fill inagain!\n");
  78. sleep(1);
  79. gotoloop3;
  80. }
  81. else if(strcmp(back,"ad")== 0)//user isadministrator
  82. {
  83. printf("\nHello!Administrator!\n");
  84. sleep(1);
  85. gotoloop8;
  86. }
  87. printf("\nLoginSuccessful!\n");
  88. sleep(1);
  89. system("clear");
  90. }
  91. else
  92. {
  93. printf("\nInvalid input!Please inputagain!\n");
  94. gotoloop1;
  95. }
  96. if(0) //for administrator
  97. {
  98. loop8:
  99. system("clear");
  100. printf("\n");
  101. printf("\n");
  102. printf("\n");
  103. loop9:
  104. printf("Pleaseselect:");
  105. setbuf(stdin,0);
  106. intchoose;
  107. scanf("%d",&choose);
  108. if((choose < 1) ||(choose > 2))
  109. {
  110. printf("Invalid input!Please inputagain!\n");
  111. gotoloop9;
  112. }
  113. if(choose == 1)//login as an ordinaryuser
  114. {
  115. memset(give,0,sizeof(give));
  116. strcpy(give,"1");
  117. Write(sockfd,give,sizeof(char));
  118. gotoloop10;
  119. }
  120. else //login asadministrator
  121. {
  122. memset(give,0,sizeof(give));
  123. strcpy(give,"2");
  124. Write(sockfd,give,sizeof(char));
  125. while(1)
  126. {
  127. system("clear");
  128. printf("\n");
  129. printf("\n");
  130. printf("\n");
  131. printf("\n");
  132. printf("\n");
  133. printf("\n");
  134. loop11:
  135. printf("Please select:");
  136. scanf("%d",&choose);
  137. if((choose < 1) ||(choose > 5))
  138. {
  139. printf("Invalid input!Please inputagain!\n");
  140. gotoloop11;
  141. }
  142. if(choose ==ADMIN_SHOW) //show user online
  143. {
  144. memset(give,0,sizeof(give));
  145. strcpy(give,"a");
  146. Write(sockfd,give,sizeof(give));
  147. ONLINE allname;
  148. Read(sockfd,&allname,sizeof(allname));
  149. printf("List of user isonline:\n");
  150. printf("--------------\n");
  151. for(i = 0;i <(allname.num);i++)
  152. {
  153. printf("%d:%s\n",i+1,allname.name[i]);
  154. }
  155. printf("--------------\n");
  156. sleep(2);
  157. }
  158. else if(choose ==ADMIN_BAN) //banwords
  159. {
  160. memset(give,0,sizeof(give));
  161. strcpy(give,"b");
  162. Write(sockfd,give,sizeof(give));
  163. printf("Please input the name of silencedpeople:");
  164. char * adname =(char*)malloc(10);
  165. scanf("%s",adname);
  166. Write(sockfd,adname,10);
  167. printf("%s is forbiddon tospeck!\n",adname);
  168. sleep(1);
  169. }
  170. else if(choose ==ADMIN_LIFT) //lift the ban
  171. {
  172. memset(give,0,sizeof(give));
  173. strcpy(give,"c");
  174. Write(sockfd,give,sizeof(char));
  175. printf("Please input the name you want to liftban:");
  176. char * adname =(char*)malloc(10);
  177. scanf("%s",adname);
  178. Write(sockfd,adname,10);
  179. printf("%s have liftban\n",adname);
  180. sleep(1);
  181. }
  182. else if(choose ==ADMIN_KICK) //kick sb out
  183. {
  184. memset(give,0,sizeof(give));
  185. strcpy(give,"d");
  186. Write(sockfd,give,sizeof(char));
  187. printf("Please input the name you want to kickout:");
  188. char * adname =(char*)malloc(10);
  189. scanf("%s",adname);
  190. Write(sockfd,adname,10);
  191. printf("%s have been kickout!\n",adname);
  192. sleep(1);
  193. }
  194. else //exit
  195. {
  196. memset(give,0,sizeof(give));
  197. strcpy(give,"e");
  198. Write(sockfd,give,sizeof(char));
  199. printf("ByeByeAdministrator!\n");
  200. Close(sockfd);
  201. exit(0);
  202. }
  203. }
  204. }
  205. }
  206. loop10:
  207. printf("");
  208. Free(message);
  209. message = NULL;
  210. Free(back);
  211. back = NULL;
  212. Free(give);
  213. give = NULL;
  214. return (char*)(myinfo->name);
  215. }
#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:

  1. #include "../../include/SC.h"
  2. voidc_revmesg(intsockfd)
  3. {
  4. REV *revinfo = (REV *)malloc(sizeof(REV));
  5. intfp,ret,i;
  6. char buffer[128];//for sendfile
  7. while(1)
  8. {
  9. if((Read(sockfd,revinfo,sizeof(REV)))== 0) //serverexit
  10. {
  11. printf("The server have toexit!\n");
  12. exit(-1);
  13. }
  14. else
  15. {
  16. if((strncmp(revinfo->message,"exit",4)== 0))//kickout
  17. {
  18. printf("Youhave been kicked out!\n");
  19. kill(getppid(),9);
  20. exit(-1);
  21. }
  22. else if(strncmp(revinfo->myname,"view",4)== 0)//show useris online
  23. {
  24. ONLINE allname;
  25. memset(&allname,0,sizeof(ONLINE));
  26. Read(sockfd,&allname,sizeof(ONLINE));//reviceallname
  27. printf("List of user isonline:\n");
  28. printf("------------------\n");
  29. for(i = 0;i <allname.num;i++)
  30. {
  31. printf("%d:%s\n",i+1,allname.name[i]);
  32. }
  33. printf("------------------\n");
  34. }
  35. else if(strncmp(revinfo->myname,"getfile",7)== 0)//downloadfile
  36. {
  37. fp = open("./copy.c",O_WRONLY | O_CREAT |O_APPEND,0777);
  38. if(fp ==-1)
  39. {
  40. printf("openfailure!\n");
  41. }
  42. else
  43. {
  44. MES *getfile = (MES *)malloc(sizeof(MES));
  45. getfile->flt = 0;
  46. strcpy(getfile->myname,"zzzz");
  47. strcpy(getfile->message,"getfile");
  48. strcpy(getfile->name,"zzzz");
  49. Write(sockfd,getfile,sizeof(MES));
  50. do //download
  51. {
  52. memset(buffer,0,128);
  53. ret = read(sockfd,buffer,128);
  54. Write(fp,buffer,ret);
  55. }while(ret ==128);
  56. printf("revicea file!\n");
  57. }
  58. close(fp);
  59. }
  60. else if(strncmp(revinfo->message,"file",4)== 0)
  61. {
  62. char *filename =(char*)malloc(60);
  63. strcpy(filename,revinfo->myname);
  64. printf("Transferring file%s\n",filename);
  65. if((fp =Open(filename,O_RDONLY,0777)) == -1)
  66. {
  67. //printf("open file failure!\n");
  68. strcpy(filename,"failure");
  69. Write(sockfd,filename,60); //send filename
  70. }
  71. else
  72. {
  73. Write(sockfd,filename,60);
  74. sleep(1);
  75. do
  76. {
  77. memset(buffer,0,128);
  78. ret = Read(fp,buffer,128);
  79. Write(sockfd,buffer,ret);
  80. }while(ret ==128);
  81. printf("Transfer file %ssuccess!\n",filename);
  82. }
  83. close(fp);
  84. }
  85. else
  86. {
  87. if((strcmp(revinfo->myname,"")== 0) || (strcmp(revinfo->message,"") ==0))
  88. {
  89. ;
  90. }
  91. else
  92. {
  93. time_t timer = time(NULL); //time show
  94. struct tm *local =localtime(&timer);
  95. printf("\n%sd:d:d\n",revinfo->myname,local->tm_hour,local->tm_min,local->tm_sec);
  96. puts(revinfo->message);
  97. }
  98. }
  99. }
  100. }
  101. }
#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:

  1. #include "../../include/SC.h"
  2. voidsendmesg(intsockfd,char*name,int pid)
  3. {
  4. MES *message = (MES *)malloc(sizeof(MES));//Datapacket
  5. char *nameptr =message->name;
  6. while(1)
  7. {
  8. strcpy(message->myname,name);
  9. if(message->name ==nameptr) //conncet to sb
  10. {
  11. loop6:
  12. printf("Please enter thecontact:");
  13. setbuf(stdin,0);
  14. scanf("%s",message->name);
  15. setbuf(stdin,0);
  16. nameptr = NULL;
  17. }
  18. setbuf(stdin,0);
  19. fgets(message->message,200,stdin);
  20. setbuf(stdin,0);
  21. message->flt = 0;
  22. if(!strncmp(message->message,"quit",4))//offline
  23. {
  24. message->flt = 1;
  25. Write(sockfd,message,200);
  26. close(sockfd);
  27. kill(pid,9);
  28. exit(-1);
  29. }
  30. if(!(strncmp(message->message,"change",6)))//changechat
  31. {
  32. gotoloop6;
  33. }
  34. if(!(strncmp(message->message,"file",4)))//transferfile
  35. {
  36. system("clear");
  37. system("ls");
  38. printf("Please input filename totransfer:");
  39. scanf("%s",message->myname);
  40. }
  41. if(!(strncmp(message->message,"/hello",6)))
  42. {
  43. strcpy(message->message,"^_^");
  44. }
  45. Write(sockfd,message,200);
  46. }
  47. }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:978345次
    • 积分:17617
    • 等级:
    • 排名:第540名
    • 原创:741篇
    • 转载:522篇
    • 译文:0篇
    • 评论:105条
    最新评论