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

