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/NetInput.Player/NetInput.Player.cpp b/NetInput.Player/NetInput.Player.cpp index 923a3a9..e862736 100644 --- a/NetInput.Player/NetInput.Player.cpp +++ b/NetInput.Player/NetInput.Player.cpp @@ -8,6 +8,9 @@ #include #include +#include +#include +#include SOCKET sock; PVIGEM_TARGET pads[XUSER_MAX_COUNT]; @@ -49,18 +52,45 @@ int main() return -2; } + //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(4313); + 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:4313."); + 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"); @@ -87,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; @@ -135,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 7d832ee..6851e7e 100644 --- a/README.md +++ b/README.md @@ -2,28 +2,36 @@ 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 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: ``` 192.168.1.31 +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 +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 +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! ## 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 -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 +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 6. Enjoy!