如何在服务器上同时阅读和写作 [英] How Can I Reading And Writing At The Same Time On Server
本文介绍了如何在服务器上同时阅读和写作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在java中编写一个服务器,用于发送手机图像并同时从中读取数据,手机发送触摸位置以及如果手机上没有触摸就会卡住读取的问题,这是非常慢地
i write a server in java that sending images for phone and reading data from it at the same time, the phone sending the position of touch and the problem that stuck on read if there is no touch on the phone, and it's been very slowlly
private void whileConnecting() throws IOException, AWTException {
byte[] buffer;
while(connection.isConnected()){
x = input.readFloat();
y = input.readFloat();
System.out.println("x = y");
capture = robot.createScreenCapture(screenRectangle);//capture whole screen
baos = new ByteArrayOutputStream();//this object will receive the capture image
ImageIO.write(capture, "jpg", baos);//chose "JPG" because this is the best image's format from size and quality
baos.flush();//flush all the bytes
System.out.println("Size of baos = " + baos.size());
buffer = baos.toByteArray();//convert ByteArrayOutputStream to bytes array that can to send it over socket
baos.close();//close the ByteArrayOutputStream to can write in it again, if not closed it will append images
output.writeObject(buffer);//send the image's byte array over the socket
output.flush();//flush all bytes
}
}
推荐答案
我会创建两个具有不同端口号的套接字,一个用于用于接收数据的图像和其他图像。
I would create two sockets with different port numbers, one for the images and other for receiving the data.
这篇关于如何在服务器上同时阅读和写作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文