I have been trying to get a udp client/server working so I can implement it in an Unreal Engine 5 project, but while I have no trouble connecting from my windows machine to my ubuntu server I cannot send traffic in the other direction.
I currently have a C++ program I developed from this tutorial https://www.youtube.com/watch?v=uIanSvWou1M that sends udp packets to my ubuntu server via port 53003. This works perfectly. My ubuntu server is simply running a nodejs script that listens on port 53003 and then replies to the ip address attached to the incoming udp message via port 53004
I have a second cpp program running on my windows machine that is listening on port 53004. Here's the code:
#include <iostream>
#include <WS2tcpip.h>
#pragma comment (lib, "ws2_32.lib")
using namespace std;
void main()
{
// Startup Winsock
WSADATA data;
WORD version = MAKEWORD(2, 2);
int wsOk = WSAStartup(version, &data);
if(wsOk != 0)
{
cout << "Can't start Winsock" << wsOk;
}
// Bind socket to ip address and port
SOCKET in = socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in serverHint;
serverHint.sin_addr.S_un.S_addr = ADDR_ANY;
serverHint.sin_family = AF_INET;
serverHint.sin_port = htons(53004); // Convert from little to big endian
if (bind(in, (sockaddr*)&serverHint, sizeof(serverHint)) == SOCKET_ERROR)
{
cout << "Can't bind socket! " << serverHint.sin_port << endl;
return;
}
sockaddr_in client;
int clientLength = sizeof(client);
ZeroMemory(&client, sizeof(client));
char buf[1024];
// Enter a loop
while (true)
{
ZeroMemory(buf, 1024);
// Wait for message
int bytesIn = recvfrom(in, buf, 1024, 0, (sockaddr*)&client, &clientLength);
if (bytesIn == SOCKET_ERROR)
{
cout << "Error receiving from client " << WSAGetLastError() << endl;
continue;
}
// Display message and client info
char clientIp[256];
ZeroMemory(clientIp, 256);
// make client info printable (number to pointer to string
inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);
cout << "Message received from " << clientIp << " : " << buf << endl;
}
// close socket
closesocket(in);
// shutdown winsock
WSACleanup();
}
I can alter my first program to send to ip address 127.0.0.1 on port 53004 and the above code receives/prints it with no problem. However when I try to send to it by my public ip address (instead of 127.0.0.1) it does not work.
Obviously my ubuntu program also cannot successfully send to the above program. Even using a netcat command like this does not work (my.ip.add.num is my public ip address)
echo 'test' | netcat -u my.ip.add.num 53004
I initially tried creating an inbound rule in windows firewall for port 53004 and when that didn't work I tried just completely turning windows firewall off. Yet still nothing works.
If anyone has any suggestions I would be extremely grateful!