Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

Send data to socket from Java Card

JavaCard Applet Development Related Questions and Answers.
vicoandrej
Posts: 11
Joined: Thu Dec 30, 2021 5:18 am
Points :158
Contact:

Send data to socket from Java Card

Post by vicoandrej » Thu Dec 30, 2021 7:01 am

Trying to build solution that allows my Java Card send data via socket. Can Java Card communicate via TCP in general? I have function that can run in Java and trying to convert it to Java Card. I know String is not available in Java Card. What other things I should consider about?

Code: Select all

  public void sendSocket()
   {
        String hostname = "localhost";
        int port = 6789;
        
        Socket clientSocket = null;  
            DataOutputStream os = null;
            BufferedReader is = null;
            
            try {
                clientSocket = new Socket(hostname, port);
                os = new DataOutputStream(clientSocket.getOutputStream());
                is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: " + hostname);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: " + hostname);
            }
            
            
            if (clientSocket == null || os == null || is == null) {
            System.err.println( "Something is wrong. One variable is null." );
            return;
        }
        
        
         try {
            os.writeBytes(  "aaa" );
            os.close();
            is.close();
            clientSocket.close(); 
            
        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
              
            
   }

mbsysde99
Posts: 40
Joined: Tue Oct 24, 2017 1:10 pm
Points :484
Contact:

Re: Send data to socket from Java Card

Post by mbsysde99 » Sun Jan 16, 2022 2:10 pm

I also have same problem with you :roll:

vicoandrej
Posts: 11
Joined: Thu Dec 30, 2021 5:18 am
Points :158
Contact:

Re: Send data to socket from Java Card

Post by vicoandrej » Fri Jan 21, 2022 12:03 pm

Looks like it is not possible from JavaCard. They definitely don't have String and looks are not able to communicate on network.

mbsysde99
Posts: 40
Joined: Tue Oct 24, 2017 1:10 pm
Points :484
Contact:

Re: Send data to socket from Java Card

Post by mbsysde99 » Fri Jan 21, 2022 12:46 pm

As far as I know, you can send Apdu from the HOST to JCIDE via socket. The goal is to SIMULATE communication between the applet running on JCIDE (as virtual card) and the HOST (as reader). To debug the applet and host programs, and make sure that they both work the way you want, before you plug the applet into a real card, and the host connects to a real reader.

Look HOST at https://www.javacardos.com/javacardforu ... cide#p6038


But In REAL CARD, communication between CARD and READER is via CONTACT CARD (ICC/integrated circuit card) or CONTACTLESS CARD (NFC/Near Field Communication) with APDU command.
Last edited by mbsysde99 on Mon Jan 24, 2022 8:08 am, edited 1 time in total.

PeteSmithSIMs
Posts: 3
Joined: Mon Jan 17, 2022 12:52 pm
Points :90
Contact:

Re: Send data to socket from Java Card

Post by PeteSmithSIMs » Mon Jan 24, 2022 7:18 am

Hi.

The application that I'm working on does this, but it uses the mobile equipment to do the actual communication, and talks to it via PDUs.

The setup code that I've got is below. It's generic, and may not be valid Javacard code, but hopefully will give some pointers.

byte[] parameters = trCommsBuffer; // This buffer shouldn't be in use for anything at this stage
short idx = 0;
byte lgth;

parameters[idx++] = (short) 2; // default bearer

// APN1
short apnIdx = getIndexOfLV(BipConstants.LVIDX_APN1);
final short apnLength = apnName.length();
parameters[idx++] = (byte) apnLength;
idx = Util.arrayCopyNonAtomic(apnName, 0, parameters, idx, apnLength);

// Specify transport level
parameters[idx++] = (byte) 0x02; // TCP
parameters[idx++] = IDX_CFG_PORT_HI;
parameters[idx++] = IDX_CFG_PORT_LO;

// Specify destination address
short configIdx = getIndexOfLV(BipConstants.LVIDX_DEST);
lgth = destination.length()
parameters[idx++] = (byte) (lgth + 1);
parameters[idx++] = (lgth == (byte) 4) ? (byte) 0x21 : (byte) 0x57;
Util.arrayCopyNonAtomic(destination, 0, parameters, idx, lgth);

byte commandResult = ProactiveRequests.openChannel(parameters);

We then start off with

// Open a channel with details as 3== auto reconnect, immediate connect
ProactiveHandler proHdlr = proHandlerInit(
(byte) 0x40,
(byte) 0x01, // Immediate connect; no automatic reconnection; no background mode ETSI 102.223 8.6
(byte) ); // 0x82DEV_ID_TERMINAL

We then append TLV data defining bearer, buffer size, APN, transport level and destination host from the parameters in the 1st code block. The Proactive handler then sends this data to the handset to open a connection, and send the data

The way we send the code is (because String isn't a thing) is to have byte arrays...

private static final byte[] cs_http_10 = { ' ', 'H', 'T', 'T', 'P', '/', '1', '.', '0', 0x0d, 0x0a };

We then have an output buffer byte array, and copy the HTTP header in, along with the POST/GET (again, byte arrays),and then the information as a BASE64 encoded REST parameter.

Hope this is of some use. I can't give the full source code, as it's commercially sensitive, but this might be enough to point you in the right direction. I believe that the Proactive Handler Init with 0x40 is the most helpful part.

The problem I have currently is similar, but I need to send the TCP data to the handset itself, rather than an external host. I'm hoping I can run a local web-server, and just change the destination address to 127.0.0.1, and it'll just work.

vicoandrej
Posts: 11
Joined: Thu Dec 30, 2021 5:18 am
Points :158
Contact:

Re: Send data to socket from Java Card

Post by vicoandrej » Tue Jan 25, 2022 12:24 pm

What is ProactiveRequests in this code?

vicoandrej
Posts: 11
Joined: Thu Dec 30, 2021 5:18 am
Points :158
Contact:

Re: Send data to socket from Java Card

Post by vicoandrej » Tue Jan 25, 2022 12:25 pm

PeteSmithSIMs wrote:
Mon Jan 24, 2022 7:18 am
byte commandResult = ProactiveRequests.openChannel(parameters);

What is ProactiveRequests in this code?

PeteSmithSIMs
Posts: 3
Joined: Mon Jan 17, 2022 12:52 pm
Points :90
Contact:

Re: Send data to socket from Java Card

Post by PeteSmithSIMs » Tue Jan 25, 2022 4:56 pm

Hi.

Sorry, I didn't make it clear.

ProactiveRequests is one of our classes.

ProactiveRequest.open is the method which contains....

// Open a channel with details as 3== auto reconnect, immediate connect
ProactiveHandler proHdlr = proHandlerInit(
(byte) 0x40,
(byte) 0x01, // Immediate connect; no automatic reconnection; no background mode ETSI 102.223 8.6
(byte) ); // 0x82DEV_ID_TERMINAL

Hope this helps. If it doesn't, just reply and I'll help if I can.

Pete.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 28 guests

JavaCard OS : Disclaimer