Example: Using sockets for interprocess communication

This example uses sockets to communicate between a Java™ program and a C program.

You should start the C program first, which listens on a socket. Once the Java program connects to the socket, the C program sends it a string by using that socket connection. The string that is sent from the C program is an American Standard Code for Information Interchange (ASCII) string in codepage 819.

The Java program should be started using this command, java TalkToC xxxxx nnnn on the Qshell Interpreter command line or on another Java platform. Or, enter JAVA TALKTOC PARM(xxxxx nnnn) on the IBM® i command line to start the Java program. xxxxx is the domain name or Internet Protocol (IP) address of the system on which the C program is running. nnnn is the port number of the socket that the C program is using. You should also use this port number as the first parameter on the call to the C program.

Source code for TalkToC client Java class

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
import java.net.*;
import java.io.*;
 
class TalkToC
{
   private String host = null;
   private int port = -999;
   private Socket socket = null;
   private BufferedReader inStream = null;
 
   public static void main(String[] args)
   {
      TalkToC caller = new TalkToC();
      caller.host = args[0];
      caller.port = new Integer(args[1]).intValue();
      caller.setUp();
      caller.converse();
      caller.cleanUp();
   } 
 
   public void setUp()
   {
      System.out.println("TalkToC.setUp() invoked");
 
      try
      {
         socket = new Socket(host, port);
         inStream = new BufferedReader(new InputStreamReader(
                                       socket.getInputStream()));
      }
      catch(UnknownHostException e)
      {
         System.err.println("Cannot find host called: " + host);
         e.printStackTrace();
         System.exit(-1);
      }
      catch(IOException e)
      {
         System.err.println("Could not establish connection for " + host);
         e.printStackTrace();
         System.exit(-1);
      }
   } 
 
   public void converse()
   {
      System.out.println("TalkToC.converse() invoked");
 
      if (socket != null && inStream != null)
      {
         try
         {
            System.out.println(inStream.readLine());
         }
         catch(IOException e)
         {
            System.err.println("Conversation error with host " + host);
            e.printStackTrace();
         }
      } 
   } 
 
   public void cleanUp()
   {
      try
      {
         if (inStream != null)
            inStream.close();
         if (socket != null)
            socket.close();
      } 
      catch(IOException e)
      {
         System.err.println("Error in cleanup");
         e.printStackTrace();
         System.exit(-1);
      }
   } 
}

SockServ.C starts by passing in a parameter for the port number. For example, CALL SockServ '2001'.

Source code for SockServ.C server program

Note: Read the Code example disclaimer for important legal information.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <sys/time.h>
 
void main(int argc, char* argv[])
{
   int    portNum = atoi(argv[1]);
   int    server;
   int    client;
   int    address_len;
   int    sendrc;
   int    bndrc;
   char*  greeting;
   struct sockaddr_in  local_Address;
   address_len = sizeof(local_Address);
 
   memset(&local_Address,0x00,sizeof(local_Address));
   local_Address.sin_family = AF_INET;
   local_Address.sin_port = htons(portNum);
   local_Address.sin_addr.s_addr = htonl(INADDR_ANY);
 
   #pragma convert (819)
   greeting = "This is a message from the C socket server.";
   #pragma convert (0)
 
   /*  allocate socket    */
   if((server = socket(AF_INET, SOCK_STREAM, 0))<0)
   {
      printf("failure on socket allocation\n");
      perror(NULL);
      exit(-1);
   }
 
   /* do bind   */
   if((bndrc=bind(server,(struct sockaddr*)&local_Address, address_len))<0)
   {
     printf("Bind failed\n");
     perror(NULL);
     exit(-1);
   }
 
   /* invoke listen   */
   listen(server, 1);
 
   /* wait for client request */
   if((client = accept(server,(struct sockaddr*)NULL, 0))<0)
   {
     printf("accept failed\n");
     perror(NULL);
     exit(-1);
   }
 
   /* send greeting to client    */
   if((sendrc = send(client, greeting, strlen(greeting),0))<0)
   {
      printf("Send failed\n");
      perror(NULL);
      exit(-1);
   }
 
   close(client);
   close(server);
}