简体   繁体   English

Android通过C#服务器通过TCP套接字接收图像

[英]Android receiving images over tcp socket from c# server

I'm perfectly receiving images in Android from C# Server and streaming video. 我可以从C# Server和流式视频中完美接收Android图像。 Problem is, I'm always opening and closing socket for each image. 问题是,我总是为每个图像打开和关闭插座。 How can I receive images on creating a single socket only. 如何仅在创建单个套接字时接收图像。 Any help would be appreciated. 任何帮助,将不胜感激。

@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
    };


    }

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 仅供参考, http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#close()套接字关闭函数也将关闭所有关联的输入/输出流

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[]) 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. 从包含的输入流中读取一定数量的字节,并将其存储到缓冲区数组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. 在循环内使用inputstream的读取功能,并使其在可用时读取新数据。

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. 但是,我建议您尝试通过限制最大套接字连接数等来尽可能减少套接字连接数。这可能会阻止某些攻击者仅打开大量套接字连接而对服务器不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM