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

[7693 byte] By [viggua] at [2007-11-27 10:38:53]
# 1

Closing the output stream closes the socket.

ejpa at 2007-7-28 18:57:29 > top of Java-index,Java Essentials,New To Java...
# 2

Hello Ejp

Thanks for the information. Does it require me to open the socket and get connected to the client every time i transfer a file ? Can you suggest any work around to my problem here. I do not wish to create new sockets every time i need to send a file. And if i do not close that bufferedstream, my program does not work. Any suggestions would be very helpful to me.

Thanks again for the reply

Vignesh

Message was edited by:

viggu

viggua at 2007-7-28 18:57:29 > top of Java-index,Java Essentials,New To Java...
# 3

Your clients read to EOS, which only happens when you close the socket. In other words the EOS is part of your application protocol. You need to change that. Send the length of the image before sending the image, and have the client stop reading when it's read that number of bytes.

ejpa at 2007-7-28 18:57:29 > top of Java-index,Java Essentials,New To Java...
# 4

Thank you for your timely reply. I am trying it out. will post the results.

viggua at 2007-7-28 18:57:29 > top of Java-index,Java Essentials,New To Java...
# 5

Hello ejp

I used file length as the indicator to specify the EOF and now i am able to transfer 'any' number of files across the network. I had to add the sleep command before transmitting files and after sending the file length. I took a random number as the parameter for sleep(millisecs). Though my code works, i am not sure whether this is the correct way to implement. Do you suggest any better ways (or) you think this good enough ?

public void transmitImage (String path, int numt)

{

File Serverfile = new File(path);

try

{

BufferedInputStream bufRead = new BufferedInputStream(new FileInputStream(Serverfile)); //Get the Input location of the File

int charRead = 0;

byte b[] = new byte[1024];

Long serverFileLen = null;

serverFileLen = Serverfile.length();

System.out.println("The Server File size is " + serverFileLen + "Bytes");

String textlength = serverFileLen.toString();

for(int i = 0;i<MAX_CONN; i++){

if(bitmapArray[i] == true)

{

try{

outStreamArray[i].write(textlength.getBytes());

outStreamArray[i].flush();

}

catch (SocketException ex)

{

outStreamArray[i].close();

socketArray[i].close();

bitmapArray[i] = false;

}

}

}

Thread.sleep(100);

while ((charRead=bufRead.read(b))> 0 )

{

for(int i=0 ; i<MAX_CONN ; i++)

{

if(bitmapArray[i] == true)

{

try{

outStreamArray[i].write(b, 0, charRead); // send the Chunks to the clients

}

catch(SocketException ex)

{

outStreamArray[i].close();

socketArray[i].close();

bitmapArray[i] = false;

}

}

}

}

for(int i=0 ; i><MAX_CONN ; i++)

{

if(bitmapArray[i] == true)

{

try{

outStreamArray[i].flush();

}

catch (SocketException ex)

{

outStreamArray[i].close();

socketArray[i].close();

bitmapArray[i] = false;

}

}

}

bufRead.close();

}

catch(Exception ex)

{

System.out.println("Issues with transmit image from Server Side");

ex.printStackTrace();

}

}

Thanks

Vignesh>

viggua at 2007-7-28 18:57:29 > top of Java-index,Java Essentials,New To Java...