Get Response from Thales Simulator using Java

Apr 13, 2011 at 12:25 PM

I am using Java to connect to the Thales Simulator on OpenSUSE 11.4 and have used socket to connect to it.

The code is as follows :

        Socket socket = new Socket("127.0.0.1", 9998);
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        BufferedOutputStream buffered = new BufferedOutputStream(out, 1024);
        String command = "1011JA12345678901210";
        byte[] inBuffer = ("  " + command).getBytes();
        inBuffer[0] = (byte) (command.length() / 256);
        inBuffer[1] = (byte) (command.length() % 256);
        System.out.println((int) inBuffer[1]);
        buffered.write(inBuffer);
        buffered.flush();

This code reaches the simulator and it shows in the "Command Events" the output..but now how to i get the output in my java console. Please help asap.

Coordinator
Apr 13, 2011 at 3:43 PM

It's been a long time since I've directly dealt with socket programming in Java. I do recall that once you write to the socket you should then read from the socket to get a response.

Apr 13, 2011 at 8:09 PM

Yes. I figured it out. The socket writes the output bytes in the inBuffer itself.

Jun 6, 2011 at 6:19 PM

Do you have a Java sample code ?  I am working on a project where I need to connect to Thales HSM 8000 and run some commands.  If you have a sample code of how to connect to the HSM, maybe I see it ?

Thanks

Jun 8, 2011 at 3:27 PM

The code above is the code that connects to the simulator. Its just that if you are looking for the output its written inside the "inBuffer" variable itself.

Jul 14, 2011 at 9:03 PM

Hi, I am newbie to HSM. We are using HSM 8000 Device using Java language.

As per the above Post, the response from the HSM will be in the inBuffer variable itself. When i try to print the inBuffer variable, its giving the same value which i sent to the HSM, but not the response code ND.

 Please provide your help, thanks in advance.

