gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:16 25 4
gpt4 key购买 nike

<分区>

我正在学习linux网络编程。下面的程序是关于epoll电平触发和边沿触发的例子。

我让程序运行并且程序通过 telent 运行良好。唯一的问题是,当我使用 ctrl+c 停止该程序后重新启动程序时。它告诉我在进行绑定(bind)时“地址已在使用中”。

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com