0

I have a problem realizing a C client/Java server. I have a problem with the put method in server side. My problem is the same as (Handling C char arrays with Java char arrays) but the given solution doesn't work in my case.

My problem is that I receive a corrupt file.

When I see the raw of the original file and the received file i noticed that Java doesn't recognize some character. For example in the original file I have the character represented by 89 in the received file I have 'ef bf bd' if I write the byte array in UTF-8 or '3f' if I write it in US-ASCII encoding. Here's the important part of my program :

InputStream entreeSocket = socketService.getInputStream();
FileOutputStream out = new FileOutputStream(filename);
while(length > 0){
int nb;
if(length > buffer.length)
        nb = socket.read(buffer,0,buffer.length);
else
    nb = socket.read(buffer,0,length);
out.write(buffer,0,nb);
if(nb == -1) break;
length -=nb;            
}

out.close();
socket.close();

Client side :

char buffer[1024];
while(length > 0 ){ 
   nb = read(fd,buffer,1024);
   write(sockfd,buffer,nbChar);               
   length = length - nb;;
 } 

Any help would be appreciated. Thanks in advance.

Community
  • 1
  • 1
user3036156
  • 181
  • 1
  • 1
  • 7

1 Answers1

0

Since it's a client server and you use socket programming it would be good to use hton() and ntoh() functions just to be sure. These are functions that are used in C/C++ and I am sure that there will be something like this in java. I think it will solve your problem, or at least it is a better implementation. I give you the link :

http://www.beej.us/guide/bgnet/output/html/multipage/htonsman.html

I find the description below the functions very informative.

JmRag
  • 1,443
  • 7
  • 19
  • 56