1
0

[Test] bind client port, not work if client rebind port in short time

This commit is contained in:
Tony Yang 2021-12-28 01:06:15 +08:00
parent 0cd8003070
commit 97dae6fe69
Signed by: t510599
GPG Key ID: D88388851C28715D
2 changed files with 48 additions and 4 deletions

View File

@ -4,6 +4,8 @@
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
@ -12,11 +14,28 @@
#define BUFFER_SIZE 256
#define NOSERVER_RET 10
#define INVALID_PORT_RET 20
#define BINDERR_RET 25
#define CONN_FAILED_RET 30
using namespace std;
int sockfd = 0;
void clean_up(int signum) {
int truthy = 1;
if (sockfd == -1) exit(1);
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);
}
int main(int argc, char const *argv[]) {
signal(SIGINT, clean_up);
// no server info specified
if (argc < 2) {
cerr << "No server info provided!" << endl << "Usage: client <ip> <port>" << endl;
@ -29,8 +48,8 @@ int main(int argc, char const *argv[]) {
return INVALID_PORT_RET;
}
int sockfd = 0;
struct sockaddr_in info;
struct sockaddr_in client_info;
// create tcp socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
@ -38,6 +57,19 @@ int main(int argc, char const *argv[]) {
cerr << "Failed to create socket." << endl;
}
// setup server info
bzero(&client_info, sizeof(client_info));
client_info.sin_family = PF_INET;
client_info.sin_addr.s_addr = INADDR_ANY;
client_info.sin_port = htons(48763);
// 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;
}
// setup server info
bzero(&info, sizeof(info));
info.sin_family = PF_INET;
@ -47,7 +79,7 @@ int main(int argc, char const *argv[]) {
if (connect(sockfd, (struct sockaddr *) &info, sizeof(info)) == -1) {
cerr << "Connection failed." << endl;
close(sockfd);
return CONN_FAILED_RET;
exit(errno);
}
cout << "--------------" << endl;
@ -108,6 +140,5 @@ int main(int argc, char const *argv[]) {
cout << endl;
}
close(sockfd);
return 0;
}

View File

@ -5,6 +5,7 @@
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
@ -18,6 +19,8 @@
using namespace std;
int sockfd;
class Game {
private:
int upper;
@ -63,7 +66,16 @@ class Game {
}
};
void clean_up(int signum) {
if (sockfd == -1) exit(1);
close(sockfd);
exit(0);
}
int main(int argc, char const *argv[]) {
signal(SIGINT, clean_up);
// initialize game
srand(time(nullptr));
Game game;
@ -140,6 +152,8 @@ 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;
close(client_sockfd);
break;
}
@ -175,6 +189,5 @@ int main(int argc, char const *argv[]) {
}
}
close(sockfd);
return 0;
}