I have few question regarding Socket Communication in Android.
1)I have developed server socket app for android that listens to port 8888.
a) When i host my server on the emulator I'm unable to communicate to it through Client application that I have on my PC since both (Emulator & Client) app are on my laptop & on the same network i think that they should be able to communicate with each other.
b) When i deploy the same server app on my android mobile device and try to communicate it through the same Client Application that I have on my PC, the client application gives a timeout exception as its unable to communicate to it.
My first question is How can i test server/client socket app with Emulator & 1 android device? Can i even use my PC's client socket application to test my server socket?
**I have Client Socket Application for my other application so theres no problem with the client application.
2) My second question is to test my server app on the android device do I have to forward the desired port? a) For Emulator: How can I forward port? b) For Device: How can i forward port? c) Can i forward port programmatically?
**Just for Information:
I'm using Eclipse as android developement tool.
** MY Server Code as there can also be problem with my server socket code too.
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(SERVERPORT);
System.out.println("Listening :" + SERVERPORT);
System.out.println("Server IP:" + SERVERIP);
}
catch (Exception e)
{
e.printStackTrace();
}
while(true)
{
try
{
socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("ip: " + socket.getInetAddress());
String str = in.readLine();
System.out.println("message Received: " + str);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if( socket!= null)
{
try
{
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if( dataInputStream!= null)
{
try
{
dataInputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if( dataOutputStream!= null)
{
try
{
dataOutputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Edited: A very interesting part is that if i set port to 8080 PC's Client Socket App do connect to Android App on my android device but i don't receive socket on my server nor the data I send. Nothing happens after the link => socket = serversocket.accept();
Also I have set the permission in the manifest.
For your first question, on how to access the network from the emulator: the emulator runs on its own network address space, isolated from your PC. You have to configure network redirection to access devices on your network.
See more details in https://developer.android.com/studio/run/emulator-networking.html
Ok that worked for me, Now only question left is how can i run this same server on my android device?
Not sure I followed the question. Do you mean you scenario 1b above? If so, it should work fine, assuming the PC is trying to connect via the port exposed to the network (the one configured for forwarding).
yes only question left unanswered is regarding android device. When i forward the port on Emulator it can communicate with the PC's Socket device but I do know how to make that PC's client application communicate with Android app deployed on the android device.
Make sure you are using the redirected port on the client application. Beyond that, there is no big secret. You can also try two other things: 1) test your client/server application entirely on the PC (run the Java code on the PC - not on Android) to make sure it's not a coding problem unrelated to Android and 2) run Wireshark (google and download) to see where exactly the connection fails (e.g. the client application sends a TCP SYN, but get no ACK or there isn't even a SYN from the application).
@Seth: Fixed the link, pointing to the new page. Thanks for letting me know. I understand the risk with links. However, the instructions and nuances around them aren't that simple. I'd have to replicate the entire page here to make it useful. The link will have to do in this case.