Can't send (or receive) data from socket

Hi,

I'm trying to get a jpeg file from a webserver using a Socket. Unfortunatly I always get an empty jpeg file :s

This is a piece of code where I save the received data:

try{

byte[] buff =newbyte[100000];

int bytesIn;

if (this.myClient.isConnected()){

// Send HTTP request.

this.out.print(this.requestString);

int offset = 0;

do{

bytesIn = this.in.read(buff, offset, 100);

offset += bytesIn;

}while (bytesIn > 0);

saveBytesToFile(buff,"test.jpg");

fos.close();

}

}catch (Exception e){

System.out.println(e.getMessage());

}

When I debug my code, the debugger just stops at "bytesIn = this.in.read(buff, offset, 100);", but the program keeps waiting for something...

Is there something wrong with the way I try to save everyting into the bytes array ?

Message was edited by:

ReggieBE

[1537 byte] By [ReggieBEa] at [2007-10-2 21:10:42]
# 1
There are some glitches in your code, but generally it looks ok (except forum formatting :).Show your requestString variable content please.
Michael.Nazarov@sun.coma at 2007-7-13 23:56:44 > top of Java-index,Core,Core APIs...
# 2
You need to stop reading when you have read the number of bytes indicated in the Content-Length header. The server probably won't close the socket immediately, but that's the only way your read() will return -1.
ejpa at 2007-7-13 23:56:44 > top of Java-index,Core,Core APIs...
# 3
I think this is not transfer from server issue. Look - OP read 100 bytes each time so first time read probably should be successful - there are more then 100 bytes withn server header usually. I assume this is not custom server returns "HTTP/1.1 200 OK\n\n" :)
Michael.Nazarov@sun.coma at 2007-7-13 23:56:44 > top of Java-index,Core,Core APIs...
# 4
Who said it blocked in the first read? and how else is the loop ever going to terminate but a server close?
ejpa at 2007-7-13 23:56:44 > top of Java-index,Core,Core APIs...
# 5

> Who said it blocked in the first read?

Well, actually only I am :( I understood OP in this way...

But who said "the loop going to terminate"? OP said only about empty jpeg file. Are you sure this file was not created before or by other code or anything else? :)

Anyway OP should provide some additional info, but looks like he are not going to do so...

Michael.Nazarov@sun.coma at 2007-7-13 23:56:44 > top of Java-index,Core,Core APIs...