TCP套接字连接即使在Android服务中运行也会关闭

时间:2015-02-23 07:31:33

标签: android android-asynctask asyncsocket objectinputstream

我正在创建一个远程PC应用程序,其中android是客户端而Java是服务器。在下面的代码中,我从Java获取截图并将其显示在android程序的ImageView中。此处Socket连接正在Service的后台运行,我使用Socket库获取Gson对象到下面的Activity。

AndroidClient.Java

public class ScreenCast extends Activity {
MyService conn_obj;
public static Socket client;
PrintWriter printwriter;
static ImageView iv;
static Drawable ob;
Socket socket;
Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);                             setContentView(R.layout.activity_screen_cast);
    Gson obj=new Gson();
    String src=getIntent().getStringExtra("clientobj");
    conn_obj= obj.fromJson(src, MyService.class);
    client=MyService.client;
    Log.i("Client Socket",""+client);
    MyTask task=new MyTask();
    task.execute();
}

private class MyTask extends AsyncTask<Void, Void, Bitmap>{
    @SuppressWarnings("deprecation")
    @Override
    protected void onPostExecute(Bitmap result) {
        if(bitmap!=null){
            iv=(ImageView) findViewById(R.id.screenView);
            ob=new BitmapDrawable(getResources(),bitmap);
            iv.setBackgroundDrawable(ob);
        }else{
            Log.d("Bitmap null","Empty");
        }
        MyTask task=new MyTask();
        task.execute();
    }

    @Override
    protected Bitmap doInBackground(Void... arg0){
        try {
            Log.i ("Client Obj",""+client);//check result in log below.
            ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
            byte[] buffer=(byte[]) ois.readObject();
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inMutable=true;
            bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length,options);
     Ois.close ();
            return bitmap;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

我面临的问题是: Socket连接处于活动状态,直到显示第一个屏幕截图,之后Socket连接关闭,我不知道它将在何处关闭。 但奇怪的是客户端在那时并非空。 (请参阅套接字关闭错误之前的log cat)

logcat的:

2-23 12:39:27.515: I/Client Socket(24599): Socket[address=/192.168.237.1,port=3434,localPort=44496]
02-23 12:39:27.585: D/dalvikvm(24599): GC_FOR_ALLOC freed 319K, 7% free 17667K/18951K, paused 26ms, total 26ms
02-23 12:39:27.620: I/dalvikvm-heap(24599): Grow heap (frag case) to 22.136MB for 4196368-byte allocation
02-23 12:39:27.660: D/dalvikvm(24599): GC_FOR_ALLOC freed 2472K, 17% free 19293K/23111K, paused 41ms, total 41ms
02-23 12:39:27.700: D/dalvikvm(24599): GC_CONCURRENT freed 0K, 17% free 19293K/23111K, paused 12ms+5ms, total 40ms
02-23 12:39:27.815: E/SpannableStringBuilder(24599): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
02-23 12:39:27.815: E/SpannableStringBuilder(24599): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
02-23 12:39:27:815: I/Client Socket(24599): Socket[address=/192.168.237.1,port=3434,localPort=44496]
02-23 12:39:27.815: W/System.err(24599): java.net.SocketException: Socket is closed
02-23 12:39:27.835: W/System.err(24599):    at java.net.PlainSocketImpl.checkNotClosed(PlainSocketImpl.java:134)
02-23 12:39:27.835: W/System.err(24599):    at java.net.PlainSocketImpl.getInputStream(PlainSocketImpl.java:216)
02-23 12:39:27.835: W/System.err(24599):    at java.net.Socket.getInputStream(Socket.java:343)
02-23 12:39:27.835: W/System.err(24599):    at com.qubelab.smartcontrol.ScreenCast$MyTask.doInBackground(ScreenCast.java:65)
02-23 12:39:27.835: W/System.err(24599):    at com.qubelab.smartcontrol.ScreenCast$MyTask.doInBackground(ScreenCast.java:1)
02-23 12:39:27.840: W/System.err(24599):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-23 12:39:27.840: W/System.err(24599):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-23 12:39:27.840: W/System.err(24599):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-23 12:39:27.840: W/System.err(24599):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-23 12:39:27.840: W/System.err(24599):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-23 12:39:27.840: W/System.err(24599):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-23 12:39:27.840: W/System.err(24599):    at java.lang.Thread.run(Thread.java:856)

更新代码:

try { 
     if (client.isConnected){ 
         Log.i ("Client Obj",""+client);
         ObjectInputStream ois=new ObjectInputStream(client.getInputStream()); 
         byte[] buffer=(byte[]) ois.readObject(); 
         BitmapFactory.Options options=new BitmapFactory.Options();  
         options.inMutable=true; 
         bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length,options); 
         Ois.close (); 
         return bitmap; 
    } else { 
         Log.d ( " Client", " closed"); 
    } 
} catch(Exception e){ 
     e.printstacktrace();   
}

2 个答案:

答案 0 :(得分:1)

我尝试了来自Reading from and Writing to a Socket的Oracle的回声服务器和客户端的修改版本,例如客户端

Socket echoSocket = new Socket(hostName, portNumber);
ObjectInputStream in = new ObjectInputStream(echoSocket.getInputStream());
/* ... */
in.close();
if (echoSocket.isClosed())
    System.out.println("Socket is closed.");

因此,echo客户端打印

  

套接字已关闭。

因此,当您因任何原因关闭ObjectInputStream时,套接字也会关闭。这就是你获得例外的原因

  

java.net.SocketException:Socket已关闭

这意味着,如果您想要读取多个图像,则需要重新连接到服务器或尝试保持套接字打开。


要保持连接处于打开状态,请关闭ois或重新创建ObjectInputStream,例如

public class ScreenCast extends Activity {
    private Socket client;

    /* ... */

    private class MyTask extends AsyncTask<Void, Void, Bitmap> {
        private ObjectInputStream ois;

        @Override
        protected void onPreExecute() {
            if (client == null || client.isClosed()) {
                /* reconnect to server somehow */

                ois = null;
            }

            if (ois == null)
                ois = new ObjectInputStream(client.getInputStream());
        }

        @Override
        protected Bitmap doInBackground(Void... arg0) {
            byte[] buffer = (byte[]) ois.readObject();
            /* process image, do *not* close ois */
        }

        @Override
        protected void onPostExecute() {
            /* ... */
        }
    }
}

答案 1 :(得分:0)

而不是使用

import java.io*;

使用

import java.nio*;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

这些有助于持久绑定您的连接。 只是在java中的SocketChannel上搜索