2

私は完全に画像を受信し、AndroidビデオC# Serverをストリーミングしています。問題は、画像ごとに常にソケットを開閉していることです。単一のソケットのみを作成するときに画像を受け取るにはどうすればよいですか。どんな助けでも大歓迎です。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView)findViewById(R.id.textView1);
    etip=(EditText)findViewById(R.id.editText1);
//  etip.setText("192.168.1.5");
    etip.setText("10.0.2.2");
    imgview=(ImageView)findViewById(R.id.imageView1);
    btn=(Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
             try {
                    serverAddr = InetAddress.getByName(etip.getText().toString());
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } 
                    Log.d("TCP", "C: Connecting..."); 
            }
            finally
            {

            }
            try
            {
                {
                 mHandler = new Handler();
                      mHandler.post(connectSocket);
                }       

}
            catch(Exception ex)
            {

            }

        }

    });
}

    private Runnable connectSocket=new Runnable() {

        @Override
        public void run() {

            {
                try { 
                socket = new Socket(serverAddr, 4444);
                            DataInputStream dis;
                            try { 
                                dis=new DataInputStream(socket.getInputStream());

                                int bytesRead;
                                byte[] pic = new byte[5000*1024];
                                bytesRead = dis.read(pic, 0, pic.length);
                                 bitmapimage = BitmapFactory.decodeByteArray(pic, 0, bytesRead);              


                            } catch(Exception e) { 
                                Log.e("TCP", "S: Error", e); 
                            } finally { 
                                socket.close(); 
                            } 

                        } catch (UnknownHostException e) { 
                            // TODO Auto-generated catch block 
                            Log.e("TCP", "C: UnknownHostException", e); 
                            e.printStackTrace(); 
                        } catch (IOException e) { 
                            // TODO Auto-generated catch block 
                            Log.e("TCP", "C: IOException", e); 
                            e.printStackTrace(); 
                        }
                        imgview.setImageBitmap(bitmapimage);
                        imgview.invalidate();

    }
             mHandler.postDelayed(this, 0);
        }   //run end
    };


    }
4

1 に答える 1

0

The socket won't close until you tell it to close like

socket.close();

FYI, http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#close() socket close function is also going to close any associative input/output streams

or the thread/application dies.

To make your application/thread constantly waits and reads,

http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html#read(byte[])

Reads some number of bytes from the contained input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

use read function of inputstream inside a loop and let it read new data whenever they are available.

However, I recommend you to try reduce number of socket connections as much as possible by limiting maximum socket connections and etc. This might prevent some attackers who want to just open up large amount of socket connections and do nothing to your server.

于 2013-05-18T07:32:40.507 に答える