Re: reading ints from a socket
From: Irrwahn Grausewitz (irrwahn33_at_freenet.de)
Date: 03/14/04
- Next message: Nick Maclaren: "Re: concurrent process vs. parallel process"
- Previous message: Irrwahn Grausewitz: "Re: need the file size via windows"
- In reply to: Jon A. Cruz: "Re: reading ints from a socket"
- Next in thread: Jon A. Cruz: "Re: reading ints from a socket"
- Reply: Jon A. Cruz: "Re: reading ints from a socket"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 14 Mar 2004 12:04:55 +0100
"Jon A. Cruz" <jon@joncruz.org> wrote:
>Stu Mac wrote:
>> Hi there
>>
>> I have a ServerSocket in a java program and i connect to it using a
>> client socket written in C. The two sockets accept each other fine
>> but I have a wierd problem when trying to send a int across the
>> socket;
<snip>
>> I'm trying to read this into c using lines similar to:
>>
>> int var1
>> int buf[2];
>>
>> read(fd, buf, 1);
>>
>> var1 = buf[0]
>
>Eeeeek! Scary.
Indeed. :-)
<snip>
>int32_t var1;
>uint8_t buf[4];
>read(fd, buff, 4 );
>var1 = ????;
>
>Ahhh. Better.
No, not really. ;-)
>int32_t var1;
>uint8_t buf[4];
>read( fd, buff, 4 );
>var1 = (buf[0] << 24)
> |(buf[1] << 16)
> |(buf[2] << 8)
> |(buf[3] << 0);
Since this is cross-posted to comp.lang.c, the following applies:
Unfortunately all the code above is completely unportable and
is based on unjustified assumptions about the implementation.
Some hints:
1. Even if you happen to have a conforming C99 implementation
available (very unlikely), this very implementation is by no
means obliged to provide the exact width integer types
[u]intN_t.
2. read() is _not_ a function from the standard C library.
3. As far as C is concerned, bytes and octets are orthogonal
concepts. IOW, a byte need not necessarily consist of eight
bits. For example, it's not uncommon for a platform to
provide a 16-bit entity as the smallest addressable amount
of memory. (NB: in C chars are defined to be one byte in
size, with one byte consisting of _at least_ eight bits:
8, 9, 10, 12, 16, 42, 111 are all perfectly valid values of
CHAR_BIT (which is defined in limits.h).
There is probably still room for some more c.l.c pedantry, but
I'll leave it at that for now. ;-)
To OP:
- Cross-posting to groups concerned about different programming
languages is usually Not A Good Idea[tm]. However, I'm not
entirely sure, if this thread may be used as an example pro or
con this claim...
- Probably, the best stab (with C) to make at problems like
yours is to read data as unsigned chars and then interpret
according to your needs.
HTH
Regards
-- Irrwahn Grausewitz (irrwahn33@freenet.de) welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt clc faq-list : http://www.faqs.org/faqs/C-faq/faq/ acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
- Next message: Nick Maclaren: "Re: concurrent process vs. parallel process"
- Previous message: Irrwahn Grausewitz: "Re: need the file size via windows"
- In reply to: Jon A. Cruz: "Re: reading ints from a socket"
- Next in thread: Jon A. Cruz: "Re: reading ints from a socket"
- Reply: Jon A. Cruz: "Re: reading ints from a socket"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|