Question
· 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
Discussion (8)0
Log in or sign up to continue

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);