From 011c463ea9e6683b84365412267ef863c3c9852d Mon Sep 17 00:00:00 2001 From: Tony Yang Date: Tue, 28 Dec 2021 01:46:48 +0800 Subject: [PATCH] [Test] still not working sad --- 109062273_cli.cpp | 20 ++++++++++++-------- 109062273_ser.cpp | 10 +++++----- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/109062273_cli.cpp b/109062273_cli.cpp index 20c5a16..3d19318 100644 --- a/109062273_cli.cpp +++ b/109062273_cli.cpp @@ -22,15 +22,15 @@ using namespace std; int sockfd = 0; void clean_up(int signum) { - int truthy = 1; if (sockfd == -1) exit(1); + int truthy = 1; + setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &truthy, sizeof(int)); + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &truthy, sizeof(int)); + shutdown(sockfd, SHUT_RDWR); close(sockfd); - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &truthy, sizeof(int)); - setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &truthy, sizeof(int)); - exit(0); } @@ -55,6 +55,8 @@ int main(int argc, char const *argv[]) { sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { cerr << "Failed to create socket." << endl; + clean_up(0); + exit(errno); } // setup server info @@ -66,8 +68,8 @@ int main(int argc, char const *argv[]) { // use static client port if (bind(sockfd, (struct sockaddr *) &client_info, sizeof(client_info)) == -1) { cerr << "socket bind error" << endl; - close(sockfd); - return BINDERR_RET; + // close(sockfd); + exit(errno); } // setup server info @@ -78,7 +80,7 @@ int main(int argc, char const *argv[]) { if (connect(sockfd, (struct sockaddr *) &info, sizeof(info)) == -1) { cerr << "Connection failed." << endl; - close(sockfd); + // close(sockfd); exit(errno); } @@ -101,7 +103,8 @@ int main(int argc, char const *argv[]) { if (send(sockfd, msg, sizeof(msg), 0) == -1) { cerr << "Send error" << endl; - break; + clean_up(0); + exit(errno); }; // receive server response @@ -140,5 +143,6 @@ int main(int argc, char const *argv[]) { cout << endl; } + close(sockfd); return 0; } \ No newline at end of file diff --git a/109062273_ser.cpp b/109062273_ser.cpp index abf6e14..9e87e9e 100644 --- a/109062273_ser.cpp +++ b/109062273_ser.cpp @@ -14,8 +14,6 @@ #define BUFFER_SIZE 256 #define NOPORT_RET 10 #define INVALID_PORT_RET 20 -#define BINDERR_RET 30 -#define LISTENERR_RET 35 using namespace std; @@ -99,6 +97,7 @@ int main(int argc, char const *argv[]) { sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { cerr << "Failed to create socket" << endl; + exit(errno); } // setup server info @@ -111,13 +110,14 @@ int main(int argc, char const *argv[]) { if (bind(sockfd, (struct sockaddr *) &server_info, sizeof(server_info)) == -1) { cerr << "Cannot bind socker." << endl; close(sockfd); - return BINDERR_RET; + exit(errno); } // only one connection at the same time if (listen(sockfd, 1) == -1) { cerr << "Cannot listen to socket." << endl; - return LISTENERR_RET; + close(sockfd); + exit(errno); } cout << "Server up. Waiting connection..." << endl; @@ -152,7 +152,7 @@ int main(int argc, char const *argv[]) { if (recv(client_sockfd, buffer, sizeof(buffer), 0) <= 0) { // closed connection cout << "Disconnected." << endl; - cout << shutdown(client_sockfd, SHUT_RDWR) << endl; + shutdown(client_sockfd, SHUT_RDWR); close(client_sockfd); break; }