Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > C to Java socket input question

Reply
Thread Tools

C to Java socket input question

 
 
Richard
Guest
Posts: n/a
 
      07-16-2003
Level: Java newbie, C experienced
Platform: Linux and Win32, Intel

Another programmer and I are working on a small project together.
He's writing a server process in Java that accepts input from
processes I've written over a TCP connection. My processes are all
written in C; his are all done in Java. He's new to Java, and I've
never really used it.

My input is basically a stream of 32-bit unsigned integers (e.g., the
low-level form of IP addresses) along with the occasional fixed-
length char array.

We're starting to spin our wheels about how to present input to his
server, since there's no native 32-bit unsigned int in Java (so far
as I understand it). He's game, but is dealing with learning Java,
and we're at the point where we have to get past this issue and on to
other parts of what we're building. I'm not getting a very clear
picture of what the Java-side problem is, and I'm at the point where
I'll take on the obligation of learning what the problem is, and
doing any necessary data conversion, if that's what it takes to get
past this.

Any references to, or discussion of, this kind of IO problem would be
greatly appreciated.

--
For email, put NOT SPAM in Subject or I'll probably miss it.
<><
 
Reply With Quote
 
 
 
 
David Zimmerman
Guest
Posts: n/a
 
      07-16-2003


Richard wrote:
> My input is basically a stream of 32-bit unsigned integers (e.g., the
> low-level form of IP addresses) along with the occasional fixed-
> length char array.
>


Treat it as a 4 byte array. InetAddress.getByAddress() wants a byte[]
anyway.

Or put it in a signed 32 bit integer and never do arithmetic with it,
the bits are the same regardless of how they're interpretted.

Either way, mind the endienness of the data. Best idea here would be to
get the C porogram to put everything in network order, the same as Java's.

 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      07-16-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote...
>
> Richard wrote:
> > My input is basically a stream of 32-bit unsigned integers (e.g., the
> > low-level form of IP addresses) along with the occasional fixed-
> > length char array.

>
> Treat it as a 4 byte array. InetAddress.getByAddress() wants a byte[]
> anyway.
>
> Or put it in a signed 32 bit integer and never do arithmetic with it,
> the bits are the same regardless of how they're interpretted.


Sorry for being brain-dead, but will he be able to do arithmetic on
it if it's in a 4-byte array? As in: unsigned char fbarray[4] ; ?

> Either way, mind the endienness of the data. Best idea here would be to
> get the C porogram to put everything in network order, the same as Java's.


Already been bit by that one.

--
For email, put NOT SPAM in Subject or I'll probably miss it.
<><
 
Reply With Quote
 
=?ISO-8859-15?Q?Thomas_Gagn=E9?=
Guest
Posts: n/a
 
      07-16-2003
Don't send binary data. Send it in text. Both C and Java understand text and
you don't have to worry about network byte ordering or whether your CPUs are
big or little-endian.

Or you could use some middleware to make your job easier.


--
..tom
remove dashes in email for replies
open source middleware at http://isectd.sourceforge.net

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-16-2003
(E-Mail Removed) wrote...
> Richard wrote:
>
> > My input is basically a stream of 32-bit unsigned integers (e.g., the
> > low-level form of IP addresses)

>
> Ummm.... Maybe. Or maybe not.
>
> 32-bit is *one* form of an IP address. Another is 128-bit addresses in IPv6.
>
> It's best that you account for both.


<Sound of hand slapping forehead>

Yes, that's true.

> see java.net.InetAddress, java.net.Inet4Address and
> java.net.Inet6Address for more.
>
> > along with the occasional fixed-
> > length char array.

>
> Fixed length char array? Or string? Logically which do you have?


String of fixed length.

> And what encoding? If you want to avoid many-to-many complexity
> nightmares, it's probably best to use UTF-8.


Oui. Of course.

> Hmmm..... so maybe fixed might not be as robust.


> > We're starting to spin our wheels about how to present input to his
> > server, since there's no native 32-bit unsigned int in Java (so far
> > as I understand it).

>
> But an inet address is not really a 32-bit unsigned int.


??? Unless you're referring to IPV6,

in_addr_t => uint32_t (<netinet/in.h>) => "unsigned 32-bit int"

> So you can just
> use Java's int, which is 32-bit signed. No problems.


> > Any references to, or discussion of, this kind of IO problem would be
> > greatly appreciated.

>
> Hmmmm.... you could search these newsgroups in Google or some such for
> any posts where I've touched on this (I do when it comes up).
>
> a few off hand:
> Message-ID: <(E-Mail Removed)>
> Message-ID: <(E-Mail Removed)>
> Message-ID: <(E-Mail Removed)_SPAM.ne t>
> Message-ID: <(E-Mail Removed)>
> Message-ID: <(E-Mail Removed)>
> Message-ID: <(E-Mail Removed)>


Thank you.

