聊天室软件代码

  • cyezoe
    了解作者
  • C/C++
    开发工具
  • 75.5KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 5 积分
    下载积分
  • 0
    下载次数
  • 2022-05-25 21:48
    上传日期
聊天室软件代码,可以锻炼自己对于网络编程和数据库的理解和使用
happychat.zip
内容介绍
#include <stdio.h> #include <errno.h> #include <netdb.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <sys/types.h> #include <sys/socket.h> #include "../../include/all.h" struct message msg; char now_user[10]; void send_msg(void *arg) { pthread_mutex_t mutex; int sockfd = *((int *)arg); while(1) { pthread_mutex_lock(&mutex); read(sockfd,&msg,sizeof(struct message)); switch(msg.action) { case STO_SUC: { printf("有消息来自用户%s:%s\n",msg.name,msg.msg); memset(&msg,0,sizeof(struct message)); break; } case STA_SUC: { if(strcmp(msg.name,now_user) != 0) { printf("用户%s群发消息:%s\n",msg.name,msg.msg); } memset(&msg,0,sizeof(struct message)); break; } case NO_SAY: { printf("对不起,你已经被禁言\n"); memset(&msg,0,sizeof(struct message)); break; } case SAY: { printf("恭喜你,你已经被解禁,可以发言了!\n"); memset(&msg,0,sizeof(struct message)); break; } case OUT: { printf("对不起,你已经被踢出聊天室\n"); exit(1); break; } /*case CHP_SUC: { printf("修改密码成功!\n"); memset(&msg,0,sizeof(struct message)); break; } case CHP_FAIL: { printf("原密码错误!\n"); memset(&msg,0,sizeof(struct message)); break; }*/ default: { break; } } pthread_mutex_unlock(&mutex); } } int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in server_addr; if(argc != 2) { printf("请输入服务器的IP地址\a\n"); exit(1); } if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) { fprintf(stderr,"创建套接字失败:%s\a\n",strerror(errno)); exit(1); } bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(portnum); server_addr.sin_addr.s_addr=inet_addr(argv[1]); if(connect(sockfd,(struct sockaddr*)(&server_addr),sizeof( struct sockaddr)) == -1) { fprintf(stderr,"连接服务器失败:%s\a\n",strerror(errno)); exit(1); } pthread_t id; system("clear"); while(1) { interface(); password_check(sockfd); switch(msg.action) { //printf("switch suc!\n"); case LOG_SUC: { if(strcmp(msg.name,"admin") == 0) { pthread_create(&id,NULL,(void *)send_msg,(void *)&sockfd); printf("管理员正在登录。。。\n"); strcpy(now_user,msg.name); system("reset"); admin_table(); admin_cmd(sockfd); } else { printf("用户%s正在登录。。。\n",msg.name); pthread_create(&id,NULL,(void *)send_msg,(void *)&sockfd); strcpy(now_user,msg.name); system("reset"); chat_table(); input_order(sockfd); break; } } case LOG_FAIL: { if(!strcmp(msg.name,"admin")) { printf("超级用户%s%s\n",msg.name,msg.msg); } else { printf("用户%s%s\n",msg.name,msg.msg); } sleep(1); system("reset"); break; } case LOGIN_SUC: { printf("LOGIN_SUC\n"); if(!strcmp(msg.name,"admin")) { printf("管理员正在登录...\n"); sleep(1); system("reset"); admin_table(); admin_cmd(sockfd); } else { printf("用户%s注册成功!id:%d,自动登录中...\n",msg.name,msg.id); pthread_create(&id,NULL,(void *)send_msg,(void *)&sockfd); sleep(2); strcpy(now_user,msg.name); system("reset"); chat_table(); input_order(sockfd); break; } } case LOGIN_FAIL: { if(!strcmp(msg.name,"admin")) { printf("管理员%s已经初始化!\n",msg.name); } else { printf("注册失败,用户名%s已经被占用\n",msg.name); } sleep(1); system("reset"); break; } default: { break; } } memset(&msg,0,sizeof(struct message)); } return 0; }
评论
    相关推荐