简体   繁体   中英

Sending String from Java to C (Socket)

I am trying to send some string from A Java client to a C server using C. First I send the length of the String. Then, I allocate the memory manually in C and finally I send the String character by character.

The problem that sometimes I get the right String and some times I get the whole String + Extra other unknown Character (like I am allocating more than I get).

Here is the Java Code:

protected void send(String data){
    short dataLength=(short)data.length();
    try {
        out.write(dataLength);
    for (int i=0; i<data.getBytes().length ;i++)
    {
        out.write(data.getBytes()[i]);
    }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

And here is the C code :

void read4(int sock, int *data)
{

    char dataRecv;
    char* memoireAllouee=NULL;
    int stringLength;
    int i=0;
    recv(sock, (char*)&dataRecv, sizeof(dataRecv), 0) ;
    *data = dataRecv;
    stringLength=dataRecv;
    memoireAllouee=malloc(sizeof(char)*stringLength);
    if (memoireAllouee==NULL)
    {
        exit(0);
    }
    for (i=0;i<stringLength;i++)
    {
        recv(sock, (char*)&dataRecv, sizeof(dataRecv), 0) ;
        *data = dataRecv;
        memoireAllouee[i]=dataRecv;
    }
    printf("\n\n%d\n\n\n",stringLength);
    printf("\n%s\n",memoireAllouee);
}

If you think also that this method is not optimal can you help me with a faster one?

protected void send(String data){
    short dataLength=(short)data.length();
    try {
        out.write(dataLength);
    for (int i=0; i<data.getBytes().length ;i++)
    {
        out.write(data.getBytes()[i]);
    }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

For starters, you're recomputing the entire getBytes() array twice for every character . Save the byte[] to a variable byteArray and use that -- you're making this take quadratic time, completely unnecessarily. Additionally, why not just call out.write(byteArray) directly, instead of doing the for loop?

Secondly, data.length() isn't always equal to data.getBytes().length() . Make sure you're writing byteArray.length instead of just data.length() .

Finally, make sure you're using a consistent charset on both ends. The string-to-byte-array mapping is heavily dependent on the Charset , so be sure it's the same Charset on both sides so you don't run into encoding issues.

To answer your second question:

for (int i=0; i<data.getBytes().length ;i++)
{
    out.write(data.getBytes()[i]);
}

should be just:

out.write(data.getBytes());

and

for (i=0;i<stringLength;i++)
{
    recv(sock, (char*)&dataRecv, sizeof(dataRecv), 0) ;
    *data = dataRecv;
    memoireAllouee[i]=dataRecv;
}

should be:

int offset= 0;
while (offset < stringLength)
{
    int count = recv(sock, &memoireAllouee[offset], stringLength-offset 0) ;
    if (count == 0)
        // premature EOS .. do something
        break;
    if (count == -1)
        // Error ... do something
        break;
    offset += count;
}

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