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.

4
  • See How to write a UTF-8 file with Java? Commented Nov 28, 2013 at 21:20
  • I have tried that but it doesn't work. Thanks Commented Nov 28, 2013 at 21:21
  • Is this a job for JNI? Commented Nov 28, 2013 at 21:21
  • No, it has nothing to do with JNI Commented Nov 28, 2013 at 21:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.