Interplatform Networking

Interplatform Networking

I want to connect a Java client program to a C/C++ server applicationusing sockets. What is the easiest way of doing this?

Java provides the ability to write platform independent programs thatwill run on any machine. TCP/IP provides the ability for programs tocommunicate with each other over a network without regard to theirnative platforms. Java provides the ability to communicate with othermachines via TCP/IP through the package. The Socket class isused to establish TCP connections from a Java program to anotherprogram on the network. Communication is treated as a full-duplexstream of bytes from one end to the other. Every byte you write tothe stream is read from the other end in the same order that it waswritten. Likewise, every byte you read from the stream is returned inthe same order that it was written by the other end. UsegetInputStream() and getOutputStream() to read and write to a Socket.

Different hardware platforms represent primitive data types usingdifferent byte orderings and bit lengths. For example, current Intelprocessors store integers in a 32-bit little-endian format while someof the latest SPARC processors use a 64-bit big-endian format.Therefore, in order for different hardware platforms to communicateover a network, they need to agree upon a data representationformat. Homogeneous distributed systems may opt to use nativerepresentation formats to avoid converting to and from a networkbyte-ordering.

However, the Internet is a heterogeneous system, andthe generally accepted format for transmitting integers over thenetwork is in a 32-bit big-endian format. More detailed standardsexist, such as XDR (external data representation), which is usedprimarily to exchange parameters and results via remote procedurecalls. The end result, is that when you interface a Java client witha non-Java server, you need to understand the data representationconvention used by the server and how to convert that to Java datatypes. The hangups involved in doing this are what make CORBA, XML, and RMI so attractive for building distributed systems. They remove data representation issues from the programmer.

I’ve provided a simple client program example that connects to a Timeprotocol server (IETF RFC 868), reads the protocol-defined data,converts it to a Java data type, and prints out the result.

import*;import*;import java.util.*;public class rdate {  public static final int TIME_PORT            = 37;  public static final String DEFAULT_TIME_HOST = "";  /***   * The number of seconds between 00:00 1 January 1900 and   * 00:00 1 January 1970.  This value can be useful for converting   * time values to other formats.   */  public static final long SECONDS_1900_TO_1970 = 2208988800L;  public static void main(String[] args) {    String timehost = DEFAULT_TIME_HOST;    long seconds;    DataInputStream input;    Socket socket;    Date date;    if(args.length > 0)       timehost = args[0];    try {      socket = new Socket(timehost, TIME_PORT);      input   = new DataInputStream(socket.getInputStream());      // Retrieve the time from the server.  The time      // is the number of seconds since 00:00 (midnight)      // January 1900 GMT, as specified by RFC 868.  This reads      // the raw 32-bit big-endian unsigned integer from the server and      // converts it to a Java long.      seconds = (((long)input.readInt()) & 0xffffffffL);      input.close();      date = new Date((seconds - SECONDS_1900_TO_1970)*1000L);      System.out.println(date.toString());    } catch(IOException e) {      e.printStackTrace();      return;    }  }}

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist