ServerSocket Android

2020-08-02 02:42发布

问题:

Hey community I have the following ServerSocket which should listen to port 53000 and log any received data. However, I cannot seem to get past the server.accept() blocking call.

public void run() {

        SocketServer server = new ServerSocket(53000);


        //---buffer store for the stream---
        byte[] buffer = new byte[1024];
        //---bytes returned from read()---
        int bytes;

        //---keep listening to the InputStream until an
        // exception occurs---
        while (true) {
            try {

                socket = server.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                String str = in.readLine();

                Log.i("received response from server", str);

                in.close();
                socket.close();

            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            } catch (Exception e){
                server.close();
                Log.e(TAG, e.getMessage());
            }
        }

    }

I have also given the application the INTERNET permission in the Manifest file. ()

To add to the mystery, I have also verified client responses get sent to that port.

Is there something in particular I need to do to make this work?

Thanks.

回答1:

Your code is very messy and won't even compile. I made some adjustments so that i could test your code, and it's working fine. Here is the test application I used:

package com.test.stackoverflow

import java.io.BufferedReader;

public class ServerSocketTestActivity extends Activity {
    /** Called when the activity is first created. */
    private static String TAG = "ServerSocketTest";

    private ServerSocket server;

    Runnable conn = new Runnable() {
        public void run() {
            try {
                server = new ServerSocket(53000);

                while (true) {
                    Socket socket = server.accept();
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(socket.getInputStream()));

                    String str = in.readLine();

                    Log.i("received response from server", str);

                    in.close();
                    socket.close();
                }
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            } catch (Exception e) {             
                Log.e(TAG, e.getMessage());
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new Thread(conn).start();
    }

    @Override
    protected void onPause() {      
        super.onPause();
        if (server != null) {
            try {
                server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }   
}

Using this code and netcat running via adb shell I was able to connect and communicate with the application.



回答2:

When working with The Client Declare these methods To access Streams

// gets the input stream // ObjectInputStream input; 
// gets the output stream // ObjectOutputStream output;
// ServerSocket server;
// Socket connection;

maybe you have a another class to access the socket;

server = new ServerSocket(5001, 100);

// step 1 create socket connection server = new ServerSocket(5001, 100); while(the condition is true)
// step 2 wait for connection

// step 3 get streams

// step 4 : process the connection

// step 5 : close connection