[Test] bind client port, not work if client rebind port in short time
This commit is contained in:
parent
0cd8003070
commit
97dae6fe69
@ -4,6 +4,8 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <errno.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>
|
||||||
@ -12,11 +14,28 @@
|
|||||||
#define BUFFER_SIZE 256
|
#define BUFFER_SIZE 256
|
||||||
#define NOSERVER_RET 10
|
#define NOSERVER_RET 10
|
||||||
#define INVALID_PORT_RET 20
|
#define INVALID_PORT_RET 20
|
||||||
|
#define BINDERR_RET 25
|
||||||
#define CONN_FAILED_RET 30
|
#define CONN_FAILED_RET 30
|
||||||
|
|
||||||
using namespace std;
|
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[]) {
|
int main(int argc, char const *argv[]) {
|
||||||
|
signal(SIGINT, clean_up);
|
||||||
// no server info specified
|
// no server info specified
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
cerr << "No server info provided!" << endl << "Usage: client <ip> <port>" << endl;
|
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;
|
return INVALID_PORT_RET;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sockfd = 0;
|
|
||||||
struct sockaddr_in info;
|
struct sockaddr_in info;
|
||||||
|
struct sockaddr_in client_info;
|
||||||
|
|
||||||
// create tcp socket
|
// create tcp socket
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
@ -38,6 +57,19 @@ int main(int argc, char const *argv[]) {
|
|||||||
cerr << "Failed to create socket." << endl;
|
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
|
// setup server info
|
||||||
bzero(&info, sizeof(info));
|
bzero(&info, sizeof(info));
|
||||||
info.sin_family = PF_INET;
|
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) {
|
if (connect(sockfd, (struct sockaddr *) &info, sizeof(info)) == -1) {
|
||||||
cerr << "Connection failed." << endl;
|
cerr << "Connection failed." << endl;
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
return CONN_FAILED_RET;
|
exit(errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "--------------" << endl;
|
cout << "--------------" << endl;
|
||||||
@ -108,6 +140,5 @@ int main(int argc, char const *argv[]) {
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sockfd);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -5,6 +5,7 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <signal.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>
|
||||||
@ -18,6 +19,8 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
int sockfd;
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
private:
|
private:
|
||||||
int upper;
|
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[]) {
|
int main(int argc, char const *argv[]) {
|
||||||
|
signal(SIGINT, clean_up);
|
||||||
|
|
||||||
// initialize game
|
// initialize game
|
||||||
srand(time(nullptr));
|
srand(time(nullptr));
|
||||||
Game game;
|
Game game;
|
||||||
@ -140,6 +152,8 @@ int main(int argc, char const *argv[]) {
|
|||||||
if (recv(client_sockfd, buffer, sizeof(buffer), 0) <= 0) {
|
if (recv(client_sockfd, buffer, sizeof(buffer), 0) <= 0) {
|
||||||
// closed connection
|
// closed connection
|
||||||
cout << "Disconnected." << endl;
|
cout << "Disconnected." << endl;
|
||||||
|
cout << shutdown(client_sockfd, SHUT_RDWR) << endl;
|
||||||
|
close(client_sockfd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,6 +189,5 @@ int main(int argc, char const *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sockfd);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user