How to write to C struct (strings and null terminator problem)?

M

Mack Attack

I'm writing a Java client that connects to a C Server. The C server has a
struct like this:

typedef struct {
TEXT name[8],
name1[8],
reg
;
}mystruct;

TEXT is a char.

When I read this from the server, it's sending me 17 bytes as I expected.
Everything works fine that way. The problem is when I send the 17 bytes back
to the server. The fields are set with the correct values but the strings are
no longer null terminated.

So for example, if I sent name = "whatever" and name1 = "somethin" and
reg = null to the server then when I check the value of name on the server it
gives me "whateversomthin". Everything up to the null terminator.

Is there any way to fix this problem? I can't rewrite the server so I have to
figure out something on the Java client.

Thanks,
Mack
 
J

Joseph Millar

I'm writing a Java client that connects to a C Server. The C server has a
struct like this:

typedef struct {
TEXT name[8],
name1[8],
reg
;
}mystruct;

TEXT is a char.

When I read this from the server, it's sending me 17 bytes as I expected.
Everything works fine that way. The problem is when I send the 17 bytes back
to the server. The fields are set with the correct values but the strings are
no longer null terminated.

So for example, if I sent name = "whatever" and name1 = "somethin" and
reg = null to the server then when I check the value of name on the server it
gives me "whateversomthin". Everything up to the null terminator.

Is there any way to fix this problem? I can't rewrite the server so I have to
figure out something on the Java client.

You can't. char name[8] in C means a maximum of 7 chars if you
are going to use it to hold a C-style string (7 chars plus '\0').
If the server expect name and name1 to contains null terminated
strings, you are limited to a max of 7 chars with one null each.
Oh, and each of those chars better fit in one byte, or you're
going to have further space problems.

If the intent of the server was to provide 8 character null
terminated names, then someone made a mistake with that struct.
They should have been defined as name[9], etc. This is why
you see things like "sizeof(name)-1" commonly in C.

--Joe
 
A

Adam Maass

Michael Winter said:
The solution appears to be simple (I could be completely wrong, so hold
on...).

It appears that every time you send strings to the server, you're using all
eight elements of the character arrays. If you have eight elements and the
last MUST be a null character, limit the strings to seven characters at a
maximum. The point to note is that the null terminator MUST appear in one
of the eight elements - it seems that you are expecting it to exist
somewhere off the end of the array.

Try it...

Michael

Yup, exactly.


// this is the output stream (byte-oriented) that you are using to talk to
your server.
OutputStream stream = ...;

// this is the text you want to send.
String textToSend = ...;

// note that Java strings are sequences of Unicode
// two-byte values; you need to get a sequence of
// ASCII one-byte values
byte[] bytes = textToSend.getBytes("ASCII");

// write at most 7 bytes from the string
int i = 0;
while(i < 7)
{
stream.write(bytes);
i++;
}

// pad with null bytes to make 8
while(i < 8)
{
stream.write((byte)0);
i++;
}


-- Adam Maass
 
R

Roedy Green

When I read this from the server, it's sending me 17 bytes as I expected.
Everything works fine that way. The problem is when I send the 17 bytes back
to the server. The fields are set with the correct values but the strings are
no longer null terminated.

You need to write bytes not chars, and deal installing or detecting
null terminators since Java uses lengths instead.

If you have ints, likely they will be littleEndian. See
http://mindpord.com/jgloss/endian.html

There are two approaches, using LeDataStream to produce exactly what C
likes, or invent a new format compatible to both, e.g. CSV.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top