1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| ServerSocket servsocket = new ServerSocket(port);
while (true) {
Socket client = servsocket.accept();
System.out.println("connection accepted");
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedOutputStream output = new BufferedOutputStream(client.getOutputStream());
while (true) {
String str = reader.readLine(); // lecture du message
if (str.equals("NO_IMG")){
System.out.println("ECHO = " + str);
System.out.println("fermeture");
break;
}
System.out.println("ECHO = " + str); // trace locale
//renvoi de l'image
File imageFile = new File("src/server/testImageSequence/imageSequence"+str+".jpeg");
System.out.println("try to send "+imageFile.getAbsolutePath());
FileInputStream fis = new FileInputStream(imageFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
System.out.println("read " + readNum + " bytes,");
}
byte[] bytes = bos.toByteArray();
output.write(bytes);
output.flush();
fis.close();
}
output.close();
reader.close();
client.close();
} |
Partager