Linux function Linux chat program socket-based TCP (connected) chat program

Source: Internet
Author: User

/* Only pay the header file here. If necessary, download the file from my resources.

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

O (∩) O Thank you

Currently, the following functions are available:

1. group chat

2. Private Chat

3. login verification

4. Registration

5. used online users

6. Exit

7. The Administrator kicks out a user.

8. The Administrator prohibits a user from chatting.

9. The administrator promotes a user as the administrator.

10. The Administrator reduced a user to a common user.

*/

/* Author QQ: 893115871 author Email: gfj19900401@163.com header file, function declaration and macro definition */# 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 // group chat # define private_chat 2 // private chat # define client_login 3 // login verification # define client_register 4 // registration # define client_online 5 // used online User # define client_exit 6 // exit # define admin_kick_client 7 // The Administrator kicks out a user # define admin_shutup_client 8 // The Administrator disallows a user to chat # define advanced_client_to_admin 9 // The administrator promotes a user for the Administrator # define drop_client_to_normal 10 // The Administrator drops a user to a common user // ================== ========================================================== ====## Define Login 0 // normal user logon failed # define normal_user_login_success 1 // normal user logon successful # Define Login 2 // normal user already online # define admin_login_success 3/ /Administrator Logon successful # define normal_user_login_passwd_error 4 // normal user logon Password error # define regist_falied 0 // registration failed # define regist_exited 1 // the registered user already exists # define normal_user 0 // ordinary user # define admin_user 1 // administrator // ============= client ATTR ============ ====================================== typedef struct {pthread_t tid; // The thread descriptor int sockfd; // The new socket descriptor char client_name [25] of the client returned by accept; // The account char client_passwd [25]; // The password int client_id; // user ID int is_online; // online status 1 online 0 offline int admin; // user permission, 1 as administrator, 0 as common user} client_info; client_info clients [queuelen]; // =============== Data Structure sent by the customer =================================== ================== typedef struct send_info {int type; // type char ID [25]; // ID of the other party char Buf [buf_size]; // content char name [25]; // User Name (nickname) Char passwd [25]; // password} send_info; // ======================== function Protocol ======================== 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 Merge (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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.