Problems with multiple read
Hello All,
The Server program that i wrote selects the image from the disk one at a time and sends it to the client on Clicking NEXT button.
On the Client side i just read the file and using image.io i display them to the user. The user in the client side does not click any button other than the connect button to connect to the Server. Only in the Server side we click next button.
I have a series of images in the server side. I name it as SlideX.jpeg, X -> 0 to 10. I read the Slide0 properly and send it to the client and it is getting displayed properly. When i click the next button to go on to the Slide1, i get a error. The socket is closed.
-
Issues with transmit image from Server Side
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
-
publicvoid transmitImage (String path)
{
try
{
BufferedInputStream bufRead =new BufferedInputStream(new FileInputStream(path));//Get the Input location of the File
int charRead = 0;
byte b[] =newbyte[1024];
String token ="SEnDinG";
for(int i=0 ; i<MAX_CONN ; i++)
{
if(bitmapArray[i] ==true)
{
tokenwriter[i].println(token);// send the token to the clients
}
}
while ((charRead=bufRead.read(b))> 0 )
{
for(int i=0 ; i<MAX_CONN ; i++)
{
if(bitmapArray[i] ==true)
{
outStreamArray[i].write(b, 0, charRead);// send the Chunks to the clients
}
}
}
for(int i=0 ; i><MAX_CONN ; i++)
{
outStreamArray[i].close();
}
for(int i=0 ; i><MAX_CONN ; i++)
{
outStreamArray[i] =new BufferedOutputStream(socketArray[i].getOutputStream()) ;
}
bufRead.close();
}
catch(Exception ex)
{
System.out.println("Issues with transmit image from Server Side");
ex.printStackTrace();
}
}
I see to that client gets a Token message before he gets the real image and displays the same onto the screen. What i mean is i send a message called asSending before transmitting the image. In the client side i read the tokenSending. This is for Synchronization purpose as the client never knows when the server guy is clicking Next.
Client Side token receiving.
publicvoid getToken(ImagePanel paneldisplay){
String checkme ="SEnDinG";
try{
while(true){
tokenread =new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String line = tokenread.readLine();
System.out.println("The token recvd is " + line);
if(line.equals(checkme))
{
getImage(path);
displayImage(path, paneldisplay);
System.out.println("It is saying that tokens are equal");
}
}
}
catch(IOException e){
System.out.println("Fetch Error");
}
}
Client Side Get Image
publicvoid getImage(String clientpath)
{
try{
BufferedInputStream in =new BufferedInputStream(clientSocket.getInputStream());
BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(clientpath));
int check, inc =0;
byte toread[] =newbyte[1024];
while ((check = in.read(toread)) > 0)
{
out.write(toread, 0, check);//read input from the client thread
System.out.println("Reading : " + check +" iterations" + inc);
inc++;
}
System.out.println("I have come out of get Image from the Client section");
out.close();
in.close() ;
}
catch(FileNotFoundException e){
System.out.println("File not found");
e.printStackTrace();
}
catch(IOException e){
System.out.println("File Read/Write Error");
e.printStackTrace();
}
}
I have been sitting with this issue for past 2 days and breaking my head as to find the problem. Why is my server able to send one image successfully and why is that my client able to get only that image and then display it ? I forgot to mention this.. In the client side, I all the time write the file on to the same name . Does anyone here think that it should not be the way it has to be done ? Also, i have one more query, if i do not close the BufferedOutputStream in the server side, i am not able to even send or recv the first image. Please folks, kindly give me a hint as to how i can solve this issue. I am not able to figure it out.
Thanks
Vignesh

