Android - Server Socket

2019-02-07 07:27发布

问题:

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.


回答1:

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



回答2:

have you set the right permission inside your Manifest.xml?

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

is needet to setup sockets