Author Topic: Cant recieve UDP Packet from server host on amazon ec2  (Read 7809 times)

JosephHK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Cant recieve UDP Packet from server host on amazon ec2
« on: February 13, 2017, 02:55:24 PM »
The tcp and udp ports are both opened and I also checked TNManager.canUseUDP which returns true.

I modified the GameClient class to check whether I can recieve Udp packet:
while (keepGoing && isActive && mUdp.ReceivePacket(out buffer, out ip)) {
   Debug.Log("Received Udp Packet");
   mUdpIsUsable = true;
   keepGoing = ProcessPacket(buffer, ip);
   buffer.Recycle();
}

However, even in the Frequent Packets demo, "Received Udp Packet" is never logged when I connected to the server hosted on amazon ec2.
Instead, the received packet sent via SendQuickly is tcp.

(The udp packets can be received when the sever is hosted locally.)

Any suggestion please?

JosephHK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Cant recieve UDP Packet from server host on amazon ec2
« Reply #1 on: February 13, 2017, 03:08:09 PM »
I also checked that the ResponseSetUDP Packet can be received but the empty udp packet cannot be received :-\

JosephHK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Cant recieve UDP Packet from server host on amazon ec2
« Reply #2 on: February 14, 2017, 07:55:05 AM »
It seems that I have solved it.

I used UPnP.OpenUDP before calling TNManager.StartUDP and now the client can receive udp packet from the server

UPnP upnp = new UPnP();
int udpPort = Random.Range(10000, 50000);
upnp.OpenUDP(udpPort);
TNManager.StartUDP(udpPort);

JosephHK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Cant recieve UDP Packet from server host on amazon ec2
« Reply #3 on: February 14, 2017, 08:16:36 AM »
Unfortunately, it only works on my desktop but not on my android mobile phone. So I still need help :'(

JosephHK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Cant recieve UDP Packet from server host on amazon ec2
« Reply #4 on: February 15, 2017, 05:19:13 PM »
Looks like it is a major bug.

I am not familiar socket programming so I did an experiment.

I write a simple udp server and run it on amazon ec2, then I connect it using a simple udp client.

Server
  1. static void Main (string[] args) {
  2.         int port = 5100;
  3.         Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  4.         socket.Bind(new IPEndPoint(IPAddress.Any, port));
  5.  
  6.         EndPoint bufferEndPoint = new IPEndPoint(IPAddress.Any, 0);
  7.  
  8.         byte[] receiveBuffer = new byte[1024];
  9.         socket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref bufferEndPoint, OnReceived, socket);
  10.  
  11.         while (true);
  12. }
  13.  
  14. private static void OnReceived (IAsyncResult ar) {
  15.         int bytesReceived;
  16.         EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
  17.  
  18.         Socket socket = ar.AsyncState as Socket;
  19.  
  20.         bytesReceived = socket.EndReceiveFrom(ar, ref remoteEndPoint);
  21.  
  22.         Console.WriteLine(remoteEndPoint + ":" + bytesReceived);
  23.  
  24.         socket.SendTo(new byte[] { 1, 2, 3, 4, 5, 6, 7 }, remoteEndPoint);
  25. }

Client
  1. void Start () {
  2.         Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  3.         socket.Bind(new IPEndPoint(IPAddress.Any, 0));
  4.  
  5.         socket.Connect(new IPEndPoint(IPAddress.Parse("52.78.177.7"), 5100));
  6.         EndPoint bufferEndPoint = new IPEndPoint(IPAddress.Any, 0);
  7.  
  8.         byte[] receiveBuffer = new byte[1024];
  9.         socket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref bufferEndPoint, OnReceived, socket);
  10.  
  11.         socket.Send(new byte[] {1,2,3,4 });
  12. }
  13.  
  14. private static void OnReceived (IAsyncResult ar) {
  15.         int bytesReceived;
  16.         EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
  17.         Socket socket = ar.AsyncState as Socket;
  18.         bytesReceived = socket.EndReceiveFrom(ar, ref remoteEndPoint);
  19.  
  20.         Debug.Log(bytesReceived);
  21. }

And it works. The server and client can both receive udp message from each other.

Finally, I figured out why tnet udp protocol not works for at least me.

tnet's code
  1.                                         case Packet.RequestSetUDP: {
  2.                                                 int port = reader.ReadUInt16();
  3.                                                 if (port != 0 && mUdp.isActive && player.tcpEndPoint != null) {
  4.                                                         IPAddress ip = new IPAddress(player.tcpEndPoint.Address.GetAddressBytes());
  5.                                                         SetPlayerUdpEndPoint(player, new IPEndPoint(ip, port));
  6.                                                 } else
  7.                                                         SetPlayerUdpEndPoint(player, null);
  8.                                                        
  9.                                                 // Let the player know if we are hosting an active UDP connection
  10.                                                 ushort udp = mUdp.isActive ? (ushort)mUdp.listeningPort : (ushort)0;
  11.                                                 player.BeginSend(Packet.ResponseSetUDP).Write(udp);
  12.                                                 player.EndSend();
  13.  
  14.                                                 // Send an empty packet to the target player to open up UDP for communication
  15.                                                 if (player.udpEndPoint != null) {
  16.                                                         mUdp.SendEmptyPacket(player.udpEndPoint);
  17.                                                 }
  18.                                                 break;
  19.                                         }
  20.  

I used the remote client's IPEndPoint created internally when the udp message is received, and tnet create the IPEndPoint using IPAddress and port sent via tcp.

I roughly modified tnet's code and send RequestSetUDP packet via udp and it seems to work without using upnp.OpenUDP(udpPort).

Please confirm it and make a official fix if it is correct

JosephHK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Cant recieve UDP Packet from server host on amazon ec2
« Reply #5 on: February 15, 2017, 10:08:34 PM »
After more careful examination, It seems that it can be solved easily by changing

IPAddress ip = new IPAddress(player.tcpEndPoint.Address.GetAddressBytes());
SetPlayerUdpEndPoint(player, new IPEndPoint(ip, port));

to

SetPlayerUdpEndPoint(player, new IPEndPoint(player.tcpEndPoint.Address, port));

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Cant recieve UDP Packet from server host on amazon ec2
« Reply #6 on: February 18, 2017, 08:00:16 PM »
Hmm... I can't even remember why I had it converted to bytes there... So you're saying replacing that line makes everything work properly without any additional changes?

P.S. Back when I wrote TNet, Amazon's EC2 didn't support UDP for security reasons. I didn't even realize that it's usable now... TNet in general uses TCP for most things. UDP is only used for packets you need to send frequently with each new one replacing the state of the previous -- such as position updates in a FPS game.

JosephHK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Cant recieve UDP Packet from server host on amazon ec2
« Reply #7 on: February 20, 2017, 08:38:27 AM »
Hmm... I can't even remember why I had it converted to bytes there... So you're saying replacing that line makes everything work properly without any additional changes?

P.S. Back when I wrote TNet, Amazon's EC2 didn't support UDP for security reasons. I didn't even realize that it's usable now... TNet in general uses TCP for most things. UDP is only used for packets you need to send frequently with each new one replacing the state of the previous -- such as position updates in a FPS game.
Yes, this replacement make my desktop and android phone able to receive udp packet from ec2.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Cant recieve UDP Packet from server host on amazon ec2
« Reply #8 on: February 21, 2017, 12:57:18 PM »
Ok, thanks. I'll make a similar change and see if it causes any issues. So far it seems to work fine.