aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Portable socket communication between Java and other languages (e.g. C++) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Portable socket communication between Java and other languages (e.g. C++)" Watch "Portable socket communication between Java and other languages (e.g. C++)" New topic
Author

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

Daniel Zuckermann
Ranch Hand

Joined: May 14, 2012
Posts: 50
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
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Daniel Zuckermann
Ranch Hand

Joined: May 14, 2012
Posts: 50
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
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39

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
 
subject: Portable socket communication between Java and other languages (e.g. C++)
 
Similar Threads
c# socket and java socket
Use of sockets through RMI
IO Streams and sockets
Java web application communication and call back with C/C++ programming language API
C++ and JAVA process communication