C++高并发服务器设计(一)

*简易TCP客户端与服务端
简易TCP服务器代码

功能:建立简易的TCP服务器,监听各个客户端,为了应对多个连接,并保证它们之间相互独立,加入多进程fork()。让子进程接收数据,父进程继续监听新的连接,接收数据存入recv_buf。

#include 
#include 
#include       
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define PORT_NUMBER 8888
#define BACKLOG     10
/* socket->bind->listen->accept->send/recv->close*/
int main(int argc, char** argv)
{
     
    int sock_fd, new_fd;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    int ret;
    socklen_t addr_len;
    int recv_len;
    unsigned char recv_buf[1000];
    int client_num = -1;
    signal(SIGCHLD, SIG_IGN);
    /* socket */
    sock_fd = socket(AF_INET, SOCK_STREAM, 0);//AF_INET:IPV4;SOCK_STREAM:TCP
    if (-1 == sock_fd)
    {
     
        fprintf(stderr, "socket error:%s\n\a", strerror(errno));
        exit(1);
    }
    /* set server sockaddr_in */
    memset(&server_addr, 0, sizeof(struct sockaddr_in));//clear
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//INADDR_ANY:This machine all IP
    server_addr.sin_port = htons(PORT_NUMBER);
    
    /* bind */
    ret = bind(sock_fd, (struct sockaddr*)(&server_addr), sizeof(struct sockaddr));
    if (-1 == ret)
    {
     
        fprintf(stderr, "bind error:%s\n\a", strerror(errno));
        close(sock_fd);
        exit(1);
    }
    /* listen */
    ret = listen(sock_fd, BACKLOG);
    if (-1 == ret)
    {
     
        fprintf(stderr, "listen error:%s\n\a", strerror(errno));
        close(sock_fd);
        exit(1);
    }
    /* accept */
    while (1)
    {
     
        addr_len = sizeof(struct sockaddr);
        new_fd = accept(sock_fd, (struct sockaddr*)&client_addr, &addr_len);
        if (-1 == new_fd)
        {
     
            fprintf(stderr, "accept error:%s\n\a", strerror(errno));
            close(sock_fd);
            exit(1);
        }
        client_num++;
        fprintf(stderr, "Server get connetion form client%d: %s\n", client_num, inet_ntoa(client_addr.sin_addr));
        if (!fork())
        {
     
            /* Child process */
            while (1)
            {
     
                /* recv */
                recv_len = recv(new_fd, recv_buf, 999, 0);
                if (recv_len <= 0)
                {
     
                    fprintf(stderr, "recv error:%s\n\a", strerror(errno));
                    close(new_fd);
                    exit(1);
                }
                else
                {
     
                    recv_buf[recv_len] = '\0';
                    printf("Get msg from client%d: %s\n", client_num, recv_buf);
                }
            }
            close(new_fd);
        }
    }
    /* close */
    close(sock_fd);
    exit(0);
}

简易TCP客户端代码

功能:建立简易的TCP客户端,可向服务器发送任意字符数据

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define PORT_NUMBER 8888
/* socket->connect->send->close*/
int main(int argc, char* argv[])
{
     
    int sock_fd;
    struct sockaddr_in server_addr;
    int ret;
     char send_buf[1000];
    int send_len;
    if (argc != 2)
    {
     
        fprintf(stderr, "Usage:%s hostname\n\a", argv[0]);
        exit(1);
    }
    /* socket */
    sock_fd = socket(AF_INET, SOCK_STREAM, 0);//AF_INET:IPV4;SOCK_STREAM:TCP
    if (-1 == sock_fd)
    {
     
        fprintf(stderr, "socket error:%s\n\a", strerror(errno));
        exit(1);
    }
    /* set sockaddr_in parameter*/
    memset(&server_addr, 0, sizeof(struct sockaddr_in));//clear
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT_NUMBER);
    ret = inet_aton(argv[1], &server_addr.sin_addr);
    if (0 == ret)
    {
     
        fprintf(stderr, "server_ip error.\n");
        close(sock_fd);
        exit(1);
    }
    /* connect */
    ret = connect(sock_fd, (const struct sockaddr*)&server_addr, sizeof(struct sockaddr));
    if (-1 == ret)
    {
     
        fprintf(stderr, "connect error:%s\n\a", strerror(errno));
        close(sock_fd);
        exit(1);
    }
    while (1)
    {
     
        if (fgets(send_buf, 999, stdin))
        {
     
            /* send */
            send_len = send(sock_fd, send_buf, strlen(send_buf), 0);
            if (send_len <= 0)
            {
     
                fprintf(stderr, "send error:%s\n\a", strerror(errno));
                close(sock_fd);
                exit(1);
            }
        }
    }
    /* close */
    close(sock_fd);
    exit(0);
}


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