problem with receiving file over socket....

hi, as part of college project i'm creating a media file server (using RDBMS would be much more easier)...the client-server are implemented using socket programming, server has hashtable which keeps track of files available on the server....though the uploading file from client to server is working file but some how vice-versa part is not working for me...client is always receiving some bytes less than that the server is sending....

i'm posting a demo code of what i've done...

//Client code

import java.net.*;

import java.io.*;

import java.util.*;

publicclass TempClient

{

public TempClient(String ip)throws Exception

{

Socket soc=new Socket(ip,20484);

byte[] buf=newbyte[512];

int bread;

int totsize=0;

Long lval=new Long(20484);

InputStream istream=soc.getInputStream();

BufferedReader bufread=new BufferedReader(new InputStreamReader(istream));

OutputStream ostream=soc.getOutputStream();

PrintWriter pout=new PrintWriter(ostream,true);

pout.println("get");

pout.println(lval.toString());

pout.flush();

String fname=bufread.readLine();

String prefix=fname.substring(0,fname.lastIndexOf('.')).trim();

String suffix=fname.substring(fname.lastIndexOf('.'),fname.length()).trim();

File tempfile=File.createTempFile(prefix,suffix);

tempfile.deleteOnExit();

FileOutputStream fos=new FileOutputStream(tempfile);

while((bread=istream.read(buf))!=-1)

{

totsize=totsize+bread;

fos.write(buf,0,bread);

}

istream.close();

fos.flush();

fos.close();

ostream.close();

System.out.println("bytes read: "+totsize);

soc.close();

}

publicstaticvoid main(String args[])throws Exception

{

new TempClient("127.0.0.1");

}

}

//server code

import java.io.*;

import java.net.*;

import java.util.*;

publicclass TempServer

{

public TempServer()throws Exception

{

ServerSocket sersoc=new ServerSocket(20484);

System.out.println("server started...");

while(true)

{

try

{

Socket soc=sersoc.accept();

File myfile=new File("c:/jmf.zip");

byte[] buf=newbyte[512];

int bread;

int totsize=0;

InputStream istream=soc.getInputStream();

BufferedReader bufread=new BufferedReader(new InputStreamReader(istream));

OutputStream ostream=soc.getOutputStream();

PrintWriter pout=new PrintWriter(ostream,true);

String action=bufread.readLine().trim();

System.out.println("action "+action);

Long lval=new Long(bufread.readLine().trim());

System.out.println("long value: "+lval.toString());

pout.println(myfile.getName());

FileInputStream fis=new FileInputStream(myfile);

while((bread=fis.read(buf))!=-1)

{

totsize=totsize+bread;

ostream.write(buf,0,bread);

}

ostream.flush();

fis.close();

ostream.close();

istream.close();

System.out.println("bytes read: "+totsize);

soc.close();

System.out.println("done...");

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

publicstaticvoid main(String args[])throws Exception

{

new TempServer();

}

}

urgently required....

thanx

[6167 byte] By [Ratnaraja] at [2007-11-26 16:38:16]
# 1

Your BufferedReader is reading ahead (to fill its buffer) and consuming the first part of the received file. You will have to use DataInputStream.readLine() to get the filename (it doesn't buffer), or organize a pushback.

Why does the client have to send a number instead of the filename?

ejpa at 2007-7-8 23:05:03 > top of Java-index,Core,Core APIs...
# 2
actually client has the file id ....and also server maintains the hashtable that maps long value to file object.....thats why..
Ratnaraja at 2007-7-8 23:05:03 > top of Java-index,Core,Core APIs...
# 3
and also i forgot to mention...DataInputStream.readLine() is deprecated....i'm jdk1.5thanx for quick reply
Ratnaraja at 2007-7-8 23:05:03 > top of Java-index,Core,Core APIs...
# 4

> and also i forgot to mention...DataInputStream.readLine() is

> deprecated....i'm jdk1.5

I'm quite aware of that, but it's also the only non-buffered readLine() method in the JDK. If you're trying to mix two kinds of input stream you will have to compromise: generally it's a really bad idea to do so. You could use writeUTF() and readUTF instead of println() and readLine() if you want perfect purity.

ejpa at 2007-7-8 23:05:03 > top of Java-index,Core,Core APIs...
# 5
so nice of u...it worked...it worked!!!!!many many thanx
Ratnaraja at 2007-7-8 23:05:03 > top of Java-index,Core,Core APIs...