Question
Sorin Petrescu · Mar 8, 2021

Cache Socket Client / Java Server

Hi,

I have to create to a web socket client, but I'm unable to read any data from the server, after flushing the buffer. I have no access to the server, only two examples for the client, one in java and the other one in php:

java example:

socket = new Socket("192.168.0.1", 2003);

DataOutputStream out = new DataOutputStream(socket.getOutputStream());

out.writeUTF("aPassword");

out.writeInt(websiteId);

out.flush();

DataInputStream in = new DataInputStream(socket.getInputStream());

int orderId = in.readInt();

in.close();
out.close();
socket.close();

php:

$this->socket = socket_create(AF_INET, SOCK_STREAM, 0);

$this->writeStr($password);

$this->writeInt($id);

and the write functions are:

public function writeInt($value){
        $value = intval($value);
        return $this->write(pack('N', $value));
    }

public function writeStr($str){
        if (is_null($str))
            $str = 'null';
        $str = strval($str);
        $utfString = utf8_encode($str);
        $length = strlen($utfString);
        return $this->write(pack('n', $length).$utfString);
    }

The client I'm creating:

#dim sock As %IO.Socket = ##class(%IO.Socket).%New()
  
set id=57
set host = "89.73.132.83" set port = 2021
set password="1...5"

set sock.TranslationTable="UTF8"

set sockedOpened = sock.Open(host,port,-1)

do sock.Write(password, 0, .sc)
do sock.Write($$bl(57)_57, 0, .sc)
do sock.Flush(.sc)
set res=sock.ReadAny(12345, -1, .sc)

The socket opens successfully, I suppose there is a problem with the write command.

Any help would be appreciated.

Product version: Caché 2015.1
$ZV: Cache for Windows (x86-64) 2015.2 (Build 664U) Fri Jul 10 2015 12:12:10 EDT
0
0 378
Discussion (8)0
Log in or sign up to continue

InterSystems IRIS natively supports WebSockets protocol as a client (your case) and as a server.

Consider upgrading.

Are you saying there is no solution with Cache 2015?

Cache 2015 is a very old release. You should update to a latest version at least, preferably to InterSystems IRIS.

Mr. Lebedyuk,

I understand your worry. You can consider your responses not usefull as you don't know the whole context. I don't think you can appreciate the costs coming from updating.

in OEX there is a series of various  Websocket Clients I produced:
The internal is just available in IRIS.
Half Externals (eg. CSP based, using JavaScript )  might fit also for Caché 2015 as for IRIS
check WebSocketClient CSP based

 

Thank you, Robert.

I read about your examples. My problem is more related to the write command. I do not know if there is a possibility in Cache to create the same function of the following:

JAVA (DataOutputStream->writeUTF)

Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for the character. If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.

or

PHP (pack function)

write(pack('n', $length).$utfString);

UTF8 has to be set to the socket translation table:

Set sock.TranslationTable="UTF8"

and the solution in objectscript for the Java function:

DataOutputStream out = new DataOutputStream(socket.getOutputStream());

out.writeUTF(str);

is the following:

Do sock.Write($$setShort($Length(str)), 0, .sc)

Do sock.Write(str, 0, .sc)

setShort(num) {
Quit $Reverse($ZWChar(num))
}