I'm trying to send a file between a client and server with sockets, the only way i've found to do it so far is by opening up a FileInputStream, reading it byte at a time and passing it over a socket and having it written out by a FileOutputStream byte at a time. this looks like it works, text files are fine but jpeg images show a scrambled pattern and the bytes being sent look the same. the hash codes don't match up either, using the File.hashCode() (though this seems to increase slightly for every transfer).
the code:
(sending)
recieving:
the print outs from System.out.println(data) seems to match up, but gets hard to check with files over a few hundred bytes.
am I doing anything wrong there?
is there a better way to do it?
do binary files need to be read/sent/written in a different way?
the code:
(sending)
Code:
FileInputStream fileinputstream = new FileInputStream(file);
int data;
for(int i=0;i<file.length(); i++)
{
data = fileinputstream.read();
System.out.print(data);
output.write(data);
}
output.flush();
recieving:
Code:
FileOutputStream outfile = new FileOutputStream(filename);
int data;
for(int i=0; i<filesize; i++)
{
data = input.read();
System.out.print(data);
outfile.write(data);
}
outfile.close();
the print outs from System.out.println(data) seems to match up, but gets hard to check with files over a few hundred bytes.
am I doing anything wrong there?
is there a better way to do it?
do binary files need to be read/sent/written in a different way?