zoukankan      html  css  js  c++  java
  • linux 服务器与客户端异常断开连接问题

    服务器与客户端连接,客户端异常断掉之后服务器端口仍然被占用, 到最后是不是服务器端达到最大连接数就没法连接了?领导让我测试这种情况,我用自己的电脑当TCP Client,虚拟机当服务器,连接之后能正常通信,每次拔掉网线断开,5次之后就不能连接了(我的服务器设置最大连接数就是5)。具体如下:1:用模拟软件Commix模拟TCP Client,连接服务器。

    <ignore_js_op> 

    2:用服务器端监听端口1234。重复打开关闭TCP Client的端口,服务器显示如下图所示:
    <ignore_js_op> 

    1——5次可以正常连接,5次完成之后出现如下问题:
    <ignore_js_op> 
    请问怎么解决这一问题,让服务器端自动清除已断掉的连接。
    socket程序如下所示:
    #include <stdio.h>
    #include 
    <stdlib.h>
    #include 
    <unistd.h>
    #include 
    <errno.h>
    #include 
    <string.h>
    #include 
    <sys/types.h>
    #include 
    <sys/socket.h>
    #include 
    <netinet/in.h>
    #include 
    <arpa/inet.h>

    #define MYPORT 1234    // the port users will be connecting to

    #define BACKLOG 5     // how many pending connections queue will hold

    #define BUF_SIZE 200

    int fd_A[BACKLOG];    // accepted connection fd
    int conn_amount;    // current connection amount

    void showclient()
    {
        
    int i;
        printf(
    "client amount: %d ", conn_amount);
        
    for (i = 0; i < BACKLOG; i++) {
            printf(
    "[%d]:%d  ", i, fd_A);
        }
        printf(
    " ");
    }

    int main(void)
    {
        
    int sock_fd, new_fd;  // listen on sock_fd, new connection on new_fd
        struct sockaddr_in server_addr;    // server address information
        struct sockaddr_in client_addr; // connector's address information
        socklen_t sin_size;
        
    int yes = 1;
        
    char buf[BUF_SIZE];
        
    int ret;
        
    int i;

        
    if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror(
    "socket");
            exit(
    1);
        }

        
    if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
            perror(
    "setsockopt");
            exit(
    1);
        }
        
        server_addr.sin_family 
    = AF_INET;         // host byte order
        server_addr.sin_port = htons(MYPORT);     // short, network byte order
        server_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
        memset(server_addr.sin_zero, ''sizeof(server_addr.sin_zero));

        
    if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
            perror(
    "bind");
            exit(
    1);
        }

        
    if (listen(sock_fd, BACKLOG) == -1) {
            perror(
    "listen");
            exit(
    1);
        }

        printf(
    "listen port %d ", MYPORT);

        fd_set fdsr;
        
    int maxsock;
        
    struct timeval tv;

        conn_amount 
    = 0;
        sin_size 
    = sizeof(client_addr);
        maxsock 
    = sock_fd;
        
    while (1) {
            
    // initialize file descriptor set
            FD_ZERO(&fdsr);
            FD_SET(sock_fd, 
    &fdsr);

            
    // timeout setting
            tv.tv_sec = 30;
            tv.tv_usec 
    = 0;

            
    // add active connection to fd set
            for (i = 0; i < BACKLOG; i++) {
                
    if (fd_A != 0) {
                    FD_SET(fd_A
    &fdsr);
                }
            }

            ret 
    = select(maxsock + 1&fdsr, NULL, NULL, &tv);
            
    if (ret < 0) {
                perror(
    "select");
                
    break;
            } 
    else if (ret == 0) {
                printf(
    "timeout ");
                
    continue;
            }

            
    // check every fd in the set
            for (i = 0; i < conn_amount; i++) {
                
    if (FD_ISSET(fd_A&fdsr)) {
                    ret 
    = recv(fd_A, buf, sizeof(buf), 0);
                    
    if (ret <= 0) {        // client close
                        printf("client[%d] close ", i);
                        close(fd_A);
                        FD_CLR(fd_A
    &fdsr);
                        fd_A 
    = 0;
                    } 
    else {        // receive data
                        if (ret < BUF_SIZE)
                            memset(
    &buf[ret], ''1);
                        printf(
    "client[%d] send:%s ", i, buf);
                    }
                }
            }

            
    // check whether a new connection comes
            if (FD_ISSET(sock_fd, &fdsr)) {
                new_fd 
    = accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size);
                
    if (new_fd <= 0) {
                    perror(
    "accept");
                    
    continue;
                }

                
    // add to fd queue
                if (conn_amount < BACKLOG) {
                    fd_A[conn_amount
    ++= new_fd;
                    printf(
    "new connection client[%d] %s:%d ", conn_amount,
                            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
                    
    if (new_fd > maxsock)
                        maxsock 
    = new_fd;
                }
                
    else {
                    printf(
    "max connections arrive, exit ");
                    send(new_fd, 
    "bye"40);
                    close(new_fd);
                    
    break;
                }
            }
            showclient();
        }

        
    // close other connections
        for (i = 0; i < BACKLOG; i++) {
            
    if (fd_A != 0) {
                close(fd_A);
            }
        }

        exit(
    0);
    }

  • 相关阅读:
    linux的一般命令------附加
    linux(4)----------ssh config详解
    linux(3)--------SSH工具的安装使用
    linux(2)-----新装linux配置
    linux(1)------vmvear虚拟机安装linux
    (3)hadoop单节点配置
    (2)hadoop之-----配置免密码登录
    (1)hadoop之----linux配置jdk环境
    BZOJ 1037 生日聚会(神DP)
    BZOJ 1046 上升序列(LIS变形)
  • 原文地址:https://www.cnblogs.com/yuzhaokai0523/p/4341189.html
Copyright © 2011-2022 走看看