Hello. Suddenly ran into a problem. You need to transfer images one by one through the network. Images are generated and saved on the computer. Next, you need to transfer over the network. The receiver receives only 1 file, which grows to infinity, instead of taking each picture separately. Do not tell me where the error is.

Broadcast:

while(true){ BufferedOutputStream(soketOutStream); File file = new File (index + ".jpg"); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int readed; while ((readed = fis.read(buf)) != -1) { baos.write(buf, 0, readed); } fis.close(); byte[] data = baos.toByteArray(); bisOutVideo.write(data, 0, data.length); bisOutVideo.flush(); bisOutVideo.close(); } 

Receiver:

 int counti=0; bisIn = new BufferedInputStream(soketInputStream) while (true) { FileOutputStream file = new FileOutputStream(messagerInput+"/"+counti+".jpg"); byte[] b = new byte[1024]; int count = 0; while ((count = bisIn.read(b)) != -1) { file.write(b, 0, count); } file.close(); counti++; } 

Does not exit the loop, last indefinitely - while ((count = bisIn.read(b)) != -1)

  • Are you sure that it is on this cycle that it hangs? What do you have on the program, what is set at the reception while (true) , in which there is not a single break -a. These are the very real endless loops. - Regent
  • The fact of the matter is that the while (true) loop does not work, it enters it once and hangs on the while loop ((count = bisIn.read (b))! = -1), as I understand it, the end-of-file character is not transmitted -1, somewhere a mistake - Alexander
  • The question is to add a reproducible example. At the moment you have in it, for example, there are enigmatic (not explicitly declared in the presented code) variables soketOutStream , soketInStream , bisOutVideo . This is necessary so that you can test and find the problem in the code. So far, I can say that permanent writing and reading without sleep in an infinite loop is not a sensible design and can ruin anything. - Regent
  • Now I will prepare this part of the code and try to add - Alexander

1 answer 1

At the moment, solved the problem in this way. The count variable receives the number of bytes read. In this case, it is equal to 1024, if the full volume was considered. If the value is less than 1024, then this is a sign of the end of the file and the file can be closed. In my case, this condition is sufficient. It is very strange that when transferring data there is no end-of-file method.