Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

UDP from AndroidEmulator to localhost Server(10.0.2.2) does not work?

$
0
0

Hi,

I simply try to send and receive data between MonodroidApp(AndroidEmulator) and a localDevServer. I understand localhost is specially mapped to "10.0.2.2" on AndroidEmulator, so I did the following, but the app does not respond.

        System.Text.Encoding enc = System.Text.Encoding.UTF8;
            string sendMsg = "testtest";
            byte[] sendBytes = enc.GetBytes(sendMsg);

            int localPort = 39000;
            var udp = new System.Net.Sockets.UdpClient(localPort);

            //send data
            string remoteHost = "10.0.2.2";//"127.0.0.1";
            int remotePort = 15000;
            udp.Send(sendBytes, sendBytes.Length,
                remoteHost, remotePort);

            //receive data
            System.Net.IPEndPoint remoteEP = null;
            byte[] rcvBytes = udp.Receive(ref remoteEP);
            string rcvMsg = enc.GetString(rcvBytes);
            Console.WriteLine("received data:{0}", rcvMsg);
            Console.WriteLine("sender address:{0}/port:{1}",
                remoteEP.Address, remoteEP.Port);

This code is verified to work with Mono for Mac and the localDevServer with the pointer: remoteHost = "127.0.0.1"

so,

remoteHost = "10.0.2.2" pattern does not work.

What do I miss? Anyone, any thought?

Thank you.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>