Skip to content

Commit

Permalink
fix some resource leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mamil authored and linyacool committed Oct 23, 2019
1 parent d21a9d2 commit 1b862ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion WebBench/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ int Socket(const char *host, int clientPort)
// if(setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)) == -1)
// return -1;
if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
{
close(sock);
return -1;

}

return sock;
}

9 changes: 9 additions & 0 deletions WebServer/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ int socket_bind_listen(int port)
// 消除bind时"Address already in use"错误
int optval = 1;
if(setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1)
{
close(listen_fd);
return -1;
}

// 设置服务器IP和Port,和监听描述副绑定
struct sockaddr_in server_addr;
Expand All @@ -247,11 +250,17 @@ int socket_bind_listen(int port)
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons((unsigned short)port);
if(bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{
close(listen_fd);
return -1;
}

// 开始监听,最大等待队列长为LISTENQ
if(listen(listen_fd, 2048) == -1)
{
close(listen_fd);
return -1;
}

// 无效监听描述符
if(listen_fd == -1)
Expand Down

0 comments on commit 1b862ed

Please sign in to comment.