Android - server socket

I have few questions regarding Socket Communication in Android.

1) I developed a socket app for Android that listens on port 8888.

a) When I host my server on the emulator, I cannot contact it through the client application that I have on my computer, since both applications (the emulator and the client) are on my laptop and on the same network. I think they should be able to communicate with each other.

b) When I deploy the same server application on my Android mobile device and try to transfer it through the same client application that I have on my PC, the client application gives a timeout exception because it cannot communicate with him.

My first question is: how can I test the application to connect to the server / client using an emulator and 1 Android device? Can I even use the PC client application to check my server socket?

** I have a Socket application for my other application, so there is no problem with the client application.

2) My second question is to check my server application on an Android device, do I need to forward the correct port? a) For emulator: how can I redirect a port? b) For a device: how can I redirect a port? c) Can I forward a port programmatically?

** For information only:

I use Eclipse as an Android development tool.

** MY Server Code, as there may be a problem with my server code.

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();
                    }
                }
            }
        }

: , 8080 PC Client Socket, Android- Android, , . = > socket = serversocket.accept();

.


+5
2

Manifest.xml?

<uses-permission android:name="android.permission.INTERNET" />

+9

All Articles