• Post Reply Bookmark Topic Watch Topic
  • New Topic

file transfer over TCP problem  RSS feed

 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I am writing a simple client/server application to download/ upload files from/to server.
I am using my own protocol.
The problem is that when I am trying to send a binary files (an image for example) it doesn't work.
I am using the following code:

I receive a file at the other end, with the same size but the first 10 offsets are different( I checked that using the windows command cmp).
Can any body help me with that ?

Thanks a lot.

regards,
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, Available Doesnt Do What You Think It Does
Second, what types are headerWriter and userDataWriter? Using Writers for binary data is a bad idea. Writers do character conversion on data and if the values in the data can't be converted into the current charset you will lose some of that particular value.
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
headerReader is of type BufferedReader and userDataReader is of type DataOutputStream.
so pleaaaaaaaase what should I use in order for this to work.
this problem has been haunting me for a long long time.
I read the link you gave me and I replaced my code with the following, but still it doesn't work:



Thanks a lot!
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using BufferedReader is a bad idea. It will attempt to read as much as 8k from the input stream, so it could be consuming some of your data. What does the corresponding client code look like?
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed the code a little bit, and now sometimes it works and sometimes it doesn't:

server code:


client code:


I have added this condition that the bytesRead are less that 1024, than we have to finish looping, because before it stayed blocked somewhere even if it's finished.

Thanks a lot for your help.
regards
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the most confusing thing is that the EXACT same code, when I use it to upload a file, is working 100% fine. I just switch the client and server code.
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by aymane chetibi:
I have added this condition that the bytesRead are less that 1024, than we have to finish looping, because before it stayed blocked somewhere even if it's finished.
[/QB]


Keep in mind that the number of bytes can be less than the size of the array for several reasons, not just EOF. Network congestion for one.
I would expect that you would get a Exception when you try to read after the server has invoked socketFromClient.close(). Are you writing exceptions out? In any case, the most reliable way to move a file using a custom protocol that I've found is to send the file size, then read that many bytes.
As for different behavior on client and server, are you sure you are keeping the classes in sync?
Exactly what is the problem? You said the "first ten offsets are different". What do you mean by that? Is it only the first ten, or is that all you've checked?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!