已经用于连续运行我的服务器程序的地址

时间:2016-01-02 16:05:51

标签: c linux network-programming epoll

我正在学习linux网络编程。以下程序是关于epoll级别触发和边缘触发示例。

我得到程序运行,程序通过telent工作正常。 唯一的问题是,当我使用ctrl + c停止此程序后重新启动程序时。它告诉我,#34;地址已经在使用"在做绑定时。

你可以告诉我哪里错了吗?如何正确关闭文件描述符?

[vagrant@localhost epoll]$ ./a.out 0.0.0.0 8888
bind ret=Address already in use
a.out: etlt.c:190: int main(int, char**): Assertion `ret != -1' failed.
Aborted

我的节目在这里:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>


#define MAX_EVENT_NUMBER 1024
#define BUFFER_SIZE 10

static int pipefd[2];
static bool stop_server = false;

void
sig_handler(int sig)
{
    int save_errno = errno;
    int msg = sig;
    printf("sig %d\n", sig);
    send(pipefd[1], (char*)&msg, 1, 0);
    errno = save_errno;
}

void
addsig(int sig)
{
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = sig_handler;
    sa.sa_flags |= SA_RESTART;
    sigfillset(&sa.sa_mask);
    assert(sigaction(sig, &sa, NULL) != -1);
}

void
handle_sig(int fd, bool* stop_server)
{
    int sig;
    char signals[1024];
    int ret = recv(fd, signals, sizeof(signals), 0);
    if (ret == -1 || ret == 0) {
        return;
    }
    else {
        for (int i = 0; i < ret; ++i) {
            switch(signals[i]) {
                case SIGTERM:
                case SIGINT:
                    {
                        printf("SIGTERM recv\n");
                        *stop_server = true;
                    }
            }
        }
    }
}

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;
}

void
addfd(int epollfd, int fd, bool enable_et)
{
    epoll_event event;
    event.data.fd = fd;
    event.events = EPOLLIN;
    if (enable_et) {
        event.events |= EPOLLET;
    }
    epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
    setnonblocking(fd);
}

void
lt(epoll_event* events, int number, int epollfd, int listenfd)
{
    char buf[BUFFER_SIZE];
    int status = 0;
    for (int i = 0; i < number; ++i) {
        int sockfd = events[i].data.fd;
        if (sockfd == listenfd) {
            struct sockaddr_in client_address;
            socklen_t client_addrlength = sizeof(client_address);
            int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
            addfd(epollfd, connfd, false);
        }
        else if ((sockfd == pipefd[0]) && (events[i].events & EPOLLIN)) {
            handle_sig(sockfd, &stop_server);
        }
        else if (events[i].events & EPOLLIN) {
            printf("event trigger once\n");
            memset(buf, 0, BUFFER_SIZE);
            int ret = recv(sockfd, buf, BUFFER_SIZE-1, 0);
            if (ret <= 0) {
                close(sockfd);
                continue;
            }
            printf("get %d bytes of content: %s\n", ret, buf);
        }
        else {
            printf("something else happened\n");
        }
    }
}

void
et(epoll_event* events, int number, int epollfd, int listenfd)
{
    char buf[BUFFER_SIZE];
    for (int i = 0; i < number; ++i) {
        int sockfd = events[i].data.fd;
        if (sockfd == listenfd) {
            struct sockaddr_in client_address;
            socklen_t client_addrlength = sizeof(client_address);
            int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
            addfd(epollfd, connfd, true);
        }
        else if ((sockfd == pipefd[0]) && (events[i].events & EPOLLIN)) {
            printf("signal here\n");
            handle_sig(sockfd, &stop_server);
        }
        else if (events[i].events & EPOLLIN) {
            printf("event trigger once\n");
            while (1) {
                memset(buf, 0, BUFFER_SIZE);
                int ret = recv(sockfd, buf, BUFFER_SIZE-1, 0);
                if (ret < 0) {
                    if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
                        printf("read later\n");
                        break;
                    }
                    close(sockfd);
                    break;
                }
                else if (ret == 0) {
                    close(sockfd);
                }
                else {
                    printf("get %d bytes of content: %s\n", ret, buf);
                }
            }
        }
        else {
            printf("something else happened\n");
        }
    }
}




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));
    int save_errno = errno;
    printf("bind ret=%s\n", strerror(save_errno));
    assert(ret != -1);

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

    epoll_event events[MAX_EVENT_NUMBER];
    int epollfd = epoll_create(5);
    assert(epollfd != -1);

    addfd(epollfd, listenfd, true);

    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, pipefd);
    assert(ret != -1);
    setnonblocking(pipefd[1]);
    addfd(epollfd, pipefd[0], true);
    addsig(SIGTERM);
    addsig(SIGINT);

    while(!stop_server) {
        int ret = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
        if (ret < 0 && errno != EINTR) {
            int save_errno = errno;
            printf("epoll failure: %s\n", strerror(save_errno));
            break;
        }

        /*lt(events, ret, epollfd, listenfd);*/
        et(events, ret, epollfd, listenfd);
    }


    printf("closing server.\n");
    close(listenfd);
    close(pipefd[0]);
    close(pipefd[1]);
    return 0; 
}

1 个答案:

答案 0 :(得分:0)

网络端口在短时间内保持不可重用的状态,以防止错误地将数据包传送到发往旧端口的新实例。

使用SO_REUSEADDR可以立即重用端口,在调试期间非常有用,但在部署期间可能不会。