using socket.io with events from matlab to a java sever

7 次查看(过去 30 天)
Hello, I am trying to connect from Matlab to a server that uses socket connection and publishes JSON files through event. This data can be accessed via the socket.on method in java or @sio.on('event' ) in python. Can someone indicate how this can be achieved with Matlab?
I was able to connect to the server with the following:
import java.net.Socket
import java.io.*
import socket.io.*
input_socket = Socket(host, port);
But I am unable to listen to the event on the socket to get the data.
Thanks,

回答(1 个)

Nagarjuna Manchineni
I see that you are trying to connect the socket by using the following syntax:
input_socket = Socket(host, port);
This is causing the issue. For creating a Java object inside MATLAB you need to use the function 'javaObject'.
See the following documentation link for more information:
For example, for creating a socket you can use the following command:
client = javaObject('java.net.Socket', 'localhost', 4000);
For sending data from the client to server:
outToServer = client.getOutputStream();
out = javaObject('java.io.DataOutputStream', outToServer)
out.writeUTF('Hello from client');
For reading the data from the server:
inFromServer = client.getInputStream();
in = javaObject('java.io.DataInputStream', inFromServer)
in.readUTF()
I hope this helps!

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by