简体   繁体   中英

How to send C char array to Java client over a socket

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 + "");
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM