资源简介
matlab使用TCP/IP Server Sockets
代码片段和文件信息
% CLIENT connect to a server and read a message
%
% Usage - message = client(host port number_of_retries)
function message = client(host port number_of_retries)
import java.net.Socket
import java.io.*
if (nargin < 3)
number_of_retries = 20; % set to -1 for infinite
end
retry = 0;
input_socket = [];
message = [];
while true
retry = retry + 1;
if ((number_of_retries > 0) && (retry > number_of_retries))
fprintf(1 ‘Too many retries\n‘);
break;
end
try
fprintf(1 ‘Retry %d connecting to %s:%d\n‘ ...
retry host port);
% throws if unable to connect
input_socket = Socket(host port);
% get a buffered data input stream from the socket
input_stream = input_socket.getInputStream;
d_input_stream = DataInputStream(input_stream);
fprintf(1 ‘Connected to server\n‘);
% read data from the socket - wait a short time first
pause(0.5);
bytes_available = input_stream.available;
fprintf(1 ‘Reading %d bytes\n‘ bytes_available);
message = zeros(1 bytes_available ‘uint8‘);
for i = 1:bytes_available
message(i) = d_input_stream.readByte;
end
message = char(message);
% cleanup
input_socket.close;
break;
catch
if ~isempty(input_socket)
input_socket.close;
end
% pause before retrying
pause(1);
end
end
end
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1728 2008-08-20 02:25 client.m
文件 1335 2009-05-22 02:21 license.txt
文件 1806 2008-08-20 01:59 server.m
- 上一篇:反步法的Matlab程序
- 下一篇:船舶一阶野本Nomoto模型
评论
共有 条评论