如何在Android中读取套接字输入流 [英] how to read socket input stream in Android

查看:172
本文介绍了如何在Android中读取套接字输入流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     can anybody tell me how to read socket input stream. Here is my code.

if (!serverIpAddress.equals(""))
        {
            try
            {
                InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                Log.i("ClientActivity", "Connecting...");
                Socket socket = new Socket(serverAddr, 5777);

                try {
                    Log.i("ClientActivity", "Sending command.");
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                            .getOutputStream())), true);
                    // where you issue the commands
                    out.println("<maplist />");
                    Log.i("ClientActivity", "Sent.");

                    input = new BufferedReader(new InputStreamReader(socket.getInputStream()));                     
                    buffer = new StringBuilder();

                    new Thread(new Runnable() {

                        @Override
                        public void run() {

                            try 
                            {
                                buffer.append(input.readLine());
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }).start();    
                    this.WriteXMLResponse(buffer.toString());

                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }

在我正在读取输入流的代码和运行方法中,发生了异常.上面写着java.net.socketException:文件编号错误.

In my code and in run method where i am reading the input Stream a exception occurs. which says java.net.socketException: bad file number.

,如果我使用while循环,我的应用程序将冻结. 任何建议都是有益的. 谢谢

and if i use while loop my app freezes. any suggestion will be appretiative. thanks

推荐答案

在看不到您的更多代码的情况下很难告诉正在发生的事情,但这是我对此处正在发生的事情的推断(我已经做了相当多的工作android应用中的套接字编程位):

It's a little bit hard to tell what's going on without seeing more of your code, but here's my deduction of what's going on here (I've done quite a bit of socket programming in android apps):

您正在创建套接字,并将输入流包装在应用程序的主线程上的BufferedReader中,然后构造一个新线程,以等待在套接字上获得输入并随后处理该输入.但是,当您创建新线程时,该代码将在后台执行,与此同时,所有这一切都在其中执行(即声明Socket和BufferedReader的方法).该方法完成后,不再有对Socket的引用,因此它已关闭并由运行时进行垃圾回收.不幸的是,您的后台线程不知道同时已关闭了套接字,因此readLine()最终抛出了一个异常,指出该套接字(fileno,unix-wise)现在是坏的",因为它已经被套接字关闭了.运行时.

You're creating the Socket and wrapping the input stream in a BufferedReader on the application's main thread and then constructing a new thread to wait until you get input on the socket and subsequently process that input. However, when you create the new thread, that code goes to execute in the background, and in the meantime the method that all this is happening in (the method where you're declaring the Socket and BufferedReader) is finishing! When that method finishes, there is no more reference to the Socket, so it's closed and garbage-collected by the runtime. Unfortunately, your background thread has no idea that it's socket has been closed in the meantime, so it's readLine() ends up throwing an exception stating that the socket (fileno, unix-wise) is now "bad" since it was closed by the runtime.

您最初认为while循环可以解决它是正确的,并且这样做是正确的,因为它不允许方法完成,从而使Socket保持打开状态,但是问题是整个UI会冻结,而您的应用正在等待来自套接字的输入.

You were somewhat right in initially assuming that a while loop might fix it, and it would, because it wouldn't allow the method to finish, thereby keeping the Socket open, but the problem is that the entire UI freezes while your app is waiting for input from the socket.

真正的解决方案是在与应用程序的UI分开的线程上运行的Service中进行 all 所有套接字通信.

The true solution to this is to do all your socket communication in a Service running on a separate thread from the UI of the application.

我尽力解释了这一点,但是如果不清楚任何部分,请发表评论,我会尝试进一步解释或为您提供更具体的帮助.

I did my best to explain this, but if any parts aren't clear, just comment, and I'll try and explain further or give you more specific help.

这篇关于如何在Android中读取套接字输入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