Interplatform Networking

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

Answer:
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 java.net 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 java.io.*;import java.net.*;import java.util.*;public class rdate {  public static final int TIME_PORT            = 37;  public static final String DEFAULT_TIME_HOST = "time.nist.gov";  /***   * 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;    }  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job