From d9c84181b9941b4f7b52477598ec9de9c67e089e Mon Sep 17 00:00:00 2001 From: Dongos <55902586+Dongo-S@users.noreply.github.com> Date: Mon, 18 Jul 2022 19:17:38 -0500 Subject: [PATCH 1/4] Add support for custom Port number --- NetInput.Capture/NetInput.Capture.cpp | 49 ++++++++++++++++++++++----- README.md | 9 ++--- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/NetInput.Capture/NetInput.Capture.cpp b/NetInput.Capture/NetInput.Capture.cpp index 50bd4f7..e889c12 100644 --- a/NetInput.Capture/NetInput.Capture.cpp +++ b/NetInput.Capture/NetInput.Capture.cpp @@ -24,7 +24,7 @@ void SendResetControllers() printf("Failed to send reset message.\n"); } -void PollControllers() +void PollControllers() { XINPUT_STATE state; for (uint32_t i = 0u; i < XUSER_MAX_COUNT; i++) @@ -49,17 +49,47 @@ void PollControllers() int main() { std::string ip; + std::int16_t port = 4313; std::ifstream input_file("target.txt"); if (input_file.is_open()) { printf("Reading ip from target.txt.\n"); - ip = std::string((std::istreambuf_iterator(input_file)), std::istreambuf_iterator()); + std::string line; + + int index = 0; + while (!input_file.eof()) { + getline(input_file, line); + + switch (index) + { + case 0: + if (inet_pton(AF_INET, line.c_str(), &(addr.sin_addr)) != 1) + { + std::cout << line << " is not a valid ip, please correct target.txt\n"; + return -1; + } + + ip = line; + break; + case 1: + try + { + port = std::stoi(line); + if (port <= 0) + port = 4313; + } + catch (...) + { + std::cout << line << " is not a valid Port number, using default value(4313) correct target.txt if your intent was to use another Port number\n"; + port = 4313; + } + break; + default: + break; + } - if (inet_pton(AF_INET, ip.c_str(), &(addr.sin_addr)) != 1) - { - std::cout << ip << " is not a valid ip, please correct target.txt\n"; - return -1; + index++; } } @@ -69,7 +99,7 @@ int main() { printf("Please enter the IP of the target computer.\n"); - std::cin >> ip; + std::cin >> ip; if (inet_pton(AF_INET, ip.c_str(), &(addr.sin_addr)) == 1) break; @@ -85,7 +115,7 @@ int main() WSADATA wsaData; int wsaStartupResult = WSAStartup(MAKEWORD(2, 2), &wsaData); - if (wsaStartupResult != 0) + if (wsaStartupResult != 0) { printf("WSAStartup failed with error code 0x%08X.\n", wsaStartupResult); return -2; @@ -98,8 +128,9 @@ int main() return -3; } + printf("Port is %d.\n", port); addr.sin_family = AF_INET; - addr.sin_port = htons(4313); + addr.sin_port = htons(port); printf("Sending reset...\n"); SendResetControllers(); diff --git a/README.md b/README.md index 7d832ee..cb1af6d 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,12 @@ NetInput is an easy way to send gamepad input over the network with low latency. It supports up to 4 gamepads at a time. The program assumes a single source and a single receiver. Ideal in a setup where you have a display out but no way to access USB to plug in a gamepad. -The program will ask for your target computer when starting up, if you do not want to be prompted every time please create a file called "target.txt" with the target computer ip as its contents. +The program will ask for your target computer when starting up, if you do not want to be prompted every time please create a file called "target.txt" with the target computer ip as its contents and the port number. For example: ``` 192.168.1.31 +4313 ``` ## Local Setup Guide @@ -14,7 +15,7 @@ For example: 1. Install the requirements 2. Run the Player on the computer you would like to send the gamepad input to 3. Figure out the IP address of the computer running the player (open up terminal and type ipconfig) -4. Create a file called target.txt in the same folder as the Capture program on the computer you would like to capture the gamepad input from and insert add the IP of the computer running the Player in that file +4. Create a file called target.txt in the same folder as the Capture program on the computer you would like to capture the gamepad input from and insert add the IP of the computer running the Player in that file and the Port number 5. Run the Capture program on the computer you would like to game the gamepad input from 6. Enjoy! @@ -22,8 +23,8 @@ For example: 1. Install the requirements 2. Run the Player on the computer you would like to send the gamepad input to -3. Figure out the remote IP address of the computer running the player (https://www.whatismyip.com/) and forward the required port (UDP 4313) on your router -4. Create a file called target.txt in the same folder as the Capture program on the computer you would like to capture the gamepad input from and insert add the IP of the computer running the Player in that file +3. Figure out the remote IP address of the computer running the player (https://www.whatismyip.com/) and forward the required port (UDP default:4313) on your router +4. Create a file called target.txt in the same folder as the Capture program on the computer you would like to capture the gamepad input from and insert add the IP of the computer running the Player in that file and the Port number 5. Run the Capture program on the computer you would like to game the gamepad input from 6. Enjoy! From ba94f44b8444f56debfd6952f7bc70ec8a5fb08a Mon Sep 17 00:00:00 2001 From: Dongos <55902586+Dongo-S@users.noreply.github.com> Date: Tue, 19 Jul 2022 11:42:00 -0500 Subject: [PATCH 2/4] Custom port in NetInput.Player --- NetInput.Player/NetInput.Player.cpp | 42 +++++++++++++++++++++++++++-- README.md | 4 +-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/NetInput.Player/NetInput.Player.cpp b/NetInput.Player/NetInput.Player.cpp index 923a3a9..6e320c7 100644 --- a/NetInput.Player/NetInput.Player.cpp +++ b/NetInput.Player/NetInput.Player.cpp @@ -8,6 +8,9 @@ #include #include +#include +#include + SOCKET sock; PVIGEM_TARGET pads[XUSER_MAX_COUNT]; @@ -30,10 +33,45 @@ void ResetGamepads() int main() { + int port = 4313; + CoInitialize(NULL); printf("Starting networking...\n"); + + std::ifstream input_file("target.txt"); + if (input_file.is_open()) + { + printf("Reading Port from target.txt.\n"); + std::string line; + + int index = 0; + while (!input_file.eof()) { + getline(input_file, line); + + switch (index) + { + case 1: + try + { + port = std::stoi(line); + if (port <= 0) + port = 4313; + } + catch (...) + { + std::cout << line << " is not a valid Port number, using default value(4313) correct target.txt if your intent was to use another Port number\n"; + port = 4313; + } + break; + default: + break; + } + + index++; + } + } WSADATA wsaData; int wsaStartupResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (wsaStartupResult != 0) @@ -51,12 +89,12 @@ int main() struct sockaddr_in addr; addr.sin_family = AF_INET; - addr.sin_port = htons(4313); + addr.sin_port = htons(port); inet_pton(AF_INET, "0.0.0.0", &(addr.sin_addr)); if (bind(sock, (const sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { - printf("Failed to bind to 0.0.0.0:4313."); + printf("Failed to bind to 0.0.0.0:%d.", port); closesocket(sock); WSACleanup(); return -3; diff --git a/README.md b/README.md index cb1af6d..df1d4b5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ For example: ## Local Setup Guide 1. Install the requirements -2. Run the Player on the computer you would like to send the gamepad input to +2. Run the Player on the computer you would like to send the gamepad input to, if you want a custom Port number, create the "target.txt" and define the port in the second line 3. Figure out the IP address of the computer running the player (open up terminal and type ipconfig) 4. Create a file called target.txt in the same folder as the Capture program on the computer you would like to capture the gamepad input from and insert add the IP of the computer running the Player in that file and the Port number 5. Run the Capture program on the computer you would like to game the gamepad input from @@ -22,7 +22,7 @@ For example: ## Over The Internet Setup Guide 1. Install the requirements -2. Run the Player on the computer you would like to send the gamepad input to +2. Run the Player on the computer you would like to send the gamepad input to, if you want a custom Port number, create the "target.txt" and define the port in the second line 3. Figure out the remote IP address of the computer running the player (https://www.whatismyip.com/) and forward the required port (UDP default:4313) on your router 4. Create a file called target.txt in the same folder as the Capture program on the computer you would like to capture the gamepad input from and insert add the IP of the computer running the Player in that file and the Port number 5. Run the Capture program on the computer you would like to game the gamepad input from From 5acaeb291ee5583b0171e54b688417a695f3d90b Mon Sep 17 00:00:00 2001 From: Dongos <55902586+Dongo-S@users.noreply.github.com> Date: Tue, 19 Jul 2022 19:26:46 -0500 Subject: [PATCH 3/4] Player: message port being used --- NetInput.Player/NetInput.Player.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/NetInput.Player/NetInput.Player.cpp b/NetInput.Player/NetInput.Player.cpp index 6e320c7..7d7ffaf 100644 --- a/NetInput.Player/NetInput.Player.cpp +++ b/NetInput.Player/NetInput.Player.cpp @@ -87,6 +87,7 @@ int main() return -2; } + printf("Port is %d.\n", port); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(port); From a77b806ea0056bf1af752fe574637701089adc89 Mon Sep 17 00:00:00 2001 From: Dongos <55902586+Dongo-S@users.noreply.github.com> Date: Mon, 25 Jul 2022 16:56:53 -0500 Subject: [PATCH 4/4] Refactor custom port --- NetInput.Player/NetInput.Player.cpp | 77 +++++++++++++---------------- README.md | 13 +++-- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/NetInput.Player/NetInput.Player.cpp b/NetInput.Player/NetInput.Player.cpp index 7d7ffaf..e862736 100644 --- a/NetInput.Player/NetInput.Player.cpp +++ b/NetInput.Player/NetInput.Player.cpp @@ -8,9 +8,9 @@ #include #include -#include #include - +#include +#include SOCKET sock; PVIGEM_TARGET pads[XUSER_MAX_COUNT]; @@ -33,45 +33,10 @@ void ResetGamepads() int main() { - int port = 4313; - CoInitialize(NULL); printf("Starting networking...\n"); - - std::ifstream input_file("target.txt"); - if (input_file.is_open()) - { - printf("Reading Port from target.txt.\n"); - std::string line; - - int index = 0; - while (!input_file.eof()) { - getline(input_file, line); - - switch (index) - { - case 1: - try - { - port = std::stoi(line); - if (port <= 0) - port = 4313; - } - catch (...) - { - std::cout << line << " is not a valid Port number, using default value(4313) correct target.txt if your intent was to use another Port number\n"; - port = 4313; - } - break; - default: - break; - } - - index++; - } - } WSADATA wsaData; int wsaStartupResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (wsaStartupResult != 0) @@ -87,19 +52,45 @@ int main() return -2; } - printf("Port is %d.\n", port); + //Read target port from file + int targetPort = 4313; + std::ifstream input_file("port.txt"); + if (input_file.is_open()) { + printf("Reading port from port.txt\n"); + std::string line; + getline(input_file, line); + try + { + targetPort = std::stoi(line); + if (targetPort <= 0) + targetPort = 4313; + } + catch (...) + { + std::cout << line << " is not a valid Port number, using default value(4313) correct port.txt if your intent was to use another Port number\n"; + targetPort = 4313; + } + } + + //Bind socket to port. struct sockaddr_in addr; addr.sin_family = AF_INET; - addr.sin_port = htons(port); + addr.sin_port = htons(targetPort); inet_pton(AF_INET, "0.0.0.0", &(addr.sin_addr)); - if (bind(sock, (const sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) + int bindResult = bind(sock, (const sockaddr*)&addr, sizeof(addr)); + + if (bindResult == SOCKET_ERROR) { - printf("Failed to bind to 0.0.0.0:%d.", port); + printf("Failed to bind to 0.0.0.0:%d\n", targetPort); closesocket(sock); WSACleanup(); return -3; } + else + { + printf("Binded to 0.0.0.0:%d\n", targetPort); + } printf("Done.\n"); @@ -126,6 +117,8 @@ int main() printf("Done.\n"); + printf("Press ESC to exit at any moment...\n"); + printf("Waiting for data...\n"); struct sockaddr_in clientAddr; @@ -174,7 +167,7 @@ int main() ResetGamepads(); vigem_disconnect(client); vigem_free(client); - closesocket(sock); + int closeSockedResult = closesocket(sock); WSACleanup(); CoUninitialize(); return 0; diff --git a/README.md b/README.md index df1d4b5..6851e7e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ NetInput is an easy way to send gamepad input over the network with low latency. It supports up to 4 gamepads at a time. The program assumes a single source and a single receiver. Ideal in a setup where you have a display out but no way to access USB to plug in a gamepad. -The program will ask for your target computer when starting up, if you do not want to be prompted every time please create a file called "target.txt" with the target computer ip as its contents and the port number. +The Capture program will ask for your target computer when starting up, if you do not want to be prompted every time please create a file called "target.txt" with the target computer ip and the port number as its contents. For example: ``` @@ -10,10 +10,17 @@ For example: 4313 ``` +For the Player, to select a custom port is neccesary to create a file called "port.txt" with the port number. +For example: +``` +4313 +``` + + ## Local Setup Guide 1. Install the requirements -2. Run the Player on the computer you would like to send the gamepad input to, if you want a custom Port number, create the "target.txt" and define the port in the second line +2. Run the Player on the computer you would like to send the gamepad input to, if you want a custom Port number, create the "port.txt" and define the port. 3. Figure out the IP address of the computer running the player (open up terminal and type ipconfig) 4. Create a file called target.txt in the same folder as the Capture program on the computer you would like to capture the gamepad input from and insert add the IP of the computer running the Player in that file and the Port number 5. Run the Capture program on the computer you would like to game the gamepad input from @@ -22,7 +29,7 @@ For example: ## Over The Internet Setup Guide 1. Install the requirements -2. Run the Player on the computer you would like to send the gamepad input to, if you want a custom Port number, create the "target.txt" and define the port in the second line +2. Run the Player on the computer you would like to send the gamepad input to, if you want a custom Port number, create the "port.txt" and define the port. 3. Figure out the remote IP address of the computer running the player (https://www.whatismyip.com/) and forward the required port (UDP default:4313) on your router 4. Create a file called target.txt in the same folder as the Capture program on the computer you would like to capture the gamepad input from and insert add the IP of the computer running the Player in that file and the Port number 5. Run the Capture program on the computer you would like to game the gamepad input from