In my project client send some text, some images to the server. Server need to handle all these things i.e if text is there then it need to display on TestArea which is on frame and if image file is there then server need to store that image file on the computer. I created one application which handle either string(text) or image file but I don't know how to store all these things when these are send by the client at the same time.
I know when I add this code: InputStream in = socket.getInputStream();
then all the data send by the client is in this InputStream So how I identify up to this much range data is image file so need to store in Byte Array, up to this much is test or string so need to display in TextAreea. If client send two or more images at a time to the server then how server understand up to this much data this is first image, up to this much data this is second image file.
I tried this code to send Images:
Client Code:
public void sendPhotoToServer(String str){ // str is image location
try {
InputStream input = new FileInputStream(str);
byte[] buffer=new byte[1024];
int readData;
while((readData=input.read(buffer))!=-1){
dos.write(buffer,0,readData); // dos is DataOutputStream
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
and this method is in the loop so client send all the images in his folder.
Now Server Code, this is in thread and while loop is there which listen client data every time:
public void run() {
while (true) {
try {
byte[] buffer = new byte[8192];
fis = new FileOutputStream("C:\\"+(s1++)+".jpg"); // fis is FileOutputStream
while ((count = in.read(buffer)) > 0){ //count is a integer and 'in' is InputStream
fis.write(buffer, 0, count);
fis.flush();
}
} catch (Exception e) {}
}
}
but by this code first image is received by the server after that it not show other images.
and here how server understand if simple string is send by the client. In server side application it open one port to listen all the data send by the client by
FileOutputStream fis = socket.getInputStream();
now how it distinguish all these files and simple string.