基于Linux实现的聊天室小程序

基于linux平台制作的简单聊天室程序,可以通过修改宏定义USER_LIMIT进而修改支持的用户个数

不涉及任何外部库,直接g++编译即可

涉及到:socket编程,零拷贝文件描述符数据splice,IO多路复用。

进入聊天室:

基于Linux实现的聊天室小程序_第1张图片

当有其他人进入聊天室:

基于Linux实现的聊天室小程序_第2张图片

发送信息(没加键盘对应的中断回调,不小心多按了sorry):

另一个用户接收基于Linux实现的聊天室小程序_第3张图片

当聊天室满了会提示:

基于Linux实现的聊天室小程序_第4张图片

源码 客户端程序

/***************************************************************************
聊天室客户端
功能:1.从标准输入接收数据发送到服务器
功能:2.打印从服务器收到的数据
功能:3.超出一定时间没连上就退出
by Frankie

****************************************************************************/
#include
#include
#include
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define _GNU_SOURCE 1
#define BUF_SIZE 300

int Timeout_Connect(const char* ip, int port, int time)
{
    int ret = 0;
    struct sockaddr_in address;
    bzero(&address, sizeof(address));
    address.sin_family = AF_INET;
    inet_pton(AF_INET, ip, &address.sin_addr);
    address.sin_port = htons(port);

    int sockfd = socket(PF_INET, SOCK_STREAM, 0);
    assert(sockfd >= 0);

    struct timeval timeout;
    timeout.tv_sec = time;
    timeout.tv_usec = 0;
    socklen_t len = sizeof(timeout);
    ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, len);
    assert(ret != -1);

    ret = connect(sockfd, (struct sockaddr*)&address, sizeof(address));
    if (ret == -1)
    {
        if (errno == EINPROGRESS)
        {
            printf("connecting timeout\n");
            return -1;
        }
        printf("Can't connect Server,Please try it agian.\n");
        return -1;
    }

    return sockfd;
}


int main(int argc, char* argv[])
{
    
    argc = 2;
	if (argc <= 2)
	{
		printf("Pls put in two args: 1.IP 2.Port \n");
	}

    //连接服务器
    //int port = atoi(argv[2]);
    //int sockfd = Timeout_Connect(argv[1], port,10);

    int sockfd = Timeout_Connect("192.168.154.128", 12347,10);
    //监听标准输入与服务端
    struct pollfd fds[2];
    memset(fds, 0, sizeof fds);
    fds[0].fd = 0, fds[0].events = POLLIN, fds[0].revents = 0;
    fds[1].fd = sockfd, fds[1].events = POLLIN | POLLRDHUP, fds[1].revents = 0;

    //用户读缓存
    char RDBUF[BUF_SIZE];


    int pipefd[2];
    int ret = pipe(pipefd);
    if (ret < 0)
    {
        perror("PIPE creat:");
    }


    while (1)
    {
        ret = poll(fds, 2, -1);
        if (ret < 0)
        {
            printf("Exception:Poll error\n");
            break;
        }
        if (fds[1].revents & POLLRDHUP)
        {
            printf("Server has closed your connection\n");
            break;
        }
        if (fds[1].revents & POLLIN)
        {
            memset(RDBUF, '\0', sizeof RDBUF);
            recv(sockfd, RDBUF, BUF_SIZE-1,0);
            printf("%s\n", RDBUF);
        }
        if (fds[0].revents & POLLIN)
        {
            splice(0, 0, pipefd[1], NULL, 32768, SPLICE_F_MORE);
            splice(pipefd[0], NULL, sockfd, 0, 32768, SPLICE_F_MORE);
        }

    }
    close(sockfd);


}

服务端

/***************************************************************************
聊天室服务器
功能:1.从对端接收用户数据并维护
功能:2.广播用户发送的报文
by Frankie

****************************************************************************/
#define _GNU_SOURCE 1
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define USER_LIMIT 5
#define BUFFER_SIZE 300
#define FD_LIMIT 65535

struct client_data
{
    sockaddr_in address;
    char* write_buf;
    char buf[BUFFER_SIZE];
};

int setnonblocking(int fd)
{
    int old_option = fcntl(fd, F_GETFL);
    int new_option = old_option | O_NONBLOCK;
    fcntl(fd, F_SETFL, new_option);
    return old_option;
}



int main(int argc, char* argv[])
{
    if (argc <= 2)
    {
        printf("usage: %s ip_address port_number\n", basename(argv[0]));
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi(argv[2]);

    int ret = 0;
    struct sockaddr_in address;
    bzero(&address, sizeof(address));
    address.sin_family = AF_INET;
    inet_pton(AF_INET, ip, &address.sin_addr);
    address.sin_port = htons(port);

    int listenfd = socket(PF_INET, SOCK_STREAM, 0);
    assert(listenfd >= 0);

    ret = bind(listenfd, (struct sockaddr*)&address, sizeof(address));
    assert(ret != -1);

    ret = listen(listenfd, 5);
    assert(ret != -1);




    //初始化环境
    client_data* users = new client_data[FD_LIMIT];
    pollfd fds[USER_LIMIT + 1];
    int user_counts = 0;
    fds[0].fd = listenfd, fds[0].events = POLLIN | POLLERR, fds[0].revents = 0;
    for (int i = 1;i <= USER_LIMIT;i++)
    {
        fds[i].fd = -1;
        fds[i].events = POLLIN | POLLERR;
        fds[i].revents = 0;
    }


    //事件循环
    while (1)
    {
        ret = poll(fds, user_counts + 1, -1);
        if (ret < 0)
        {
            printf("POLL FAILED!\n");
            break;
        }

        for (int i = 0;i < user_counts + 1;i++)
        {
            //监听到lfd上的连接事件
            if ((fds[i].fd == listenfd) && (fds[i].revents & POLLIN))
            {
                struct sockaddr_in client_address;
                socklen_t client_addrlength = sizeof(client_address);
                int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
                if (connfd < 0)
                {
                    printf("errno is: %d\n", errno);
                    continue;
                }
                if (user_counts >= USER_LIMIT)
                {
                    //printf("Refuse the %d Connection\n",connfd);
                    send(connfd, "Sorry,The Person is full\n", 30, 0);
                    close(connfd);
                    continue;
                }
                user_counts++;
                users[connfd].address = client_address;
                setnonblocking(connfd);
                fds[user_counts].fd = connfd;
                fds[user_counts].events = POLLIN | POLLERR | POLLRDHUP;
                fds[user_counts].revents = 0;
                for (int j = 1;j < user_counts;j++) { send(fds[j].fd, "Coming a new Person\n", 30, 0); } //向其他人说明
                send(connfd, "Welcome to my chatting room!\n\n\nYou can say what ever you want!\n", 80, 0);
            }

            //出现错误事件
            else if (fds[i].revents & POLLERR)
            {
                printf("get an error from %d\n", fds[i].fd);
                char errors[100];
                memset(errors, '\0', 100);
                socklen_t length = sizeof(errors);
                if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &errors, &length) < 0)
                {
                    printf("get socket option failed\n");
                }
                continue;
            }


            else if (fds[i].revents & POLLRDHUP)
            {
                users[fds[i].fd] = users[fds[user_counts].fd]; //将用户信息向前移,下个连接进来user_counts处会被覆盖
                close(fds[i].fd);
                fds[i] = fds[user_counts]; //i被删除,现在将最后面的用户数据移到这里,所以要重新在处理一次,故将i--
                i--; user_counts--;
                printf("A Client Left\n");
            }

            else if (fds[i].revents & POLLIN)
            {
                int connfd = fds[i].fd;
                memset(users[connfd].buf, '\0', BUFFER_SIZE);
                ret = recv(connfd, users[connfd].buf, BUFFER_SIZE - 1, 0);
                //printf("Get %d bytes of client data %s from %d\n", ret, users[connfd].buf, connfd);
                //读时出现错误
                if (ret < 0)
                {
                    if (errno != EAGAIN)
                    {
                        close(connfd);
                        users[fds[i].fd] = users[fds[user_counts].fd];
                        fds[i] = fds[user_counts];
                        i--;user_counts--;
                    }
                }
                else {
                    for (int j = 1;j <= user_counts;j++)
                    {
                        if (fds[j].fd == connfd) continue;
                        fds[j].events &= ~POLLIN;     //准备下次写事件,同时防止逻辑混乱,暂时~POLLIN
                        fds[j].events |= POLLOUT;
                        users[fds[j].fd].write_buf = users[connfd].buf; //要通知每个人,置指针指向发送者的缓冲区
                    }
                }

            }

            else if (fds[i].revents & POLLOUT)
            {
                int connfd = fds[i].fd;
                if (!users[connfd].write_buf)
                {
                    continue;
                }
                ret = send(connfd, users[connfd].write_buf, strlen(users[connfd].write_buf), 0);
                users[connfd].write_buf = NULL;
                fds[i].events &= ~POLLOUT;
                fds[i].events |= POLLIN;
            }

        }

    }
    delete[] users;
    close(listenfd);
    return 0;



}

对于本服务器程序,有很多改善的地方。比如我们可以开多进程或多线程将核利用起来实现负载均衡,其次,可以关注到服务器读到的数据是可以不用处理的,我们可以用与客户端同样的方式,利用splice进行零拷贝将数据发送到对端,还可以利用shmget同样可以优化性能。

源码链接: https://github.com/VerdantE1/Chatting_room

你可能感兴趣的:(linux,服务器,c++,小程序)