基于套接字实现的聊天室

在这里插入代码片
#include 
#include 
#include 
#include 
#include 
#include  
#include 
#include 

#define port 3000 // 端口号

#define Cli_backlog 10 // 支持的最大用户人数

char *user_name[Cli_backlog]; // 用户名字

int new_sockfd[Cli_backlog+1]; // 每个用户的sockfd
    
char buff[4096] = {}; // 收发空间
    
int User_Count = 0; // 当前用户数量统计 

typedef struct Inc_thd
{
        char username[20];
        int newsockfd;
}INC_thd;

void *serve(void *arg)
{
        INC_thd *ithdp = (INC_thd *)malloc(sizeof(INC_thd));
        memcpy(ithdp,(INC_thd *)arg,sizeof(INC_thd));
    	free(arg);

        for(;;)
        {
                // 收发某位用户的信息
                bzero(buff,4096);
                if(recv(ithdp->newsockfd, buff, 4096, 0))
                {
                        strcat(buff," :<<< ");
                        strcat(buff,ithdp->username);
                        // 显示收到信息处理后,要发送的消息
                        puts(buff);
                        // 将该用户发的消息分发给其他用户
                        for(int i=0; i<User_Count; i++)
                                if(ithdp->newsockfd != new_sockfd[i])
                                        send(new_sockfd[i], buff, strlen(buff), 0);
                }
                bzero(buff,4096);
        }
}

int main()
{
        printf("        The Server is Working !\n");

        // 创建套接字
        int sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if(-1 == sockfd)
        {
                perror("socket error");
                return 0;
        }

        struct sockaddr_in Skin = {};
        Skin.sin_family = AF_INET;
        Skin.sin_port = htons(port);
        Skin.sin_addr.s_addr = INADDR_ANY;
        socklen_t addrlen = sizeof(Skin);
        
        // 绑定套接字
        if(-1 == bind(sockfd, (struct sockaddr *)&Skin, addrlen))
        {
                perror("bind error");
                return 0;
        }

        // 监听
        if(-1 == listen(sockfd, Cli_backlog))
        {
                perror("listen error");
                return 0;
        }
	
	   for(int i=0; i<Cli_backlog; i++)
                user_name[i] = (char *)malloc(20);

        pthread_t tid[Cli_backlog] = {};

        // 等待连接
        for(;;)
        {
                if(User_Count > Cli_backlog)
                {
                        char user_full_signal[40] = "The System is being upgraded";
                        send(new_sockfd[User_Count-1], user_full_signal, strlen(user_full_signal),0);
                        User_Count--;
                }
			   else
                {
                        User_Count++;
                        struct sockaddr_in Skin_Client = {};

                        new_sockfd[User_Count-1]  = accept(sockfd, (struct sockaddr *)&Skin_Client, &addrlen);
                        if(-1 == new_sockfd[User_Count-1])
                        {
                                perror("accept error");
                        }
                        
                        recv(new_sockfd[User_Count-1], user_name[User_Count-1], 20, 0);
                        printf("Wellcome to Chat Room,%s\n",user_name[User_Count-1]);
                        printf("当前在线人数: %d\n\n",User_Count);

                        // 将当前用户名字和newsockfd传入线程
                        INC_thd* Ithdp = (INC_thd *)malloc(sizeof(INC_thd));
                        strcpy(Ithdp->username,user_name[User_Count-1]);
                        Ithdp->newsockfd = new_sockfd[User_Count-1];

                        // 为每一个用户创建一个线程,为其服务   
                        pthread_create(&tid[User_Count], NULL, serve, Ithdp);

                }

        }
}
                    

你可能感兴趣的:(项目,linux,网络通信,socket,c语言)