zoukankan      html  css  js  c++  java
  • 1

    % clc
    % clear
    % tcpclient = tcpip('localhost', 8000, 'Timeout', 60,'OutputBufferSize',10240,'InputBufferSize',10240);%连接这个ip和这个端口的TCP服务器
    % fopen(tcpclient);
    % fwrite(tcpclient,'please sent');%发送一段数据给tcp服务器。服务器好知道matlab的ip和端口
    % while(1) %轮询,直到有数据了再fread
    % nBytes = get(tcpclient,'BytesAvailable');
    % if nBytes>0
    % break;
    % end
    % end
    % receive = fread(tcpclient,nBytes);%读取tcp服务器传来的数据
    % %关闭连接
    % fclose(tcpclient);
    % data=str2num(char(receive(2:end-1)')); %将ASCII码转换为str,再将str转换为数组
    % disp(data)
    % delete(tcpclient);

    % Open Socket
    ip = '0.0.0.0';
    port = 2002;
    fprintf('Socket openning @%s:%d ... ',ip,port);
    t = tcpip(ip, port, 'NetworkRole','server');
    fopen(t);
    fprintf('Opened. ');
    tick = 0;

    while true
    % Read from socket (wait here)
    while t.BytesAvailable == 0, WAIT=true; end
    data = fread(t, t.BytesAvailable);
    string = char(data)';
    disp(string); % print-out
    % Read mat file
    %mat_name2load = 'script/file_from_python.mat';
    %l = load(mat_name2load);
    %disp(l);

    % Save mat file (echoing the loaded mat file)
    mat_name2save = 'script/file_from_matlab.mat';
    x = l.x;
    y = l.y;
    save(mat_name2save,'x','y'); % save back
    % Send to socket 
    tx_data = sprintf('[%d] This is from MATLAB.',tick);
    fwrite(t, tx_data);
    
    % terminate 
    tick = tick + 1;
    if tick >= 10, break; end
    pause(1e-0);
    

    end

    % Close
    fclose(t);
    fprintf('Closed. ');

  • 相关阅读:
    SQLite-SQLiteDatabase 数据库实例练习
    全局配置一个ajax的错误监控
    文件上传&&验证文件格式
    CSS3 resize 属性
    select change()
    window.location.Reload()和window.location.href 区别
    条件检索
    jQuery $.ajax传递数组的traditional参数传递必须true 对象的序列化
    jquery中attr方法和prop方法的区别
    resize
  • 原文地址:https://www.cnblogs.com/ccpang/p/14263087.html
Copyright © 2011-2022 走看看