Interplatform Networking

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;    }  }}
devx-admin

devx-admin

Share the Post:
Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security.

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists

AI Tool

Unleashing AI Power with Microsoft 365 Copilot

Microsoft has recently unveiled the initial list of Australian clients who will benefit from Microsoft 365 (M365) Copilot through the exclusive invitation-only global Early Access Program. Prominent organizations participating in

Microsoft Egnyte Collaboration

Microsoft and Egnyte Collaboration

Microsoft has revealed a collaboration with Egnyte, a prominent platform for content cooperation and governance, with the goal of improving real-time collaboration features within Microsoft 365 and Microsoft Teams. This

Best Laptops

Top Programming Laptops of 2023

In 2023, many developers prioritize finding the best laptop for programming, whether at home, in the workplace, or on the go. A high-performing, portable, and user-friendly laptop could significantly influence

Renaissance Gaming Magic

AI Unleashes A Gaming Renaissance

In recent times, artificial intelligence has achieved remarkable progress, with resources like ChatGPT becoming more sophisticated and readily available. Pietro Schirano, the design lead at Brex, has explored the capabilities

New Apple Watch

The New Apple Watch Ultra 2 is Awesome

Apple is making waves in the smartwatch market with the introduction of the highly anticipated Apple Watch Ultra 2. This revolutionary device promises exceptional performance, robust design, and a myriad

Truth Unveiling

Unveiling Truths in Bowen’s SMR Controversy

Tony Wood from the Grattan Institute has voiced his concerns over Climate and Energy Minister Chris Bowen’s critique of the Coalition’s support for small modular nuclear reactors (SMRs). Wood points

Avoiding Crisis

Racing to Defy Looming Financial Crisis

Chinese property developer Country Garden is facing a liquidity challenge as it approaches a deadline to pay $15 million in interest associated with an offshore bond. With a 30-day grace

Open-Source Development

Open-Source Software Development is King

The increasingly digital world has led to the emergence of open-source software as a critical factor in modern software development, with more than 70% of the infrastructure, products, and services

Home Savings

Sensational Savings on Smart Home Security

For a limited time only, Amazon is offering massive discounts on a variety of intelligent home devices, including products from its Ring security range. Running until October 2 or while

Apple Unleashed

A Deep Dive into the iPhone 15 Pro Max

Apple recently unveiled its groundbreaking iPhone 15 Pro and iPhone 15 Pro Max models, featuring a revolutionary design, extraordinary display technology, and unrivaled performance. These new models are the first