我正在从java服务器发送屏幕截图到android客户端抛出套接字 并使用异步任务接收它,它可以正常工作几秒钟并断开连接并显示连接重置
客户代码
connectTask conctTask = new connectTask();
conctTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
public class connectTask extends AsyncTask<Bitmap,Bitmap,TCPClient> {
@Override
protected TCPClient doInBackground(Bitmap... bitmap)
{
//we create a TCPClient object and
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived()
{
@Override
//here the messageReceived method is implemented
public void messageReceived(Bitmap bitmap)
{
try
{//this method calls the onProgressUpdate
publishProgress(bitmap);}
catch (Exception e)
{e.printStackTrace();}
}
},ip);
mTcpClient.run();
if(mTcpClient!=null)
{
mTcpClient.sendMessage("Initial Message when connected with Socket Server");
}
return null;
}
@SuppressWarnings("deprecation")
@Override
protected void onProgressUpdate(Bitmap...vbitmap) {
super.onProgressUpdate(vbitmap);
if(vbitmap!=null){
super.onProgressUpdate(vbitmap);
Bitmap bitmap = vbitmap[0];
if (bitmap != null) {
ob = new BitmapDrawable(getResources(), bitmap);
im.post(new Runnable (){
@Override
public void run() {
// TODO Auto-generated method stub
im.setBackgroundDrawable(ob);
}
});
** Java Server *
try
{
serverSocket = new ServerSocket(SERVERPORT);
// create client socket... the method accept() listens for a
// connection to be made to this socket and accepts it.
try
{
client = serverSocket.accept();
System.out.println("S: Receiving...");
// sends the message to the client
mOut = new ObjectOutputStream(client.getOutputStream());
if (mOut != null)
{
//Start this thread ie below run()
}
}
catch (Exception e)
{
System.out.println("Error: "+e.getMessage());
e.printStackTrace();
}
finally
{
client.close();
System.out.println("PA: Done.");
}
}
catch (Exception e)
{
System.out.println("PA: Error");
e.printStackTrace();
}
finally
{
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run()
{
while(screenCasting)
{
try {
BufferedImage screenShot = rb.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
screenShot.setAccelerationPriority(1);
ByteArrayOutputStream bis=new ByteArrayOutputStream();
ImageIO.write(screenShot,"jpeg",bis);
byte[] buffer=bis.toByteArray();
mOut.writeObject(buffer);
mOut.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
它可以正常工作几分钟,但在服务器上显示一些错误连接重置