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

조회 수: 15 (최근 30일)
Marwan Tabet
Marwan Tabet 2017년 6월 15일
답변: Nagarjuna Manchineni 2017년 6월 20일
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
Nagarjuna Manchineni 2017년 6월 20일
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!

카테고리

Help CenterFile Exchange에서 Java Client Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by