File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Portable socket communication between Java and other languages (e.g. C++)

 
Daniel Zuckermann
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is important if one wants to send data between a Java socket and a socket that is written in another language, maybe C++? Of course I can't use any classes that write Java-specific code into the stream and I must also define a charset that sender and receiver use. At the beginning it would be sufficient to use text data, so I don't have to care about number representation.
Which Java classes can be used to communicate with sockets that don't use Java?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Zuckermann wrote:What is important if one wants to send data between a Java socket and a socket that is written in another language, maybe C++? Of course I can't use any classes that write Java-specific code into the stream and I must also define a charset that sender and receiver use. At the beginning it would be sufficient to use text data, so I don't have to care about number representation.
Which Java classes can be used to communicate with sockets that don't use Java?


The main classes for TCP -- ServerSocket and Socket -- uses input and output streams of bytes... and should have no issues talking to TCP sockets implemented with other languages.

The main classes for UDP -- DatagramSocket and MulticastSocket -- uses DatagramPacket, which in turn, uses byte arrays -- and should have no issues talking to UDP sockets implemented with other languages.

Henry
 
Daniel Zuckermann
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, sheriff. Ok, InputStream and OutputStream read and write pure bytes and are most flexible. There are many wrapper classes, e.g. Object-I/O-Stream, Buffered-I/O-Stream, Checked-I/O-Stream, Cipher-I/O-Stream and Data-I/O-Stream but as far as I know they are all Java-specific. For this reason I can't write data to a stream like this and send it to a C++ socket:


Is my understanding correct? My goal is just to send some simple data from a Java socket to a C++ socket and vice versa and for the sake of simplicity I want to encode numbers as text, so I don't have to deal with any number representation issues.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Zuckermann wrote:Thank you, sheriff. Ok, InputStream and OutputStream read and write pure bytes and are most flexible. There are many wrapper classes, e.g. Object-I/O-Stream, Buffered-I/O-Stream, Checked-I/O-Stream, Cipher-I/O-Stream and Data-I/O-Stream but as far as I know they are all Java-specific. For this reason I can't write data to a stream like this and send it to a C++ socket:


Is my understanding correct? My goal is just to send some simple data from a Java socket to a C++ socket and vice versa and for the sake of simplicity I want to encode numbers as text, so I don't have to deal with any number representation issues.


The buffered stream is just buffering -- it doesn't change the data stream. DataOutputStream writes in network byte order -- so if the C++ side also uses network byte order, it should work fine.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic