Matlab server for sending a file
Afficher commentaires plus anciens
I'm trying to send a jpeg file from matlab to unity and it's not connecting..any idea why?
matlab server
function server(message, output_port, number_of_retries)
import java.net.ServerSocket
import java.io.*
if (nargin < 3)
number_of_retries = 20; % set to -1 for infinite
end
retry = 0;
server_socket = [];
output_socket = [];
while true
retry = retry + 1;
try
if ((number_of_retries > 0) && (retry > number_of_retries))
fprintf(1, 'Too many retries\n');
break;
end
fprintf(1, ['Try %d waiting for client to connect to this ' ...
'host on port : %d\n'], retry, output_port);
% wait for 1 second for client to connect server socket
server_socket = ServerSocket(output_port);
server_socket.setSoTimeout(1000);
output_socket = server_socket.accept;
fprintf(1, 'Client connected\n');
output_stream = output_socket.getOutputStream;
d_output_stream = DataOutputStream(output_stream);
% output the data over the DataOutputStream
% Convert to stream of bytes
fprintf(1, 'Writing %d bytes\n', length(message))
d_output_stream.writeBytes(char(message));
d_output_stream.flush;
% clean up
server_socket.close;
output_socket.close;
break;
catch
if ~isempty(server_socket)
server_socket.close
end
if ~isempty(output_socket)
output_socket.close
end
% pause before retrying
pause(1);
end
end
end
this is my unity
using System.IO;public class Socket : MonoBehaviour {
// Use this for initialization
internal Boolean socketReady = false;
TcpClient mySocket;
NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
String Host = "localhost";
Int32 Port = 53000;
void Start () {
setupSocket ();
Console.Write ("socket is set up");
}
// Update is called once per frame
void Update () {
}
public void setupSocket() {
try {
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
socketReady = true;
//writeSocket("yah!! it works");
Console.Write ("socket is sent");
}
catch (Exception e) {
Debug.Log("Socket error: " + e);
}
}
}
Réponses (0)
Catégories
En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!