首页 > 代码库 > C++ 简易scoket Server

C++ 简易scoket Server

//
//  main.cpp
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>


#define MAXUSERS 5
#define BUFFER_SIZE 64
#define FD_LIMIT 65535

int USERS[MAXUSERS];
int user_count = 0;


void showclient()
{
    int i;
    printf("user_count: %d\n", user_count);
    for (i = 0; i < MAXUSERS; i++) {
        printf("[%d]:%d  ", i, USERS[i]);
    }
    printf("\n\n");
}

int main(int argc, const char * argv[])
{
    
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    
    socklen_t client_addrlength = sizeof(client_addr);
    
    
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(33333);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    //创建
    int sock_fd = socket( PF_INET, SOCK_STREAM, 0 );
    if (sock_fd<0) {
        printf("socket error:%d",errno);
        close(sock_fd);
        exit(1);
    }
    
    
    int yes = 1;
    if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }
    
    
    int sret;
    
    //绑定
    sret = bind( sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr) );
    if (sret<0) {
        printf("bind error:%d",errno);
        close(sock_fd);
        exit(2);
    }
    
    //监听
    sret = listen(sock_fd, MAXUSERS);
    if (sret<0) {
        printf("listen error:%d",errno);
        close(sock_fd);
        exit(3);
    }
    printf("start ok, listen port:%d\n", ntohs(server_addr.sin_port));
    
    char buf[1024];
    int maxsock = sock_fd;
    
    
    fd_set read_fds;        //可读
    ssize_t ret;
    
    while (1) {
        
        FD_ZERO(&read_fds);
        FD_SET(sock_fd, &read_fds);
        
        //需要监听的集合
        for (int i =0; i<user_count; i++) {
            if (USERS[i] != 0) {
                FD_SET(USERS[i], &read_fds);
            }
        }
        
        //注册监听事件
        ret = select(maxsock+1, &read_fds, NULL, NULL, NULL);
        if (ret<0) {
            printf("select error\n");
            continue;
        }else if (ret==0){
            printf("select timeout\n");
            continue;
        }
        
        //读取客户端发来的数据
        for (int i =0; i<user_count; i++) {
            if (FD_ISSET(USERS[i], &read_fds)) {
                ret = recv(USERS[i], buf, sizeof(buf), 0);
                printf("client[%d] send:%s\n", i, buf);
            }
        }
        
        //处理新用户链接
        if (FD_ISSET(sock_fd, &read_fds)) {
            printf("new client\n");
            int new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &client_addrlength);
            if (new_fd<0) {
                printf("accept error\n");
                close(new_fd);
                continue;
            }
            
            USERS[user_count] = new_fd;
            printf("new connection client[%d] %s:%d\n", user_count,
                   inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
            user_count++;
            if (new_fd > maxsock){
                maxsock = new_fd;
            }
        }
        
        showclient();
    }
    
    
    
    
    close(sock_fd);
    return 0;
}



C++ 简易scoket Server