Linux Linux函數 Linux聊天程式 基於socket的TCP(有串連的)聊天程式

來源:互聯網
上載者:User

/*在此只付上標頭檔,如需要去我的資源下載。   

http://download.csdn.net/detail/ta893115871/4322905

O(∩_∩)O謝謝

暫時實現的功能有:

1.群聊

2.私聊

3.登陸驗證

4.註冊

5.所用的線上使用者

6.退出

7.管理員踢出某使用者

8.管理員禁止某使用者聊天

9.管理員提升某使用者為管理員

10.管理員把某使用者降為普通使用者

*/

/*Author QQ:       893115871Author Email:gfj19900401@163.com 標頭檔,函式宣告和宏定義  */#ifndef CHAT_H#define CHAT_H#include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/types.h>#include<sys/socket.h>//#include<linux/in.h>#include<arpa/inet.h>#include<pthread.h>#include<fcntl.h>#include<sys/stat.h>#include<sys/types.h>#include<signal.h>#include<time.h>   //for asctime() and time()#include<ctype.h>    //for ispunct() #define QUEUELEN     100  //max of  client#define BUF_SIZE     256  //#define IP        "127.0.0.1"#define IP          "192.168.1.101"   //remote ip#define PORT         1234             // port#define FILENAME     "userinfo"       // file name for save users info(name:passwd:id:authority)#define CLIENT_ID    1000    // client id //========================functions================================================#define PUBLIC_CHAT           1  //群聊#define PRIVATE_CHAT          2  //私聊#define CLIENT_LOGIN          3  //登陸驗證#define CLIENT_REGISTER       4  //註冊#define CLIENT_ONLINE         5  //所用的線上使用者#define CLIENT_EXIT           6  //退出#define ADMIN_KICK_CLIENT       7  //管理員踢出某使用者#define ADMIN_SHUTUP_CLIENT     8  //管理員禁止某使用者聊天#define ADVANCED_CLIENT_TO_ADMIN   9  //管理員提升某使用者為管理員#define DROP_CLIENT_TO_NORMAL    10  //管理員把某使用者降為普通使用者//====================================================================#define NORMAL_USER_LOGIN_FAILED      0 //普通使用者登入失敗#define NORMAL_USER_LOGIN_SUCCESS     1 //普通使用者登入成功#define NORMAL_USER_LOGIN_FAILED_ONLINE  2 //普通使用者已經線上#define ADMIN_LOGIN_SUCCESS              3 //管理員登入成功#define NORMAL_USER_LOGIN_PASSWD_ERROR   4 //普通使用者登入密碼錯誤#define REGIST_FALIED   0   //註冊失敗#define REGIST_EXITED   1   //註冊的使用者已經存在#define NORMAL_USER     0   //普通使用者#define ADMIN_USER      1   //管理員// ===========client attr==========================================typedef struct{ pthread_t tid;     //線程的描述符 int  sockfd;       //accept的返回的用戶端的新的通訊端描述符 char client_name[25];  // 帳號 char client_passwd[25]; //密碼 int client_id;     //使用者ID  int is_online;     // 線上狀態 1 線上 0 不線上  int admin;              //使用者權限,1為管理員,0為普通使用者}client_info;client_info clients[QUEUELEN];//===========客戶發送的資料結構===================================== typedef struct send_info{ int  type;      //類型 char id[25];      //對方id char buf[BUF_SIZE];  //內容 char name[25];    //使用者名稱(暱稱) char passwd[25];   //密碼 }send_info;//================函數功能的協議====================================typedef struct {     int  fun_flag; //function flag     void (*fun)();// function pointer variable}proto;//====================debug============================#define CHAT_DEBUG#ifdef  CHAT_DEBUG#define DEBUG(message...) fprintf(stderr, message)#else#define DEBUG(message...)#endif// ========fun=========client.c====================void  print_err(char *msg);void  reg_log();void  register_client();void  login();void  init_conn();void  writefun();void  show_menu();void  wait_minutes();void  signHandler(int signNo);//  ======fun=======server.c======================int   system_init();void   connect_to_client(int socketfd );void   err(char *err_msg);int    init_clents(char  *tok_file[]);void   register_new_client(send_info *send,int newfd);void   server_check_login(send_info *send,int newfd);void   client_exit(send_info *send,int newfd);int    get_sockfd(char dest[]);void   private_chat ( send_info *send,int newfd);void   public_chat (send_info *send ,int newfd);void   get_all_online_clients (send_info *send ,int newfd);void   admin_kick_client (send_info *send,int newfd);void   admin_shutup_client(send_info *send,int newfd);void   advanced_client_to_admin (send_info *send,int newfd);void   drop_client_to_normal (send_info *send,int newfd) ;int    admin_is_opt_self(send_info *send,int newfd);int    admin_opt_self(send_info *send,int newfd);#endif 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.