Prevent socket leak on error
Signed-off-by: Yu-Chen Lin <npes87184@gmail.com> Signed-off-by: Romain Vimont <rom@rom1v.com>
This commit is contained in:
parent
fcf225049d
commit
a56045dd80
1 changed files with 3 additions and 0 deletions
|
@ -33,6 +33,7 @@ net_connect(uint32_t addr, uint16_t port) {
|
||||||
|
|
||||||
if (connect(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
if (connect(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||||
perror("connect");
|
perror("connect");
|
||||||
|
close(sock);
|
||||||
return INVALID_SOCKET;
|
return INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,11 +61,13 @@ net_listen(uint32_t addr, uint16_t port, int backlog) {
|
||||||
|
|
||||||
if (bind(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
if (bind(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||||
perror("bind");
|
perror("bind");
|
||||||
|
close(sock);
|
||||||
return INVALID_SOCKET;
|
return INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(sock, backlog) == SOCKET_ERROR) {
|
if (listen(sock, backlog) == SOCKET_ERROR) {
|
||||||
perror("listen");
|
perror("listen");
|
||||||
|
close(sock);
|
||||||
return INVALID_SOCKET;
|
return INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue