java 如何使用数据输入流从套接字读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14176999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read from socket using datainputstream
提问by Wajih Ahmed
I can display a single image received, but now I want to receive and write multiple images on my disk which will be sent from Android client after every 5 seconds.
我可以显示接收到的单个图像,但现在我想在我的磁盘上接收和写入多个图像,这些图像将在每 5 秒后从 Android 客户端发送。
Socket sock = servsock.accept();
dataInputStream = new DataInputStream(sock.getInputStream());
dataOutputStream = new DataOutputStream(sock.getOutputStream());
System.out.println("Accepted connection : " + sock);
dataInputStream = new DataInputStream(sock.getInputStream());
byte[] base64=dataInputStream.readUTF().getBytes();
byte[] arr=Base64.decodeBase64(base64);
FileOutputStream imageOutFile = new FileOutputStream("E:\image.jpeg");
imageOutFile.write(arr);
回答by Pragmateek
You'll need to build a protocolbetween your client and the server.
您需要在客户端和服务器之间建立一个协议。
If the image size is constant you just have to loop over the input-stream and keep buffering until you get the fixed number of bytes and write all the already buffered image.
如果图像大小不变,您只需要遍历输入流并保持缓冲,直到获得固定数量的字节并写入所有已缓冲的图像。
Otherwise you'll need to add some metadataindicating the size of the images; e.g. you could use a simple format like this :
否则,您需要添加一些指示图像大小的元数据;例如,您可以使用这样的简单格式:
[size1][image1][size2][image2][size3][image3]
with [size-i] occupying a fixed amount of space and [image-i] the size of the ith image.
[size-i] 占据固定的空间量,[image-i] 是第 i 个图像的大小。
But above all don't be tempted to do such a naive protocol as processing an image, sleeping 5 seconds and retrieving the next one because the exact time could vary a lot between each image due to client, network or the server (your code and/or the file-system).
但最重要的是,不要试图执行诸如处理图像、休眠 5 秒并检索下一个图像这样的幼稚协议,因为由于客户端、网络或服务器(您的代码和/ 或文件系统)。
回答by ggrandes
Little changes:
小改动:
Socket sock = servsock.accept();
dataInputStream = new DataInputStream(sock.getInputStream());
dataOutputStream = new DataOutputStream(sock.getOutputStream());
System.out.println("Accepted connection : " + sock);
int imagesCount = dataInputStream.readInt();
for (int imgNum = 0; imgNum < imagesCount; imgNum++) {
int imgLen = dataInputStream.readInt();
byte[] base64 = new byte[imgLen];
dataInputStream.readFully(base64);
byte[] arr = Base64.decodeBase64(base64);
FileOutputStream imageOutFile = new FileOutputStream("E:\image"+imgNum+".jpeg");
imageOutFile.write(arr);
}
回答by AshwinC
If you are having multiple clients and a single server. Try multi-threading. Sample piece of code:
如果您有多个客户端和一个服务器。尝试多线程。示例代码:
public static void main(String[] args){
try {
servSocket = new ServerSocket(xxxx);
while(true)
{
socket = servSocket.accept();
ClientNum++;
for(int i=0;i<10;i++){ //this for eg accepts 10 clients, with each client assigned an ID.
if(threads[i]==null){
sockets[i]=socket;
In your "run", you can call each of these clients by their respective ID and perform the reqd function you need. Not sure how clear this is. If needed, I can send you the entire code I have which is kinda similar to what you are doing.
在您的“运行”中,您可以通过它们各自的 ID 调用这些客户端中的每一个,并执行您需要的 reqd 功能。不知道这有多清楚。如果需要,我可以将我拥有的整个代码发送给您,这与您正在做的有点相似。