C# UDPClient not receiving packets over external IP
So here's the thing, i'm creating a server/client environment for a game where a port forwarded computer acts as a server, and clients send the first UDP message to connect and get the accept message then start sending the in-game UDP messages after successfully connecting. The problem is, when I use the local IPs say the server using 10.0.0.7 and a client is using 10.0.0.4, everything works just fine:
Client sends connect message.
Server receives it.
Servers sends back accept message.
Client receives it
All further messages reach both ends without any problem.
But when I use the external IP:
Client sends connect message.
Server receives it.
Servers sends back accept message.
Client receives it
Both ends can send messages but the client doesn't receive any more messages.
Wanted to know at the end if the problem is somehow from the code, or from what I think it's really from, router, NAT, firewalls or anything like that. (Note: Client sends to server's 8888 port, and server sends back through the client's local port, as it should be.)
Below is the client code:
void Start()
{
client = new UdpClient();
client.Connect(IPAddress.Parse("<Global IP>"), 8888);
client.BeginReceive(new AsyncCallback(ReadMessage), client);
}
private void ReadMessage(IAsyncResult result)
{
IPEndPoint ipEndPoint = null;
string message = Encoding.ASCII.GetString(client.EndReceive(result, ref ipEndPoint));
print("Got: " + message);
string[] wholeMessages = message.Split('#');
for (int w = 0; w < wholeMessages.Length - 1; w++)
{
string[] parts = wholeMessages[w].Split('$');
if (!connected && parts.Length == 3 && parts[0] == "accept" && int.TryParse(parts[1], out ID))
{
connected = true;
code = parts[2];
}
}
client.BeginReceive(new AsyncCallback(ReadMessage), client);
}
public void SendUDP(string message)
{
client.Send(Encoding.ASCII.GetBytes(message), message.Length);
}
Did you find a solution to your issue?
I got an answer in the Unity Discord but didn't find the source to back it up. Nevertheless it ended up being true for me:
In the editor, Unity blocks all connections to external IPs. But when built, the game is capable of connecting to external IPs. So what I ended up doing is to write a small python script which receives external UDP messages and forwards them via the local IP. That was sufficient for development. Before building I changed the socket to the external IP/Port and the built game behaved like expected.
Hope that helps
Link to the stack overflow question with the python code.
Hey! Damn it's been 2 years since i've worked on this project. Indeed the problem occurred when I sent a UDP or TCP packet from my pc over to my external IP which my computer received according to Wireshark but silently ignored it. So apparently you can't send stuff to your device using your external IP, I never needed to cause' the program would later be ran on a server not a local pc. In the end I made the the server and client programs work over Local IP, and when I wanted to send client to my friends to test, I sent them a build that works on external IP. Sorry for not writing the solution and closing the thread, I was new to forums back then. Thanks for the reply!
Your answer
Follow this Question
Related Questions
Solved Webcamtexture 0 Answers
Unity Editor has not permission on UDP port 2 Answers
How do I use a c# library in unity? 1 Answer