ServerSocket的安卓ServerSocket

由网友(指天大喊“我是爷\")分享简介:嘿社会,我有以下的ServerSocket应该侦听端口53000,并记录任何接收到的数据。不过,我似乎无法让过去的server.accept()阻塞调用。Hey community I have the following ServerSocket which should listen to port 53000...

嘿社会,我有以下的ServerSocket应该侦听端口53000,并记录任何接收到的数据。不过,我似乎无法让过去的server.accept()阻塞调用。

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

    }

我也给应用程序的清单文件中的INTERNET权限。 ()

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?

感谢。

推荐答案

您code是非常乱,甚至不会编译。我做了一些调整,这样我可以测试你的code和它的正常工作。下面是测试应用程序我使用:

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

使用这个code,并通过亚行外壳我是能够连接和通信与应用程序。netcat的运行

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

阅读全文

相关推荐

最新文章