Question:
I want to connect a Java client program to a C/C++ server application
using sockets. What is the easiest way of doing this?
Answer:
Java provides the ability to write platform independent programs that
will run on any machine. TCP/IP provides the ability for programs to
communicate with each other over a network without regard to their
native platforms. Java provides the ability to communicate with other
machines via TCP/IP through the java.net package. The Socket class is
used to establish TCP connections from a Java program to another
program on the network. Communication is treated as a full-duplex
stream of bytes from one end to the other. Every byte you write to
the stream is read from the other end in the same order that it was
written. Likewise, every byte you read from the stream is returned in
the same order that it was written by the other end. Use
getInputStream() and getOutputStream() to read and write to a Socket.
Different hardware platforms represent primitive data types using
different byte orderings and bit lengths. For example, current Intel
processors store integers in a 32-bit little-endian format while some
of the latest SPARC processors use a 64-bit big-endian format.
Therefore, in order for different hardware platforms to communicate
over a network, they need to agree upon a data representation
format. Homogeneous distributed systems may opt to use native
representation formats to avoid converting to and from a network
byte-ordering.
However, the Internet is a heterogeneous system, and
the generally accepted format for transmitting integers over the
network is in a 32-bit big-endian format. More detailed standards
exist, such as XDR (external data representation), which is used
primarily to exchange parameters and results via remote procedure
calls. The end result, is that when you interface a Java client with
a non-Java server, you need to understand the data representation
convention used by the server and how to convert that to Java data
types. 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 Time
protocol 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;
}
}
}