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:
Yu-Chen Lin 2019-05-31 22:15:31 +08:00 committed by Romain Vimont
parent fcf225049d
commit a56045dd80

View file

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