数学建模社区-数学中国

标题: 局域网下使用MATLAB传送文件 [打印本页]

作者: 建不了的模。    时间: 2014-12-17 09:43
标题: 局域网下使用MATLAB传送文件
局域网下使用MATLAB传送文件
本程序的功能是用matlab接收和传送文件。要求两台电脑都连接到了同一个路由器(即位于同一局域网中),并都安装有matlab软件。程序分成两个文件sendfile.m和getfile.m。发送端电脑使用sendfile.m发送文件,接收端电脑使用getfile.m接收文件。
举例
第一步,发送端电脑执行:
ip   
复制代码
第二步,发送端电脑执行:
sendfile
复制代码
选择好要传送的文件之后,在接收端执行第三步。

第三步,接收端电脑输入,注意接收端电脑接收文件时需要知道发送端的IP地址:
getfile('192.168.2.100');
复制代码
源程序代码:
function ip
%功能:获取电脑IP地址。
[s r]=system('ipconfig');
r=regexp(r,'IP Address. . . . . . . . . . . . : \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}','match');
r=r{1};
r=regexp(r,'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}','match');
lip=r{1};
disp(['本机IP为',lip]);
复制代码


getfile.m
function getfile(ip)
tcp=tcpip(ip,1314);
set(tcp,'InputBufferSize',6000000);
fopen(tcp);
while tcp.BytesAvailable==0
    pause(0.1);
end
filename=fread(tcp,tcp.BytesAvailable/2,'uint16'); %接收文件名
filename=char(filename);
filename=filename';
fwrite(tcp,1314,'uint16');  %接收名字成功
while tcp.BytesAvailable==0
    pause(0.1);
end
disp(['正在接收文件',filename]);
filesize=fread(tcp,1,'uint32')
disp(['文件共',num2str(filesize),'字节']);
fwrite(tcp,1314,'uint16');  %接收文件大小成功
while tcp.BytesAvailable~=filesize
    pause(0.5);
end
data=fread(tcp,filesize,'uint8');
fwrite(tcp,1314,'uint16');  %接收文件大小成功
[path name ext]=fileparts(filename);
filename=[name, '_' , num2str(round(rand*10000)), ext];
fid=fopen(filename,'w');
fwrite(fid,data,'uint8');
fclose(fid);
fclose(tcp);
复制代码



作者: w785485068    时间: 2014-12-17 15:20
顶一下。。。。。

作者: WSHXPY    时间: 2014-12-17 22:40
厉害厉害





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5