0

I'm writing a server program in c, and the client is on android platform which uses java language.Now I have a trouble to send char array from the server to the client,which means the client can get the data but can not decode it.I think it maybe because of the problem of data types or encoding differences.Can anyone give me some ideas,Thanks a lot!

Here is my code of server side:

char buf[MAXSIZE];
memset(buf, 0, MAXSIZE);
int n_write;

strcpy(buf, "0008200050005001");

n_write = Write(confd, buf, strlen(buf));

if (n_write <= 0)
{
    fputs("Write error in send_matrix_and_position\n", stderr);
    close(confd);
    return -1;
}

And here is Java code:

mSocket = new Socket(HOST, PORT);
mIn = mSocket.getInputStream();
mOut = mSocket.getOutputStream();
byte[] lengthByte = new byte[4];
mIn.read(lengthByte);
for(byte b : lengthByte)
{
System.out.println(b + "");
}
2
  • I've tried to send a char array like this 'n_write = Write(confd, buf, strlen(buf));',and received with a ByteBuffer in client side in Java. Commented Mar 1, 2013 at 4:14
  • show us the code, make it as small as possible to demonstrate the problem. Put in more than an iota of effort and you will get better results. Commented Mar 1, 2013 at 5:10

2 Answers 2

1

You are sending 16 characters, but you read only four. Aren't you getting the data 48 48 48 56? These are the codes of the first four characters sent.

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

Comments

0

Try checking what values you get at the client when you read the char array, you might probably be doing br.readline(); see if this prints the characters?? You need to debug and check, then you might come up with some way.

1 Comment

The values I get at client are not like what I send from server side,and no br.readline();

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.