--
For email, put NOT SPAM in Subject or I'll probably miss it.
<><
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      07-16-2003
Richard wrote:
> Level: Java newbie, C experienced
> Platform: Linux and Win32, Intel
>
> Another programmer and I are working on a small project together.
> He's writing a server process in Java that accepts input from
> processes I've written over a TCP connection. My processes are all
> written in C; his are all done in Java. He's new to Java, and I've
> never really used it.
>
> My input is basically a stream of 32-bit unsigned integers (e.g., the
> low-level form of IP addresses) along with the occasional fixed-
> length char array.
>
> We're starting to spin our wheels about how to present input to his
> server, since there's no native 32-bit unsigned int in Java (so far
> as I understand it). He's game, but is dealing with learning Java,
> and we're at the point where we have to get past this issue and on to
> other parts of what we're building. I'm not getting a very clear
> picture of what the Java-side problem is, and I'm at the point where
> I'll take on the obligation of learning what the problem is, and
> doing any necessary data conversion, if that's what it takes to get
> past this.
>
> Any references to, or discussion of, this kind of IO problem would be
> greatly appreciated.
>


Why not just read as a stream of bytes and use
InetAddress.getByAddress( byte[] )
to convert into a format which can be conveniently
used by various Java methods?
So while "there's no native 32-bit unsigned in in Java"
there IS this wonderful class called InetAddress which
encapsulates IP addresses.
Drop me a line if you need some sample code.

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-16-2003
(E-Mail Removed) wrote...

> [Since this reply is all java I've removed comp.lang.c from the post]
>
> It's not a particularly difficult problem in Java. The class DataInputStream
> almost does it; it has methods readUnsignedByte() and readUnsignedShort() -
> quite why they didn't add a readUnsignedInt() I don't know.
>
> But you could easily extend DataInputStream and add a readUnsignedInt()
> method which returns a long. All you have to do then is open a Socket and
> wrap its InputStream with your extended DataInputStream. To read unsigned
> ints you just invoke readUnsignedInt() on the extended DataInputStream.
>
> If you want to use unsigned int's in Java, WBEM services have developed some
> classes. You can find out more at http://wbemservices.sourceforge.net/


Let me crack out the Java doc now that I've got a good place to look.
Thanks very much for the WBEM services link, too.

--
For email, put NOT SPAM in Subject or I'll probably miss it.
<><
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-16-2003
On Wed, 16 Jul 2003 12:53:55 GMT, David Zimmerman
<(E-Mail Removed)> wrote or quoted :

>Either way, mind the endienness of the data. Best idea here would be to
>get the C porogram to put everything in network order, the same as Java's.


An alternative is to use LEDataInputStream on the Java end to read
little endian data or use the nio classes.

see http://mindprod.com/jgloss/endian.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      07-16-2003
On Wed, 16 Jul 2003 11:15:47 +0000, Richard wrote:

> Level: Java newbie, C experienced
> Platform: Linux and Win32, Intel
>
> Another programmer and I are working on a small project together. He's
> writing a server process in Java that accepts input from processes I've
> written over a TCP connection. My processes are all written in C; his
> are all done in Java. He's new to Java, and I've never really used it.
>
> My input is basically a stream of 32-bit unsigned integers (e.g., the
> low-level form of IP addresses) along with the occasional fixed- length
> char array.
>
> We're starting to spin our wheels about how to present input to his
> server, since there's no native 32-bit unsigned int in Java (so far as I
> understand it). He's game, but is dealing with learning Java, and we're
> at the point where we have to get past this issue and on to other parts
> of what we're building. I'm not getting a very clear picture of what
> the Java-side problem is, and I'm at the point where I'll take on the
> obligation of learning what the problem is, and doing any necessary data
> conversion, if that's what it takes to get past this.
>
> Any references to, or discussion of, this kind of IO problem would be
> greatly appreciated.


Well, you have already had some good responses, but let me add my own
opinion anyway...

Start by defining the protocol between the machines. Do this by defining
the byte stream in terms of bytes, not any higher structure, since TCP
does not inherently support any higher structure. If you MUST send larger
structures such as IP addresses (yes I know you must), define how they
will be sent byte-by-byte. E.g. an IPv4 address goes as 4 bytes, in
the following order: 1.2.3.4 is sent as 0x01, 0x02, 0x03, 0x04.
This is how the RFCs define their protocols.

Now each of you can go about assembling and disassembling these messages
in the way that seems most natural to you and your language. This may well
turn out to be that the C code mainly handles addresses as unsigned ints
but the java end handles them as byte[4]. You can ignore each other's
implementation and concentrate on meeting the network spec.

By the way, NEVER send structures from C like this:
socket.write(myStruct, sizeof myStruct);
This can introduce hidden padding, and does not specify which order things
will be sent in. Always specify the byte stream format, and work to that.

Steve
 
Reply With Quote
 
Jordan Zimmerman
Guest
Posts: n/a
 
      07-16-2003
Send it as a String. You have a problem sitting there in C as well.
Different machine types have different long int encodings. It's much safer
to send a String.

--
Jordan Zimmerman
http://www.jordanzimmerman.com

"Richard" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments