• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

sending files over sockets in java

Desova

Limp Gawd
Joined
Jun 24, 2002
Messages
467
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)
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?
 
I'm not sure, but it looks awfully fishy that you're using an int rather than a byte or byte array.
 
Back
Top