此代码正在通过 tcp 从 c# 服务器接收多图像(视频)。并且工作正常。但它非常慢,而且丢失帧是他们使其快速(消除延迟)的任何方法,比如不关闭套接字,或者不使用数据输入流。
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);
}
};
}