How to get the ASCII data from a server

Other open source tools published on ScottKlement.com
Post Reply
richardchen
Posts: 5
Joined: Fri Jun 23, 2023 1:24 am

How to get the ASCII data from a server

Post by richardchen »

Hi all, greeting. Hope you are doing good.

I have a RPGLE program which will call a Java program. The Java program will connect to a TCP server and get the ASCII data back.
I print the data in Java - it is ASCII value. And I use below command to set the value to a variable in RPGLE.

Code: Select all

 * Prototype for Java String Object - get bytes method             
D getBytes        PR          2048A   ExtProc(*JAVA:               
D                                             'java.lang.String':  
D                                             'getBytes')          
D                                     Varying                      
 *
d l1rcvmsg        S           2048A
/free
    $rcvMsgStr = receive(TCPClient_);    
    l1rcvmsg = getBytes( $rcvMsgStr ); 
/end-free
I want to get the data in ASCII format in `lrcvmsg` like the C tcp package.
It is because the auto conversion has issue.

The auto conversion will convert my data wrongly. For example the ASCII Hex is: 31 32 36 09 40 D0 10
The auto conversion will convert it to: F1 F2 F6 05 7C 0E FE, where the last byte (D0 10) is wrong. the excepted value should be: F1 F2 F6 05 7C 9F 10

From IBM document/Internet, looks like getBytes will convert the data to EBCDIC. My section is set CCSID 65535 and I have tried to set `l1rcvmsg` to CCSID(1208) etc.
Also I try `String_getUCS2` in post https://www.scottklement.com/forums/viewtopic.php?t=76
Nothing works.

IBM document said I should use JNI if i do not want the auto conversion.
I raise my query and want to check if any other way to resolve the issue without JNI.
Thank you all in advance.

Information about JNI:
- https://www.ibm.com/docs/en/i/7.3?topic ... jni-coding
- https://www.ibm.com/docs/en/i/7.3?topic ... prototypes
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: How to get the ASCII data from a server

Post by Scott Klement »

Java stores data in String objects which are internally stored in Unicode. So if you retrieve them as UCS-2, you'll get the same Unicode in RPG.

But, possibly your data isn't Unicode to begin with? You said something about a "TCP server", with basically no context. I have no clue why an RPG programmer would use Java for that rather than the operating system's routines for TCP communication. But, assuming you must use Java for some reason (maybe your computer is too fast and you need some way to slow it down?) I think it very unlikely that the output of a TCP receive would be a String object. I would expect it to be a byte[] array...

Can you provide more details, here?
richardchen
Posts: 5
Joined: Fri Jun 23, 2023 1:24 am

Re: How to get the ASCII data from a server

Post by richardchen »

But, possibly your data isn't Unicode to begin with?
Yup, the data is ASCII data. But there are binary data there. for example, the server use 1 byte 7C to represent the field length is 126 character.
I have no clue why an RPG programmer would use Java for that rather than the operating system's routines for TCP communication
The tcp server is outside network. I need to use SOCKS5 to communicate with it.
IBM TCP package (C language) did not support socks5, only support socks4.
I would expect it to be a byte[] array...
Correct. In the Java program, i return the data as byte[] array.

I think it very unlikely that the output of a TCP receive would be a String object.
No issue for the String object. I think the challenge here is that i need to use getBytes to convert the String object back to EBCDIC and store it in a variable in RPGLE. The getBytes will do the conversion for me as the target (var in RPGLE) is defined as Character. The issue is that, the conversion will corrupt my data - given the example in original post: the ascii hex data xD0 is converted wrongly. The expected result is x9F with QXLATE.


Updated on 15Aug24: After long working hour,I try to simulate the data with a simple java program.
I define the PR with integer 3i 0 dim(4048) , this way the RPGLE compiler will not convert the ASCII data to EBCDIC for me.
I can get the correct hex data in w1msg. But actually it stores all the data as Integer. But those "empty" position, the default value will be x00, instead of x40 (blank). This introduce difficulty for me to further process the data.

Appreciated if some of you have a better solution.

Code: Select all

D generateByteArrays...                                             
D                 PR             3i 0 dim(4048)                     
D                                     EXTPROC(*JAVA:                
D                                            'HelloWorld':          
D                                             'generateByteArrays') 

w1msg = generateByteArrays;
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: How to get the ASCII data from a server

Post by Scott Klement »

I would've used uns(3) (3U 0 if you must still use fixed format)

I would never expect the data to be returned as x'40', that's EBCDIC. You said your data is a mixture of binary data and ASCII. I don't know what HelloWorld/generateByteArrays is, but what you need is to get the length of the array and the data in the array, then the data should be the exact binary values.

Please be aware that "ASCII" is terribly unspecific. Many different things called "ASCII" were invented over the years for different computer systems and different languages/cultures... ASCII is a sort of "family" of character sets, it is not a specific one. To interpret it properly, you're going to have to know what the actual encoding was, or at least say "I'll just use X, it's close enough."
Post Reply