Here is my sample code:

     socket = new Socket(ipAddress, port);
    System.out.println("<<< Sockt s >>> :" + socket);
    if (socket != null) { System.out.println("<<< Connected to HSM  >>>:" + socket.isConnected());
    in = socket.getInputStream();
    out = socket.getOutputStream();
          BufferedOutputStream buffered = new BufferedOutputStream(out, 1024);
          command = "0000NC";
          byte[] inBuffer = ("  " + command).getBytes();
          inBuffer[0] = (byte) (command.length() / 256);
          inBuffer[1] = (byte) (command.length() % 256);
          System.out.println((int) inBuffer[1]);
          buffered.write(inBuffer);
          buffered.flush();
          int i=0;
          System.out.print("Response : ");
          while(i<inBuffer.length)
          {
           System.out.print((char)inBuffer[i]);
           i++;
          }

 

Output:

bash-3.2$ <<< Main Method Entry >>>
<<< Given Ip ............ : 10.3.245.67
<<< Given Port ............ : 3675
<<< Sockt s >>> :Socket[addr=/10.3.245.67,port=3675,localport=57004]
<<< Connected to HSM  >>>:true
6
Response : 0000NC

 

Sep 6, 2011 at 4:02 PM

hsmsignin, did you get the code working ?  I am looking to do exactly the same.  If you got it working, can I see how you do it ?

Thanks

Sep 6, 2011 at 4:37 PM

Here you go:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class TestHSMJava {

 public static void main(String args[]) {

  System.out.println("<<< Main Method Entry >>>");
  System.out.println("<<< Given Ip ............ : " + args[0]);
  String ipAddress = args[0];
  System.out.println("<<< Given Port ............ : " + args[1]);
  int port = Integer.parseInt(args[1]);
  String command = null;
  Socket socket = null;
  DataOutputStream out = null;
  DataInputStream in = null;
  byte[] b= new byte[100];
  try {
    socket = new Socket(ipAddress, port);
    System.out.println("<<< Sockt s >>> :" + socket);
    if (socket != null) {
     System.out.println("<<< Connected to HSM  >>>:"
       + socket.isConnected());
    in = new DataInputStream (new BufferedInputStream(socket.getInputStream()));
    out = new DataOutputStream (new BufferedOutputStream(socket.getOutputStream()));
          command = "00043302        NC";
          out.writeUTF(command);
          System.out.println("Input to HSM : " +command);
          out.flush();
          String response = in.readUTF();
          System.out.println("Output from HSM : " +response);
          System.out.println("");
   }
  } catch (Exception ex) {
   try {
    socket.close();
   } catch (Exception e) {
   }
   System.out.println("<<< Exception Message :: >>" + ex.getMessage());
   ex.printStackTrace();
  } finally {
   try {
    out.close();
    in.close();
    System.out.println("<<< Out/In Stream Closed>>>");
    socket.close();
    System.out.println("<<< Socket Closed>>>");
   } catch (Exception exception) {
    exception.printStackTrace();
   }
  }
 }

}

 

Output:

bash-3.2$ <<< Main Method Entry >>>

<<< Given Ip ............ : 182.33.45.123

<<< Given Port ............ : 9876

<<< Sockt s >>> :Socket[addr=/182.33.45.123,port=9876,localport=62924]

<<< Connected to HSM >>>:true

Input to HSM : 00043302        NC

Output from HSM : 00043302 ND0065267200000000001113-0803

<<< Out/In Stream Closed>>>

<<< Socket Closed>>>

Sep 6, 2011 at 6:22 PM
Edited Sep 6, 2011 at 6:45 PM

Thanks very much for the code.  I got the output correctly.

 

Sep 6, 2011 at 6:53 PM

make sure to specify 16 characters before the HSM command(in this case NC). as i specified in my program, command = "00043302        NC";.

i specified the characters as 00043302 + 8 spaces.

Sep 6, 2011 at 7:27 PM

It doesn't like blank spaces for some reason.  I specified the characters as 1234 + 12 spaces and I got :

Output from HSM : 1234 !67

So I took out the spaces and specified 1234NC and I got the output correctly:

Output from HSM : 1234ND0026860400000000001110-0870

 

Aug 22, 2012 at 8:05 PM

I am trying to use your code to send a command which contains HEX data for example:

.Ô1004ZG4A5ADBEA51E41D982AA4603731DFDF6D1080ˆþ¤áŒJQQìð..èc¡0Ï.uJ6–î.ØÄ$Ï.ÐRSm.ú#yç.A£aÔ33´O`..Ø·Ã..ŽÅÐd."$.?³.s5_‡Ì%Dž?M±Ê´$;200.ÈP.....á.<...G.€.€...........X¯....."é§ÈóÛsš»ï¶¥.›¦Ú?R@¡¼ae½Ÿ.9XVt.H¼š¸áÞÌm-jLs...„.ÆŒïÔ(ÀÇå‘·/54Œ.ß‚<Ðk.AµmŸ.ï¬.Sgr¸Fµž—.Í.€Û5·¸`_£3³"ì#µ‚|yCü–Áôò7$›£Ig.T’L.´ã™¿F>X.Môhf.R]Œ.Âîo. ².v.BîŠ.VÅ.Ç.W:ñnN!;(õæ‘0.†..€ãF¹†„x.ç.ø¬ðþ³ÿ.%;g.W1¬XÌ.TKMÓ{.½¹ûÐi?kÂM/šÎ÷$Õ@BÔ.9ÒóÓ._.<Ó.«Ý...>..¢.‚ô@.›–žû.ópð/ümô†.”¦@8.±«¹À¶Þô5oÖ..b>âµ'kŸ..á.×ÿ6'Ÿs™Qœ¼A... which is equal to 0X 01 D4 31 30 30 34 5A 47 34 41 35 41 44 42 45 41 35 31 45 34 31 44 39 38 32 41 41 34 36 30 33 37 33 31 44 46 44 46 36 44 31 30 38 30 88 FE A4 E1 8C 4A 51 51 EC F0 0C 19 E8 63 A1 30 CF 0F 75 4A 36 96 EE 09 D8 C4 24 CF 9D D0 52 53 6D 2E FA 23 79 E7 09 41 A3 61 D4 33 33 B4 4F 60 AD 04 D8 B7 C3 0E 15 8E C5 D0 64 02 22 24 11 3F B3 1F 73 35 5F 87 CC 25 44 9E 3F 4D B1 CA B4 24 3B 32 30 30 00 C8 50 01 00 00 00 05 E1 2E 3C 02 03 02 47 00 80 00 80 00 00 01 03 00 00 00 01 12 00 00 58 AF 01 01 15 00 00 22 E9 A7 C8 F3 DB 73 9A BB EF B6 A5 1E 9B A6 DA 3F 52 40 A1 BC 61 65 BD 9F 11 39 58 56 74 0A 48 BC 9A B8 E1 DE CC 6D 2D 6A 4C 73 03 AD 1B 84 1E C6 8C EF D4 28 C0 C7 E5 91 B7 2F 35 34 8C 1B DF 82 3C D0 6B 01 41 B5 6D 9F 15 EF AC 0A 53 67 72 B8 46 B5 9E 97 1F CD 0F 80 DB 35 B7 B8 60 5F A3 33 B3 22 EC 23 B5 82 7C 79 43 FC 96 C1 F4 F2 37 24 9B A3 49 67 0D 54 92 4C 0F B4 E3 99 BF 46 3E 58 1E 4D F4 68 66 2E 52 5D 8C 19 C2 EE 6F 7F 20 B2 16 76 04 42 EE 8A 0F 56 C5 19 C7 0F 57 3A F1 6E 4E 21 3B 28 F5 E6 91 30 81 86 02 81 80 E3 46 B9 86 84 78 13 E7 09 F8 AC F0 FE B3 FF 0F 25 3B 67 1F 57 31 AC 58 CC 08 54 4B 4D D3 7B 01 BD B9 FB D0 69 3F 6B C2 4D 2F 9A CE F7 24 D5 40 42 D4 81 39 D2 F3 D3 14 5F 00 3C D3 04 AB DD 0A 2E AD 3E 18 1B A2 05 82 F4 40 2E 9B 96 9E FB 17 F3 70 F0 2F FC 6D F4 86 04 94 A6 40 38 00 B1 AB B9 C0 B6 DE F4 35 6F D6 00 18 62 3E E2 B5 27 6B 9F 1C 13 E1 1D D7 FF 36 27 9F 73 99 51 9C BC 41 02 01 03,

what I have to change to do this.