How To Separate Header from File in Socket Connection

Hi, I'm writing a P2P program using Gnutella API, and thisi uses a socket that connects to a Gnutella server, and attempts to download a file from it. The problem is, I can't separate the header from the file itself, and use the information from the header to guide the file i/o. Here's the code:

Socket s = new Socket(IP, PORT);

InputStream is = s.getInputStream();

OutputStream os = s.getOutputStream();

os.write((

"GET /get/"+FILE_ID+"/"+FILE_NAME+" HTTP/1.0\r\n"

+ "Connection: Keep-Alive\r\n" // not necessary

+ "Range: bytes=0-\r\n" // not necessary

+ "\r\n" // not necessary

).getBytes());

When I tried to read/write into a file, the file gets corrupted/truncated since the header was stuck into the file (meaning the header of the response, like "HTTP 200 OK" or something.

I'm on a deadline. Please help!

[900 byte] By [wwwslashdotcom] at [2007-9-26 3:33:10]
# 1

String temp;

boolean header = true;

BufferedReader buffReader = new BufferedReader(new InputStreamReader(s.getInputStream()));

while((temp=buffReader.readLine()) != null){

if(temp.length() == 0){//temp's length will be 0 at the end of the headers

header = false;

}

if(!header){

//insert into file code here

}

}

this should strip out the headers before writing to the file.

Hope this helps

Mass

Massarian at 2007-6-29 12:01:27 > top of Java-index,Archived Forums,Socket Programming...