Attempting to perform sockets communication between Android and C# I seem to be stuck.
This is basically the code I have so far.
Android:
public void onClick(View v) {
try {
InetAddress serverAddr = InetAddress.getByName("localhost");
Socket mySocket = new Socket(serverAddr, 666);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
C#:
TcpListener listener = new TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), 666);
listener.Start();
using (Socket socket = listener.AcceptSocket())
{
using (Stream stream = new NetworkStream(socket))
{
}
}
When hitting the line Socket mySocket = new Socket(serverAddr, 666); in my Android application I am getting the following error.
“java.net.ConnectException:
localhost/127.0.0.1:666 – Connection
refused”
Anyone able to guide me as to what I am doing wrong?
PS: I am testing this through the emulator so wondering if there is possibly some sort of limitation about my ability to communicate with other processes running on the machine?
Inside the emulated machine the address 127.0.0.1 is not the host’s loopback interface, it’s the emulated machine’s own loopback interface.
According to the emulator documentation you can access the host’s loopback interface through the special address 10.0.2.2.