【图像处理】matlab tcp server 持续通信

本文介绍了如何使用C++作为客户端,MATLAB作为服务器进行TCP/IP通信。MATLAB端创建一个持续监听的TCP服务器,等待C++发送指令并接收返回数据。通过设置TCP端口和缓冲区大小,实现数据交换。这种方法适用于需要持续交互的场景。
摘要由CSDN通过智能技术生成

需求:

        c++做客户端调用matlab处理数据,获取matlab返回的数据集合,但是希望matlab以服务的形式一直挂载使用,这里提供一个简单的方法,在tcp服务上再套一层while(1)即可,如何有更好的方法欢迎在评论区讨论。

while(1)

    port = 9010;
 
	% 构造服务器端tcpip对象
	tcpipServer = tcpip('0.0.0.0',port,'NetWorkRole','Server');
	set(tcpipServer,'Timeout',10);
	N = 1024;
	set(tcpipServer,'InputBufferSize',8*N);
	set(tcpipServer,'OutputBufferSize',1024);
	 
	% 打开连接对象
	fopen(tcpipServer);
	 
	% 发送指令
	instruction = 'Please send back a signal.';
	fwrite(tcpipServer,instruction,'int8');
	disp('Instruction sending succeeds.');
	numSent = get(tcpipServer,'valuesSent');
	disp(strcat('Bytes of instruction is :',num2str(numSent)));
	 
	% 等待接收数据
	while(1)
		nBytes = get(tcpipServer,'BytesAvailable');
		if nBytes > 0
			break;
		end
	end
	 
	% 接收数据
	receivedInstruction = fread(tcpipServer,nBytes,'int8');
	disp(strcat('received instruction is: ',char(receivedInstruction')));
	% 关闭和删除连接对象
	fclose(tcpipServer);
	delete(tcpipServer);
end

 

 

参考:

【图像处理】c++ matlab 线程通讯_ranmaxli的博客-CSDN博客本篇讲述2种场景:c++做服务器,matlab做客户端;matlab做服务器,c++做客户端;1、c++做服务器,matlab做客户端c++代码:#include <iostream>#include <stdio.h>#include <stdlib.h>#include <WinSock2.h>#pragma comment(lib,"Ws2_32.lib")#include <memory.h>#defhttps://blog.csdn.net/oqqHun123/article/details/120842144?spm=1001.2014.3001.5501

MATLAB实现TCP/IP的持续通信_y=520(2sinM+sin2M)的博客-CSDN博客_matlab tcpiphttps://blog.csdn.net/qq_18820125/article/details/109820825

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口职业技术学院落榜生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值