通过套接字发送的消息缺少\ n

时间:2018-11-21 08:51:43

标签: python matlab sockets

我正在为python和matlab之间的tcpip套接字生成协议。在尝试设置某种协议时,我遇到了一个问题。与下面的这组代码有关

FPath = Path('c:/test/dogg.jpg')     
HASH = Commons.get_file_md5_hash((FPath))

msg = ('IDINFO'+FPath.name+'HASH'+ HASH+'\n')

生成消息

IDINFOdogg.jpgHASH7ad1a930dab3c099939b66267b5c57f8

我在消息中输入:IDINFO,它将告诉服务器文件名,而HASH,将告诉文件详细信息。

之后,我使用

打开文件
f = open(FPath,"rb")
chunk = f.read(1020)

并构建一个带有标签DATA的软件包

msg = b`DATA` + chunk + b'\n'

问题是b'\n'与第一条消息中的不一样。因为Matlab无法读取定界符,也不会继续获取数据块。

Matlab代码,供以下参考。这不是整个对象,而是可能引起麻烦的部分。 设置回调。

       set(gh.tcpipServer, 'BytesAvailableFcnMode','Terminator');
       set(gh.tcpipServer, 'BytesAvailableFcn', @(h,e)gh.Serverpull(h,e)); 

查看字节的功能

    function Serverpull(gh,h,e)
        gh.msg = fread(gh.tcpipServer,gh.tcpipServer.BytesAvailable);
        gh.msgdecode = char(transpose(gh.msg));

        if strfind(gh.msgdecode,'IDINFO')
            Hst = strfind(gh.msgdecode,'HASH');
            gh.Fname = gh.msgdecode(7:Hst-1);
            gh.HASH = gh.msgdecode(Hst+4:end);
            fwrite(gh.tcpipServer,'GoodToGo');

            gh.PrepareforDataAq()
        elseif strfind(gh.msgdecode,'DATA')
            fwrite(gh.fileID,gh.msg(5:end),'double');
        elseif strfind(gh.msgdecode,'EOF')
            fclose(gh.fileID);
            display('File Transfer Complete')
        end
    end
    function PrepareforDataAq(gh)
        path = fullfile('c:\temp\',gh.Fname);
        gh.fileID = fopen(path,'w');

    end    

对于TLDR,

在从二进制文件而不是编码字符串中构建tcp消息时,如何使字符串'\n'b \n相同?

0 个答案:

没有答案