资源简介

在linux系统用C语言编写一个多用户的聊天室管理系统。主要功能: 1.能做到3个以上用户之间的聊天; 2.系统要有用户管理功能; 3.每个用户能管理自己的权限,比如不接受信息,撤销已发的信息等; 4.聊天信息的保存,比如保存三天内的信息,或其他规定;

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#define KEYPATH “/doc/process/ipc/message_queue/chat“
 
#define NAMESIZE 16
#define MSGSIZE 128
#define LEN sizeof(struct msgbuf) - 4
 
#define SERTYPE 1
 
#define NORMAL 0
#define REGISTER 1
#define UNREGISTER -1
 
 
struct msgbuf
{
 
    long mtype;
    int subtype;
    int id;
    int status;
    char nickname[NAMESIZE];
    char mtext[MSGSIZE];
 
}buf;
 
int main(int argc char **argv)
{
 
   int msgidpidid;
   key_t key;
   int i;
   char ch;
 
   opterr = 0;
 
   buf.status = 1;
   while ( (ch = getopt(argc argv “h“)) != -1 )
   {
 
      switch (ch)
      {
 
         case ‘h‘:
                   buf.status = 0;
                   break;
       
}
    
}
 
   if ( optind >= argc )
   {
 
      fprintf(stdout “Usage: %s  [-h]\n“ argv[0]);
      exit(1);
    
}
 
   key = ftok(KEYPATH ‘c‘);
   msgid = msgget(key 0666);
   if ( msgid == -1 )
   {
 
      printf(“Can‘t connect to the server maybe it is down!\n“);
      //perror(“msgget“);
      exit(-1);
    
}
 
// register to the server
   id = getpid();
   buf.mtype = SERTYPE;
   buf.subtype = REGISTER;
   buf.id = id;
   strcpy(buf.nickname argv[optind]);
   memset(buf.mtext 0 MSGSIZE);
 
   if ( msgsnd(msgid &buf LEN 0) == -1 )
   {
  
      perror(“Failed to register“);
      exit(-1);
    
}
 
   if ( (pid = fork()) == -1 )
   {
 
      perror(“fork“);
      exit(-1);
    
}
 
   if ( pid > 0 )   // parent process
   {
   
      while ( 1 )
      {
  
         usleep(100000); 
         printf(“Please input a string : “); 
         fgets(buf.mtext MSGSIZE stdin);
         buf.mtype = SERTYPE;
 
         if ( strcmp(buf.mtext “quit\n“) == 0 )
         {
 
            kill(pid SIGQUIT);
            buf.subtype = UNREGISTER;
            if ( msgsnd(msgid &buf LEN 0) == -1 )
            {
    
               perror(“can‘t send message...“);
             
}  
            exit(0);
          
}
          
         if ( buf.status == 0 )
         {
 
            printf(“You can‘t send message in hidden mode\n“);
            continue;
          
}        
         buf.subtype = NORMAL;
         if ( msgsnd(msgid &buf LEN 0) == -1 )
         {
 
            perror(“can‘t send message...“); 
          
}
       
}
    
}
   else                  // child process
   {
 
      while ( 1 )
      {
 
         if ( msgrcv(msgid &buf LEN id 0) == -1 )
         {
 
            //perror(“can‘t receive message exit client“);
            kill(getppid() SIGINT);
            sleep(1);
            exit(-1);
          

         if ( buf.subtype == NORMAL )
         {
  
             printf(“\n[%s] %s\n“ buf.nickname buf.mtext);

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-01-07 09:18  C语言编写简单聊天室\
     文件        3167  2016-12-27 13:14  C语言编写简单聊天室\client .txt
     文件        3167  2016-12-27 13:14  C语言编写简单聊天室\client.c
     文件        4727  2016-12-27 13:15  C语言编写简单聊天室\server .txt
     文件        4727  2016-12-27 13:15  C语言编写简单聊天室\server.c
     文件        8486  2017-01-07 09:15  C语言编写简单聊天室\实验结果.png

评论

共有 条评论