1
0

[Fix] should close socket descriptor before exit

This commit is contained in:
Tony Yang 2021-12-28 00:08:48 +08:00
parent 3b7cd9404a
commit 0cd8003070
Signed by: t510599
GPG Key ID: D88388851C28715D
2 changed files with 6 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -45,6 +46,7 @@ int main(int argc, char const *argv[]) {
if (connect(sockfd, (struct sockaddr *) &info, sizeof(info)) == -1) { if (connect(sockfd, (struct sockaddr *) &info, sizeof(info)) == -1) {
cerr << "Connection failed." << endl; cerr << "Connection failed." << endl;
close(sockfd);
return CONN_FAILED_RET; return CONN_FAILED_RET;
} }
@ -106,5 +108,6 @@ int main(int argc, char const *argv[]) {
cout << endl; cout << endl;
} }
close(sockfd);
return 0; return 0;
} }

View File

@ -4,6 +4,7 @@
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -97,6 +98,7 @@ int main(int argc, char const *argv[]) {
// bind // bind
if (bind(sockfd, (struct sockaddr *) &server_info, sizeof(server_info)) == -1) { if (bind(sockfd, (struct sockaddr *) &server_info, sizeof(server_info)) == -1) {
cerr << "Cannot bind socker." << endl; cerr << "Cannot bind socker." << endl;
close(sockfd);
return BINDERR_RET; return BINDERR_RET;
} }
@ -173,5 +175,6 @@ int main(int argc, char const *argv[]) {
} }
} }
close(sockfd);
return 0; return 0;
} }