• 大小: 8KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: 其他
  • 标签: Linux  C  TCP  Thread  

资源简介

纯代码,当年大学写的毕业设计,通俗易懂,Linux平台下直接编译可用。

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define  N  5

int Client_socketfd;
char logout_name[20];

pthread_t thread_id_send;
pthread_t thread_id_recv;

typedef struct user
{
 char name[20];
 char password[20];
 char cmd_type[20];
 char chat_info[100];
}User;
User user;

typedef struct online_user_info
{
  char oline_user_name[30];
  int oline_user_count;
  
}Online_User;
Online_User online_user[N];

int _show()
{
      
  int i;
  memset((unsigned char*)&user0sizeof(User));
  strcpy(user.cmd_type“SHOW“);
  if(send(Client_socketfd&usersizeof(User)0) <0)
  {
        perror(“send error!“);
        return -1;
  }
  printf(“-----------------------------------\n“);
  printf(“online user:\n“);
  for(i=0; i   {
if(recv(Client_socketfd&online_user[i]sizeof(Online_User)0) < 0)
{
 perror(“recv error!“);
 return -1;
}
printf(“%s\n“online_user[i].oline_user_name);

if(i==online_user[i].oline_user_count-1)
break;
   }
   printf(“-----------------------------------\n“);
   
}

//发送线程

void _send()
{
        char talk[100];
while(1)
{
        memset(talk0100);
        gets(talk);
        if(strcmp(“LOGOUT“talk) == 0)
{
       strcpy(user.cmd_type“LOGOUT“);
       strcpy(user.namelogout_name);
       if(send(Client_socketfd&usersizeof(User)0) < 0)
       {
             perror(“send error!“);
             return;
       }
       printf(“\n“);
       printf(“-------------------退出聊天室--------------------\n“);
       printf(“\n“);
       break;
}
strcpy(user.cmd_type“CHAT“);
strcpy(user.chat_infotalk);
if(send(Client_socketfd&usersizeof(User)0)<0)
{
       perror(“send error!“);
       return;
}


 }
 pthread_cancel(thread_id_recv);
 zhuce_denglu();
}

//接收线程

void _recv()
{
      
       char buff[200];
       while(1)
       {
           memset(buff0200);
   if(recv(Client_socketfdbuff2000) < 0)
   {
              perror(“recv error!“);
              return;
   }
   printf(“%s\n“buff);
   
       }
}




int  chatroom()
{
      char cmd[20];
      memset(cmd020);
      while(1)
  { 
       scanf(“%s“cmd);
       if(strcmp(cmd“SHOW“) == 0)
       {      
               _show();
       }
       if(strcmp(cmd“CHAT“) == 0)
       {
               pthread_create(&thread_id_sendNULL(void*)_sendNULL);
               pthread_create(&thread_id_recvNULL(void*)_recvNULL);
               pthread_join(thread_id_sendNULL);
               pthread_join(thread_id_recvNULL);
               close(Client_socketfd);
       }
       
  }
}

//注册信息

int  process_client_cmd_REG(char *str)
{
      User user;
      char buff_1[50];
      memset(buff_1050);
  printf(“请输入您的用户名:“);
      scanf(“%s“

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-12-15 14:45  基于Linux平台的网络聊天室\
     文件        7489  2012-05-31 14:27  基于Linux平台的网络聊天室\client1.c
     文件       19725  2012-05-31 14:27  基于Linux平台的网络聊天室\server1.c

评论

共有 条评论