Skip to main content

Why is Image Transfer NOT ALWAYS completed?

14 replies [Last post]
davewaterworth
Offline
Joined: 2006-06-15

Hi guys, please help me avoid going insane! I'm transferring images from a server application to a client applet, using a socket system. (If there's a simpler way, please let me know...). Anyway, the image transfer seems to work ok...SOMETIMES! Other times, transferring the same image(s), the client doesn't appear to receive the complete stream of the specified number of bytes, and hangs.

I'm attaching the essentials of the Server and Client code, with some resulting debug messages. I've also attached at the bottom of the code, a simpler read method (LABEL 2) that I was using before my problems surfaced. If anybody can tell me what I'm missing or overdoing, I'll be eternally grateful..

cheers from Zululand, South Africa,
Dave

//************************************************************************************
//************************************************************************************
// SERVER APPLICATION:
//************************************************************************************
//************************************************************************************

File imgFile = new File("Image_1282.png");
BufferedImage bi = ImageIO.read(imgFile);

Iterator pngWriterCnt = ImageIO.getImageWritersByFormatName("png");
ImageWriter pngWriter = (ImageWriter)pngWriterCnt.next();

int svrPort = 2508;
Socket svrSocket = new ServerSocket(svrPort);
Object imDestStream = svrSocket.getOutputStream();
ImageOutputStream outStream = ImageIO.createImageOutputStream(imDestStream);
//************************************************************************************
pngWriter.setOutput(outStream);
pngWriter.write(bi);
outStream.flush();

//************************************************************************************
//************************************************************************************
// CLIENT APPLET:
//************************************************************************************
//************************************************************************************

String svrURL = "DJW_WS.myco.co.za";
int cliPort = 2508;
Socket cliSocket = new Socket(svrURL, cliPort);
int iBytesSoFar = 0;
int iWaitCount = 0;

InputStream myInStream = cliSocket.getInputStream();

//************************************************************************************

while ( true ) {
if ( myInStream.available() > 0 ) {
iBytesSoFar += myInStream.read(imageBuffer
,iBytesSoFar
,iImageSizeInBytes-iBytesSoFar );
iWaitCount = 0;
System.out.println("Client has read " + iBytesSoFar +
" of " + iImageSizeInBytes + " bytes");
} else if ( iWaitCount < 5 ) {
if ( iBytesSoFar == iImageSizeInBytes )
iWaitCount = 5;
else {
iWaitCount += 1;
wait((long) 500);
}
} else {
if ( iBytesSoFar != iImageSizeInBytes ) {
//LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1
//LABEL 1 - Failure point...
//LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1
System.out.println("Client: Expecting to upload <" + iImageSizeInBytes +
"> bytes, but received only <" + iBytesSoFar + ">!");
}
break;
}
}

Image 1 (LARGE) Result:
Client has read 1310873 of 1390512 bytes
Client has read 1376409 of 1390512 bytes
Client has read 1390512 of 1390512 bytes

Image 2 (Thumbnail) Result:
Client has read 8590 of 16730 bytes
Client: Expecting to upload <16730> bytes, but received only <8590>!
Client: Couldn't read Buffered Image from Server (IllegalStateException e)...

//LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2
//int iImageIndex = 0;
//boolean bSeekForwardOnly = true;
//boolean bIgnoreMetadata = true;
//pngInStream = ImageIO.createImageInputStream(cliSocket.getInputStream());
//pngReader.setInput(pngInStream,bSeekForwardOnly,bIgnoreMetadata);
//biJPGImage = pngReader.read(iImageIndex);
//LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

Actually that is possible.

On Mon, 6 Nov 2006, jai-imageio@javadesktop.org wrote:

> This is just a shot in the dark, and probably too little too late, but I noticed in your original code you were flushing the stream but not disposing of the PNG writer. I wonder if there could be some unknown reason why the PNG doesn't get fully written to the stream from the writer? Presumably ImageWriter.write() should fully populate the stream but it's just a thought. I've seen weirder things happen for sure.
>
> Mike
> [Message sent by forum member 'mnidel' (mnidel)]
>
> http://forums.java.net/jive/thread.jspa?messageID=170804
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

davewaterworth
Offline
Joined: 2006-06-15

Hi all,

I've decided to close this thread now - it doesn't look as though anybody has a real answer for me. I've decided to go the route of RMI, and put up with whatever overhead I incur. It is a lot simpler to implement, and I hope I don't get similar problems. Thanks to all who tried to help, your contributions are very much appreciated

cheers
Dave

Brian Burkhalter

Thanks for closing the topic. Sorry no effective conclusion was reached.

On Sun, 5 Nov 2006, jai-imageio@javadesktop.org wrote:

> Hi all,
>
> I've decided to close this thread now - it doesn't look as though anybody has a real answer for me. I've decided to go the route of RMI, and put up with whatever overhead I incur. It is a lot simpler to implement, and I hope I don't get similar problems. Thanks to all who tried to help, your contributions are very much appreciated
>
> cheers
> Dave
> [Message sent by forum member 'davewaterworth' (davewaterworth)]
>
> http://forums.java.net/jive/thread.jspa?messageID=170553
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

mnidel
Offline
Joined: 2005-04-08

This is just a shot in the dark, and probably too little too late, but I noticed in your original code you were flushing the stream but not disposing of the PNG writer. I wonder if there could be some unknown reason why the PNG doesn't get fully written to the stream from the writer? Presumably ImageWriter.write() should fully populate the stream but it's just a thought. I've seen weirder things happen for sure.

Mike

Fabrizio Giudici

Were you sure that the InputStream from the Socket is always wrapped
in a BufferedInputStream? The read() semantics on a socket say that
the call can return also with a partially filled buffer, and another
read() should be tried if so. Basically BufferedInputStream
implements this policy and always fixes the problem.

On Nov 6, 2006, at 19:27 , jai-imageio@javadesktop.org wrote:

> This is just a shot in the dark, and probably too little too late,
> but I noticed in your original code you were flushing the stream
> but not disposing of the PNG writer. I wonder if there could be
> some unknown reason why the PNG doesn't get fully written to the
> stream from the writer? Presumably ImageWriter.write() should fully
> populate the stream but it's just a thought. I've seen weirder
> things happen for sure.
>
> Mike
> [Message sent by forum member 'mnidel' (mnidel)]
>
> http://forums.java.net/jive/thread.jspa?messageID=170804
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>
>

--
Fabrizio Giudici, Ph.D. - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
http://www.tidalwave.it/blog - Fabrizio.Giudici@tidalwave.it
mobile: +39 348.150.6941 - fax: +39 027.005.105.36

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

davewaterworth
Offline
Joined: 2006-06-15

Brian & others,

After looking through the options mentioned, I realised I was probably overcomplicating my transfer methodology, and before trying one of the methods proposed (with extra overhead), I thought I'd have one last bash at the standard methods. I'm sending strings up & down the Socket channel, to indicate requests and notification responses(ugly format I know, but I'll change it to XML format later...), interleaved with the images themselves. I've found that there is indeed some leftover baggage from the first image transfer, which then becomes included as a prescript in the subsequent text message. I've attached the debugging output and full code below so that nothing is left out. The extra baggage can be seen as the "…]Aù IEND®B`‚" prefix to the "INCOMING THUMBNAIL:..." debug string. I don't understand why the end of the first image doesn't get processed by the client image-receipt code line: biImage = imReader.read(0); It seems to affect transfer of the 2nd (smaller) image more drastically than the first.
(Locations of pertinent output statements in the code, have been highlighted with "//@@@@@" comment strings)

regards
Dave
Siyabonga Khakulu (Zulu for "Thanks very much")

Client: Constructor has been entered.
Client: init() has been entered.
Client: start() has been entered.
Client: converse() has been entered.
Client: fromServer = HELLO
Client: Primary Image
Client: fromServer = INCOMINGIMAGE:
Client: iImageSizeInBytes = 1390512
Client: THUMBNAILIX<0>
Client: fromServer = …]Aù IEND®B`‚INCOMINGTHUMBNAIL:

Server: myCPResponseToClient = >
Server: Writing image to Client...
Server: Flushing ioStream to Client...
Server: myCPResponseToClient = >
Server: Writing image to Client...
Server: Flushing ioStream to Client...

JAVA CODE:

/*************************************************************************************************/
package package1;

import java.net.*;
import java.io.*;
import package1.Project1MultiServerThread;

public class Project1MultiServer {

public Project1MultiServer()
{
System.out.println("IXMultiServer: Constructor has been entered.");
}

public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
int serverPort = 25761;

try {
serverSocket = new ServerSocket(serverPort);
} catch (IOException e) {
System.err.println("IXMultiServer: Could not listen on port: " + serverPort + ".");
System.exit(-1);
}
while (listening) {
new Project1MultiServerThread( serverSocket.accept() ).start();
}
serverSocket.close();
}
}

/*************************************************************************************************/
package package1;

import java.net.*;
import java.io.*;
import java.util.Iterator;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

public class Project1MultiServerThread extends Thread {

private Socket txtSocket = null;

public Project1MultiServerThread( Socket inTxtSocket) {
super("IXMultiServerThread");
this.txtSocket = inTxtSocket;
}

public void run() {

Iterator imWriterCnt = null;
ImageWriter imWriter = null;
Object outStream = null;

Iterator imReaderCnt = null;
ImageReader imReader = null;
int imageIndex = 0;
String[] sCPStrings = {"INCOMINGIMAGE"
,"INCOMINGTHUMBNAIL"};

try {
PrintWriter mySocketWriter = new PrintWriter(txtSocket.getOutputStream(), true);
BufferedReader mySocketReader = new BufferedReader(
new InputStreamReader(
txtSocket.getInputStream()));
String inputStringFromClient, myCPResponseToClient;
Project1Protocol myCP = new Project1Protocol();
myCPResponseToClient = myCP.processInput(null);
mySocketWriter.println(myCPResponseToClient);

imWriterCnt = ImageIO.getImageWritersByFormatName("png");
imWriter = (ImageWriter)imWriterCnt.next();

while ((inputStringFromClient = mySocketReader.readLine()) != null) {
myCPResponseToClient = myCP.processInput(inputStringFromClient);

if (myCPResponseToClient.toUpperCase().indexOf(sCPStrings[0]) >= 0 ||
myCPResponseToClient.toUpperCase().indexOf(sCPStrings[1]) >= 0
) {
String imgFileName = "tempname";
myCPResponseToClient = myCPResponseToClient.replaceAll(" ","");
imgFileName = myCPResponseToClient.substring(myCPResponseToClient.indexOf("<")+1,myCPResponseToClient.indexOf(">")) +
myCPResponseToClient.substring(myCPResponseToClient.lastIndexOf("<")+1,myCPResponseToClient.indexOf(";"));
if (myCPResponseToClient.toUpperCase().indexOf(sCPStrings[1]) >= 0) {
imgFileName = imgFileName.replaceFirst ("\\.","_TN.");
myCPResponseToClient = myCPResponseToClient.replaceFirst("\\.","_TN.");
}

File imgFile = new File(imgFileName);
myCPResponseToClient = myCPResponseToClient.substring(0,myCPResponseToClient.indexOf("<")+1)
+ myCPResponseToClient.substring(myCPResponseToClient.lastIndexOf("<")+1);

BufferedImage bi = ImageIO.read(imgFile);

int intFileSizeInBytes = (int)imgFile.length();
String strFileSizeInBytes = Long.toString(imgFile.length()) ;
myCPResponseToClient =
myCPResponseToClient.substring(0,myCPResponseToClient.indexOf("~")+1)
+ strFileSizeInBytes
+ myCPResponseToClient.substring(myCPResponseToClient.lastIndexOf(">"));
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
System.out.println("Server: myCPResponseToClient = <" + myCPResponseToClient + ">");
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
mySocketWriter.println(myCPResponseToClient);

outStream = this.txtSocket.getOutputStream();
ImageOutputStream ioStream = ImageIO.createImageOutputStream(outStream);
imWriter.setOutput(ioStream);

try {
System.out.println("Server: Writing image to Client...");
imWriter.write(bi);

System.out.println("Server: Flushing ioStream to Client...");
ioStream.flush();

} catch (IllegalStateException ise) {
System.err.println("MultiServerThread: Couldn't write Buffered Image to Client...");
ise.printStackTrace();
System.exit(1);
} catch (IllegalArgumentException iae) {
System.err.println("MultiServerThread: Couldn't write Buffered Image to Client...");
iae.printStackTrace();
System.exit(1);
} catch (UnsupportedOperationException uoe) {
System.err.println("MultiServerThread: Couldn't write Buffered Image to Client...");
uoe.printStackTrace();
System.exit(1);
} catch (IOException ioe) {
System.err.println("MultiServerThread: Couldn't write Buffered Image to Client...");
ioe.printStackTrace();
System.exit(1);
}
}
if (myCPResponseToClient.equals("Bye"))
break;
}
myCP = null;
mySocketWriter.close();
mySocketReader.close();
txtSocket.close();

} catch (IOException e) {
System.err.println("MultiServerThread: Couldn't generate Image I/O to client...");
e.printStackTrace();
System.exit(1);
}
}
}

/*************************************************************************************************/
package package1;

import java.awt.geom.Point2D;
import java.net.*;
import java.io.*;

public class Project1Protocol {
private static final int WAITING = 0;
private static final int AWAITINGREQUEST = 1;
private static final int DISCONNECTING = 2;
private static final int ANOTHER = 3;

private static final int NUMIMAGES = 5;

private int state = WAITING;
private int reqdImageID = 0;
private int reqdHTMLID = 0;

double X_PctCoord = 0.0;
double Y_PctCoord = 0.0;

boolean bEntityIdentified = false;

private String pathToImageNames = "C:\\DJW\\Development\\JDeveloper_Projects\\project1\\";

private String[] imageNames = { "Chris"
, "Nigel"
, "Dennis"
, "Louis"
, "Leon" };

private String[] imageFileNames = { "Image_1282.png"
, "Image_3043.png"
, "Image_1862.png"
, "Image_1988.png"
, "Image_2867.png" };

private String[] htmlFileNames = { "NodeInfo_1282.htm" };

public String processInput(String theInput) {

String theOutput = null;

if (state == WAITING) {
theOutput = "HELLO";
state = AWAITINGREQUEST;
} else if (state == AWAITINGREQUEST) {
if (theInput.replaceAll(" ","").toUpperCase().indexOf("PRIMARYIMAGE") >= 0 ||
theInput.replaceAll(" ","").toUpperCase().indexOf("IMAGEIX") >= 0 ) {
if (theInput.replaceAll(" ","").toUpperCase().indexOf("PRIMARYIMAGE") >= 0 )
reqdImageID = 0;
else {
reqdImageID = Integer.parseInt(
theInput.substring(theInput.indexOf("<")+1, theInput.indexOf(">")-1));
}
theOutput = "INCOMINGIMAGE: "
+ "<" + pathToImageNames + ">"
+ "<" + imageFileNames[reqdImageID] + ";" + reqdImageID + "~0>";
state = AWAITINGREQUEST;
} else if (theInput.replaceAll(" ","").toUpperCase().indexOf("THUMBNAILIX") >= 0) {
reqdImageID = Integer.parseInt(
theInput.substring(theInput.indexOf("<")+1, theInput.indexOf(">")));
theOutput = "INCOMINGTHUMBNAIL: "
+ "<" + pathToImageNames + ">"
+ "<" + imageFileNames[reqdImageID] + ";" + reqdImageID + "~0>";
state = AWAITINGREQUEST;
} else if (theInput.replaceAll(" ","").toUpperCase().indexOf("DISCONNECT") >= 0) {
theOutput = "Disconnecting...";
state = DISCONNECTING;
} else {
theOutput = "Invalid request!";
}
}
return theOutput;
}

/*************************************************************************************************/

package package1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.*;
import java.io.File;
import java.net.*;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageReadParam;
import javax.imageio.stream.ImageInputStream;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import pkgdisplayBean.displayBean;

public class package1ClientApplet extends JApplet {
protected JButton jButton1 = new JButton();
protected displayBean displayBeanInst = new displayBean();
protected PlanarImage piAppletImage, piThumbNail;

public package1ClientApplet()
{
System.out.println("Client: Constructor has been entered.");
}

public void init() {
try {
System.out.println("Client: init() has been entered.");
}
catch(Exception e) {
e.printStackTrace();
}
}

public void start() {
System.out.println("Client: start() has been entered.");
try {
converse();
} catch (IOException e) {
System.err.println("Client: start() I/O Exception issued from converse():");
System.err.println(e.getMessage());
e.printStackTrace();
System.exit(1);
}
}

public void stop() {
System.out.println("Client: stop() has been entered.");
}

public void destroy() {
System.out.println("Client: destroy() has been entered.");
}

public synchronized void converse() throws IOException {
System.out.println("Client: converse() has been entered.");
Socket serverTEXTSocket = null;
PrintWriter writerToTEXTServer = null;
BufferedReader readerFromTEXTServer = null;

boolean bSeekForwardOnly = true;
boolean bIgnoreMetadata = true;
ImageReader imReader = null;
Iterator imReaders = null;
ImageInputStream imInStream = null;

BufferedImage biImage = null;
PlanarImage piAppletImage, piThumbNail;
String sImageType;
String iImageSizeStr;
int iImageSizeInBytes;
byte[] imageBuffer;

String serverURL = "R0177475.rbct.co.za";
int serverTEXTPort = 25761;
int responseCounts = 0;
int iOkToDisplayImage = 0;

try {
serverTEXTSocket = new Socket(serverURL, serverTEXTPort);
readerFromTEXTServer = new BufferedReader(new InputStreamReader(serverTEXTSocket.getInputStream()));
writerToTEXTServer = new PrintWriter(serverTEXTSocket.getOutputStream(), true);

imReaders = ImageIO.getImageReadersByFormatName("png");
imReader = (ImageReader)imReaders.next();
imInStream = ImageIO.createImageInputStream(serverTEXTSocket.getInputStream());
imReader.setInput(imInStream,bSeekForwardOnly,bIgnoreMetadata);

} catch (UnknownHostException e) {
System.err.println("Client: Don't know about host: \"" + serverURL + "\".");
System.err.println(e.getMessage());
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
System.err.println("Client: Couldn't get I/O for the connection to: \"" + serverURL + "\".");
System.err.println(e.getMessage());
e.printStackTrace();
System.exit(1);
}

String fromServer;
while ((fromServer = readerFromTEXTServer.readLine()) != null) {
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
System.out.println("Client: fromServer = " + fromServer);
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
if (fromServer.equals("Bye."))
break;

if ( fromServer.toUpperCase().indexOf("HELLO") >= 0) {
System.out.println("Client: Primary Image");
writerToTEXTServer.println("Primary Image");
}
else if ( fromServer.toUpperCase().indexOf("INCOMINGIMAGE") >= 0 ||
fromServer.toUpperCase().indexOf("INCOMINGTHUMBNAIL") >= 0) {
try {
iImageSizeStr = fromServer.substring(fromServer.indexOf("~")+1,fromServer.indexOf(">"));
iImageSizeInBytes = Integer.parseInt(iImageSizeStr);
System.out.println("Client: iImageSizeInBytes = " + iImageSizeInBytes);

biImage = imReader.read(0);
} catch (IllegalStateException ise) {
System.err.println("Client: Couldn't read Buffered Image from Server (IllegalStateException ise)...");
ise.printStackTrace();
System.exit(1);
} catch (IndexOutOfBoundsException ioobe) {
System.err.println("Client: Couldn't read Buffered Image from Server (IllegalStateException ioobe)...");
ioobe.printStackTrace();
System.exit(1);
} catch (IOException ioe) {
System.err.println("Client Couldn't read Buffered Image from Server (IllegalStateException ioe)...");
ioe.printStackTrace();
System.exit(1);
}

try {

if ( fromServer.toUpperCase().indexOf("INCOMINGIMAGE") >= 0 ) {
iOkToDisplayImage += 1;
writerToTEXTServer.println("THUMBNAILIX<" + fromServer.substring(fromServer.indexOf(";")+1
,fromServer.indexOf("~"))
+ ">"
);
System.out.println("Client: THUMBNAILIX<" + fromServer.substring(fromServer.indexOf(";")+1
,fromServer.indexOf("~"))
+ ">"
);
piAppletImage = PlanarImage.wrapRenderedImage(biImage);

int iBeanWidth=0,iBeanHeight=0;
if ( piAppletImage.getWidth() > this.getWidth()-100)
iBeanWidth = this.getWidth()-100;
else
iBeanWidth = piAppletImage.getWidth();
if ( piAppletImage.getHeight() > this.getHeight()-100)
iBeanHeight = this.getHeight()-100;
else
iBeanHeight = piAppletImage.getHeight();

displayBeanInst.setBounds( (int)this.getX()+50
,(int)this.getY()+30
,iBeanWidth
,iBeanHeight);
displayBeanInst.setImage(piAppletImage);
} else if ( fromServer.toUpperCase().indexOf("INCOMINGTHUMBNAIL") >= 0 ){
iOkToDisplayImage += 2;
piThumbNail = PlanarImage.wrapRenderedImage(biImage);
displayBeanInst.createGUI(piThumbNail);
}

if (iOkToDisplayImage == 3) {
iOkToDisplayImage = 0;
this.getContentPane().add(jButton1, BorderLayout.NORTH);
this.getContentPane().add(displayBeanInst, BorderLayout.CENTER);
break;
}
} catch(Exception e) {
System.err.println("Client: Couldn't read Buffered Image from Server (IllegalStateException e)...");
e.printStackTrace();
System.exit(1);
}
} else {
System.err.println("Client: UNRECOGNISED PROMPT :- \"" + fromServer + "\"");
}
}

writerToTEXTServer.close();
readerFromTEXTServer.close();
serverTEXTSocket.close();
}

static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}
}

Andrey Kuznetsov

Hi Dave,

it is unclear how client know the expected image size: "iImageSizeInBytes"

Andrey

--
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

----- Original Message -----
From:
To:
Sent: Thursday, June 15, 2006 12:06 PM
Subject: [JAI-IMAGEIO] Why is Image Transfer NOT ALWAYS completed?

> Hi guys, please help me avoid going insane! I'm transferring images from a
> server application to a client applet, using a socket system. (If there's
> a simpler way, please let me know...). Anyway, the image transfer seems to
> work ok...SOMETIMES! Other times, transferring the same image(s), the
> client doesn't appear to receive the complete stream of the specified
> number of bytes, and hangs.
>
> I'm attaching the essentials of the Server and Client code, with some
> resulting debug messages. I've also attached at the bottom of the code, a
> simpler read method (LABEL 2) that I was using before my problems
> surfaced. If anybody can tell me what I'm missing or overdoing, I'll be
> eternally grateful..
>
> cheers from Zululand, South Africa,
> Dave
>
>
> //************************************************************************************
> //************************************************************************************
> // SERVER APPLICATION:
> //************************************************************************************
> //************************************************************************************
>
> File imgFile = new File("Image_1282.png");
> BufferedImage bi = ImageIO.read(imgFile);
>
> Iterator pngWriterCnt =
> ImageIO.getImageWritersByFormatName("png");
> ImageWriter pngWriter = (ImageWriter)pngWriterCnt.next();
>
> int svrPort = 2508;
> Socket svrSocket = new ServerSocket(svrPort);
> Object imDestStream = svrSocket.getOutputStream();
> ImageOutputStream outStream =
> ImageIO.createImageOutputStream(imDestStream);
> //************************************************************************************
> pngWriter.setOutput(outStream);
> pngWriter.write(bi);
> outStream.flush();
>
>
>
> //************************************************************************************
> //************************************************************************************
> // CLIENT APPLET:
> //************************************************************************************
> //************************************************************************************
>
> String svrURL = "DJW_WS.myco.co.za";
> int cliPort = 2508;
> Socket cliSocket = new Socket(svrURL, cliPort);
> int iBytesSoFar = 0;
> int iWaitCount = 0;
>
> InputStream myInStream = cliSocket.getInputStream();
>
> //************************************************************************************
>
> while ( true ) {
> if ( myInStream.available() > 0 ) {
> iBytesSoFar += myInStream.read(imageBuffer
> ,iBytesSoFar
> ,iImageSizeInBytes-iBytesSoFar );
> iWaitCount = 0;
> System.out.println("Client has read " + iBytesSoFar +
> " of " + iImageSizeInBytes + " bytes");
> } else if ( iWaitCount < 5 ) {
> if ( iBytesSoFar == iImageSizeInBytes )
> iWaitCount = 5;
> else {
> iWaitCount += 1;
> wait((long) 500);
> }
> } else {
> if ( iBytesSoFar != iImageSizeInBytes ) {
> //LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1
> LABEL1 LABEL1
> //LABEL 1 - Failure point...
> //LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1 LABEL1
> LABEL1 LABEL1
> System.out.println("Client: Expecting to upload <" +
> iImageSizeInBytes +
> "> bytes, but received only <" + iBytesSoFar +
> ">!");
> }
> break;
> }
> }
>
>
> Image 1 (LARGE) Result:
> Client has read 1310873 of 1390512 bytes
> Client has read 1376409 of 1390512 bytes
> Client has read 1390512 of 1390512 bytes
>
> Image 2 (Thumbnail) Result:
> Client has read 8590 of 16730 bytes
> Client: Expecting to upload <16730> bytes, but received only <8590>!
> Client: Couldn't read Buffered Image from Server (IllegalStateException
> e)...
>
>
>
> //LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2
> LABEL2 LABEL2
> //int iImageIndex = 0;
> //boolean bSeekForwardOnly = true;
> //boolean bIgnoreMetadata = true;
> //pngInStream =
> ImageIO.createImageInputStream(cliSocket.getInputStream());
> //pngReader.setInput(pngInStream,bSeekForwardOnly,bIgnoreMetadata);
> //biJPGImage = pngReader.read(iImageIndex);
> //LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2 LABEL2
> LABEL2 LABEL2
> [Message sent by forum member 'davewaterworth' (davewaterworth)]
>
> http://forums.java.net/jive/thread.jspa?messageID=123578
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

davewaterworth
Offline
Joined: 2006-06-15

Hey Andrey,

Thanks very much for the reply.

The server determines the image size on reading the image from disk, and then sends the size in bytes, through the stream, before sending the image. (I omitted that code for brevity, because it wasn't really relevant to the problem). My Client is receiving the correct size ok, but in receiving the image itself, the transfer seems (SOMETIMES)to end before that full size is received. Baffles me...

cheers
Dave

Robert Engels

It is probably because you are using an ImageInstream attached to the
underlying communications stream, and the file probably has padding on the
end (that the image reading code does not need).

-----Original Message-----
From: jai-imageio@javadesktop.org [mailto:jai-imageio@javadesktop.org]
Sent: Monday, June 19, 2006 9:55 AM
To: interest@jai-imageio.dev.java.net
Subject: Re: [JAI-IMAGEIO] Why is Image Transfer NOT ALWAYS completed?

Hey Andrey,

Thanks very much for the reply.

The server determines the image size on reading the image from disk, and
then sends the size in bytes, through the stream, before sending the image.
(I omitted that code for brevity, because it wasn't really relevant to the
problem). My Client is receiving the correct size ok, but in receiving the
image itself, the transfer seems (SOMETIMES)to end before that full size is
received. Baffles me...

cheers
Dave
[Message sent by forum member 'davewaterworth' (davewaterworth)]

http://forums.java.net/jive/thread.jspa?messageID=124595

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

davewaterworth
Offline
Joined: 2006-06-15

Hi Robert,

What do I do about the padding? I've tried to use the image as is (i.e. "incomplete"), but it is rejected as NULL when I try to wrap it in a PlanarImage for display, so it seems NOT in fact, to be complete:
//--------------------------------------------------
java.lang.IllegalArgumentException: The input argument(s) may not be null.
at javax.media.jai.PlanarImage.wrapRenderedImage(PlanarImage.java:582)
at pkgConspondeo.ConspondeoClientApplet.converse(ConspondeoClientApplet.java:432)
at pkgConspondeo.ConspondeoClientApplet.start(ConspondeoClientApplet.java:69)
at sun.applet.AppletPanel.run(AppletPanel.java:377)
at java.lang.Thread.run(Thread.java:534)
//--------------------------------------------------

I have also received a reply to an off-list request (BB), and will run through that as well. If I make any headway, I'll post the reply and resolution to this thread.

cheers & thanks again
Dave

Brian Burkhalter

May we assume that you do not have either JAI or JAI Image I/O available on
the client? Which version of Java is on the client?

Brian

On Tue, 20 Jun 2006, jai-imageio@javadesktop.org wrote:

> Hi Robert,
>
> What do I do about the padding? I've tried to use the image as is (i.e. "incomplete"), but it is rejected as NULL when I try to wrap it in a PlanarImage for display, so it seems NOT in fact, to be complete:
> //--------------------------------------------------
> java.lang.IllegalArgumentException: The input argument(s) may not be null.
> at javax.media.jai.PlanarImage.wrapRenderedImage(PlanarImage.java:582)
> at pkgConspondeo.ConspondeoClientApplet.converse(ConspondeoClientApplet.java:432)
> at pkgConspondeo.ConspondeoClientApplet.start(ConspondeoClientApplet.java:69)
> at sun.applet.AppletPanel.run(AppletPanel.java:377)
> at java.lang.Thread.run(Thread.java:534)
> //--------------------------------------------------
>
> I have also received a reply to an off-list request (BB), and will run through that as well. If I make any headway, I'll post the reply and resolution to this thread.

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

davewaterworth
Offline
Joined: 2006-06-15

Hey Brian,

I do have JAI on the client (actually, both server and client reside on my W/S at the moment), and I'm using JDK 1.4.2 06 within an Oracle JDeveloper 10g IDE (which loads JDK 1.4.2 03 as standard). I'm not sure about JAI Image I/O though. I've tried to get a clear understanding of what the JAI version of Image I/O comprises and where/how to get it, but still a bit clueless in that department - sorry. I have hunted the web for it, but have come up dry. Pointers?

Dave

Brian Burkhalter

Dave,

On Thu, 22 Jun 2006, jai-imageio@javadesktop.org wrote:

> I do have JAI on the client (actually, both server and client reside on
> my W/S at the moment), and I'm using JDK 1.4.2 06 within an Oracle
> JDeveloper 10g IDE (which loads JDK 1.4.2 03 as standard).

If you have JAI on the client then why not just use something like this
directly?:

http://download.java.net/media/jai/javadoc/1.1.3-beta/javax/media/jai/re...

> I'm not sure about JAI Image I/O though. I've tried to get a clear
> understanding of what the JAI version of Image I/O comprises

What JAI Image I/O comprises is pretty much described in its javadoc overview:

http://download.java.net/media/jai-imageio/javadoc/1.1-beta/overview-sum...

> and where/how
> to get it, but still a bit clueless in that department - sorry. I have
> hunted the web for it, but have come up dry. Pointers?

JAI, JAI-Image I/O, and other Java media projects may be found on java.net
below this root:

https://media.dev.java.net/

Brian

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

davewaterworth
Offline
Joined: 2006-06-15

Ok great, I'll have a sniff around this stuff & see if I can find something that works for me (ie Something I can understand, heh, heh...) It may take me a while, but I'll post something as soon as I get a result or fall down a hole,

ngiyabonga khakulu