Receiving image using socket connection in midlet

not able to display images received by socket connection in midlet
[73 byte] By [Neema] at [2007-11-26 19:11:28]
# 1
Then you should fix that!
deepspacea at 2007-7-9 21:08:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

see here is my server code:

ServerSocket s=new ServerSocket(5000);

Socket c=s.accept();

String fileName = "C://duke.png";

FileImageInputStream input = new FileImageInputStream(new File(fileName));

ByteArrayOutputStream output = new ByteArrayOutputStream();

PrintWriter p=new PrintWriter(new OutputStreamWriter(c.getOutputStream()),true);

byte[] buf = new byte[(int)input.length()];

byte[] data = null;

int numBytesRead = 0;

while ((numBytesRead = input.read(buf)) != -1) {

output.write(buf, 0, numBytesRead);

}

data = output.toByteArray();

System.out.print(data);

p.print(data);

output.close();

input.close();

p.close();

s.close();

*************client code***********

ByteArrayOutputStream bStrm=null;

try {

sc = (SocketConnection) Connector.open("socket://localhost:5000");

DataInputStream dis=sc.openDataInputStream();

int len=dis.available();

imgData=new byte[len];

os = sc.openOutputStream();

int s=0;

int c;

String ss;

// Loop forever, receiving data

bStrm=new ByteArrayOutputStream();

c = 0;

while ((c=dis.read())!=-1)

{

bStrm.write(c);

}

imgData = bStrm.toByteArray();

System.out.println(imgData);

bStrm.close();

// Form Screen;

img=Image.createImage(imgData,0,imgData.length);

} catch (ConnectionNotFoundException cnfe) {

Alert a = new Alert("Client", "Please run Server MIDlet first",

null, AlertType.ERROR);

a.setTimeout(Alert.FOREVER);

a.setCommandListener(this);

} catch (IOException ioe) {

if (!stop) {

ioe.printStackTrace();

}

} catch (Exception e) {

e.printStackTrace();

}

*******************

but it throws sometimes ArrayOutOfBound or IllegalArgumentException. pls help.

Neema at 2007-7-9 21:08:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
pls help!thanks in advance
Neema at 2007-7-9 21:08:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

Hi,

Sending the data from server through PrintWriter.print(Object) doesn't look good. It seems, that it will only send Object.toString() instead of you array. try to use OutputStream instead.

You can even get rid of intermidiate buf[] variable and do like the following:

OutputStream os=c.getOutputStream();

int numBytesRead = 0;

while ((numBytesRead = input.read(buf)) != -1) {

os.write(buf, 0, numBytesRead);

}

Message was edited by:

Tigra321

Tigra321a at 2007-7-9 21:08:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

> pls help!

> thanks in advance

this links may help you...

http://www.ddj.com/184406435;jsessionid=ZA1AYWHR2QFGUQSNDLQSKH0CJUNN2JVN

http://www.java-tips.org/java-me-tips/midp/how-to-download-an-image-from-a-web-server.html

i think that you make confusion between input and output streams...

suparenoa at 2007-7-9 21:08:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
thnk u Tigra321. i used OutputStream and now its working perfectly.Message was edited by: Neem
Neema at 2007-7-9 21:08:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

I seem to be having trouble with receiving images on client side. I keep getting a error:

error 10054 during TCP read

It seems to work sometimes and at other times it fails, which is just weird. I'm not sure how or why this happens.

Could you paste the working version of your client and server side code please. I'd greatly appreciate it as I've been struggling with this for quite a while now.

kpn04a at 2007-7-9 21:08:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8
did you follow the links given?
suparenoa at 2007-7-9 21:08:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9

i figured out the problem. basically you need to set the buffer size to something larger say about a 1000 bytes for example. This needs to be done both on the server side and the j2me client side. the buf array of bytes also needs to be set to the same size as your socket connection buffer:

SERVER:

FileImageInputStream input = new FileImageInputStream(new File("FILENAME"));

sock.setSendBufferSize(1000);

byte[] buf = new byte[1000];

os = sock.getOutputStream();

int numBytesRead = 0;

while ((numBytesRead = input.read(buf)) != -1) {

os.write(buf, 0, numBytesRead);

}

CLIENT:

sockCon.setSocketOption(SocketConnection.RCVBUF, 3000);

is = sockCon.openDataInputStream();

int len = is.available();

byte [] imgByte = new byte[len];

ByteArrayOutputStream bStr = new ByteArrayOutputStream();

int ch = 0;

while ( (ch = is.read()) != -1 ) {

bStr.write(ch);

}

is.close();

sockCon.close();

imgByte = bStr.toByteArray();

bStr.close();

Image imgRes = Image.createImage(imgByte, 0, imgByte.length);

Message was edited by:

kpn04

kpn04a at 2007-7-9 21:08:45 > top of Java-index,Java Mobility Forums,Java ME Technologies...