• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What IO classes can accept non-Java data types

 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a server program and I need to allow access to clients that might be written in another language. I looked around the API and nothing stood out.

Specifically, I need to be able to pass integral values and character arrays(in lieu of a Java String, since I doubt a Java String would accept a String from say a C++ program).

Does anyone have any ideas on how to go about it?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the java.net.Socket and java.net.ServerSocket classes should work fine. When writing, it just converts a String into a stream of ASCII characters. When reading, it builds a String from ASCII characters. The other side could be a C, C++, Perl, etc., program, as long as it supports the socket protocol.

Just stay away from Java specific protocols like RMI.

Henry
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

So I could use ObjectOutputStream and ObjectInputStream for this, if I just send, receive primitive data types? Since they serialize the data, would this work? DataInputStream specifies Java data types. Is something like ByteArrayInputStream closer to what I need?
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David McCombs:
Thanks

So I could use ObjectOutputStream and ObjectInputStream for this, if I just send, receive primitive data types? Since they serialize the data, would this work? DataInputStream specifies Java data types. Is something like ByteArrayInputStream closer to what I need?



No... You can't use ObjectInputStream or ObjectOutputStream (wrapping a socket), they are Java specific -- meaning they send other data in order for reconstruction on the other side. DataInputStream should work... but to be safe I would tested it with an actual C/C++ socket client or server.

Henry
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can't use Object*Streams. That's the one thing you absolutely can't do, as the format is completely Java-specific.

Henry suggested plain text; sending Strings as a sequence of ASCII characters, sending numbers as their text representation, etc (usually using CRLF sequences to separate inputs). This is how HTTP, FTP, SMTP, IMAP, etc work -- and if it's good enough for all the infrastructure of the Internet, it's good enough for you! So this would just involve receiving with a BufferedReader, and sending with a PrintWriter.

Data*Stream don't send Java-specific data, but they do send binary data -- non-human-readable data in a specific format. The data is sent in network byte order, which is a standard that's easy to support in other languages, especially C and its relatives. The only issue is that the binary data uses specific sizes for each data type: 4 bytes for an int, 8 for a double, 2 for a short -- which you'd have to match from C, and that isn't always easy. That's why ASCII (text) data is used for so many Internet protocols: it's insanely easy to do cross-platform data interchange that way.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


No... You can't use ObjectInputStream or ObjectOutputStream (wrapping a socket), they are Java specific -- meaning they send other data in order for reconstruction on the other side. DataInputStream should work... but to be safe I would tested it with an actual C/C++ socket client or server.

Henry



ObjectOutputStream does not wrap a Socket, but a OutputStream. Or am I way off here?

Ernest,

From the API on DataInputStream: A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

Is that consistent with your comments? Yes, it is binary data, but really what isn't?


Thank you to both of you for clearing it up for me.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ObjectOutputStream does not wrap a Socket, but a OutputStream. Or am I way off here?



Yes... an ObjectOutputStream wraps an OutputStream. Get the output stream to wrap from the socket object.... no wait... don't use ObjectOutputStream as explained eariler.

From the API on DataInputStream: A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.



Unfortunately, it is not completely machine independent, if one side is not Java.... Actually, it should work for most cases, as Java uses the same byte order as Intel.

In any case, if you just use strings, to send text, you should be fine.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


Unfortunately, it is not completely machine independent, if one side is not Java.... Actually, it should work for most cases, as Java uses the same byte order as Intel.



Actually, Java uses the opposite byte order as Intel: Java is "big-endian" and Intel processors are "little-endian." But as I said above, Java's byte order just happens to be the same as "network byte order", the informal standard used to encode binary data for transmission across a network from C code on UNIX. Most C libraries offer functions like "hton()" and "ntoh()" for translating between host byte order and network byte order. Therefore, byte order is a non-issue if the C program uses established conventions.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David McCombs:
Yes, it is binary data, but really what isn't?



Text isn't, by definition. To send the integer 1, you might send the single byte 0x31 (the ASCII code for the character 1) or you might send the four bytes 0x00000001. DataOutputStream will do the latter. For the number 1, the first one seems easier; but for the number 87687638, sending binary data takes only half the space.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:

Actually, Java uses the opposite byte order as Intel: Java is "big-endian" and Intel processors are "little-endian." But as I said above, Java's byte order just happens to be the same as "network byte order", the informal standard used to encode binary data for transmission across a network from C code on UNIX. Most C libraries offer functions like "hton()" and "ntoh()" for translating between host byte order and network byte order. Therefore, byte order is a non-issue if the C program uses established conventions.



Wow... Been wrong on this for more than a decade. Heck, I don't even remember who told me this anymore.

Henry
 
You can't expect to wield supreme executive power just because
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic