-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from UM-Bridge/hpc-fix-port-issue
HPC: Fix port checking
- Loading branch information
Showing
4 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
all: build-load-balancer build-testmodel | ||
all: build-load-balancer build-is-port-free build-testmodel | ||
|
||
build-load-balancer: | ||
- g++ -O3 -Wno-unused-result -std=c++17 -I../lib/ LoadBalancer.cpp -o load-balancer -pthread | ||
|
||
build-is-port-free: | ||
- g++ -O3 -std=c++17 is_port_free.cpp -o is_port_free | ||
|
||
build-testmodel: | ||
- g++ -O3 -Wno-unused-result -std=c++17 -I../lib/ ../models/testmodel/minimal-server.cpp -o testmodel -pthread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "../lib/httplib.h" | ||
|
||
#include <iostream> | ||
|
||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
|
||
// Takes one integer argument - a port. | ||
// If the port is free, returns EXIT_SUCCESS; otherwise returns EXIT_FAILURE. | ||
// Note: Uses the Linux IPv4 protocol implementation! | ||
int main(int argc, char* argv[]) | ||
{ | ||
// Get port to check from command line args | ||
if (argc < 2) { | ||
std::cerr << "Missing required positional argument: port" << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
int port = std::atoi(argv[1]); | ||
|
||
// Create socket | ||
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (sockfd == -1) { | ||
std::cerr << "Failed to create socket: " << std::strerror(errno) << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// Set address to 0.0.0.0:port | ||
struct sockaddr_in addr; | ||
memset(&addr, 0, sizeof(addr)); | ||
addr.sin_family = AF_INET; | ||
addr.sin_port = htons(port); | ||
addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
|
||
// Attempt to assign address to socket | ||
int success = bind(sockfd, (struct sockaddr*) &addr, sizeof(addr)); | ||
|
||
if (success == -1) { | ||
return EXIT_FAILURE; | ||
} else { | ||
return EXIT_SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters