Python代码
上边有的注释这里就不赘诉了。
import socket
import numpy as np
import json
from progress.bar import Bar
import math
#json解析numpy数据类
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
addr = ('127.0.0.1',1008) #设置服务端ip地址和端口号
buff_size = 65535 #消息的最大长度
tcpSerSock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcpSerSock.bind(addr)
tcpSerSock.listen(1)
tcpSerSock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 65535)
while True:
print('等待连接...')
tcpCliSock, addr = tcpSerSock.accept()
print('连接到:', addr)
while True:
decode = [] #存放解码后的数据
recv_data = []
#只接受第一个数据包的数据,提取里面的数据量值
while not recv_data:
recv_data = tcpCliSock.recv(int(buff_size))
data_bytes = np.frombuffer(bytes(recv_data),count=1,dtype='>f4')
#如果没有接收完大小为data_bytes[0]的数据,则一直接收
while len(recv_data) < data_bytes[0]:
data = []
while not data:
data = tcpCliSock.recv(int(buff_size))
recv_data += data #数据拼接
data_bytes = np.frombuffer(bytes(recv_data),count=1,dtype='>f4')
print(data_bytes,len(recv_data))
decode = np.frombuffer(bytes(recv_data),dtype='>f4') #数据解码
print('接收到数据')
height = int(decode[1])
width = int(decode[2])
echo = decode[3:int(height*width+3)]
mat = echo.reshape((height,width),order='F')
#完成计算任务
U, S, V = np.linalg.svd(mat, full_matrices=False) #奇异值分解
#在字典里增加一个L来保存返回数据包的大小
result={'L':2e8,
'U':U,
'S':S,
'V':V}
send_result = json.dumps(result,cls=NpEncoder) #将字典编码一次,看看数据包有多大
result['L'] = len(send_result) #把数据包真实大小写进字典
#matlab数据包接收不完全的时候会等到超时,浪费时间。把数据包用‘ ’填满到matlab接收数据包大小的整数倍。
matlab_buffer = 8388608
fill_space = math.ceil(result['L']/matlab_buffer+1)*matlab_buffer
send_result = json.dumps(result,cls=NpEncoder).ljust(fill_space).encode('utf-8')#重编码
print('需要发回%d字节的数据包'%len(send_result))
tcpCliSock.sendto(send_result,addr) #数据发送
print('发送完成')
break
tcpCliSock.close()
tcpSerSock.close()
Matlab代码
close all;clear;clc;
data = double(rand(2048,2048));
config = whos('data');
time_out = 60; % 投送数据包的等待时间
tcpipClient = tcpip('127.0.0.1',1008,'NetworkRole','Client');
set(tcpipClient,'OutputBufferSize',67108880+64);%2048*4096
set(tcpipClient,'Timeout',time_out);
tcpipClient.InputBufferSize = 8388608;%8M
tcpipClient.ByteOrder = 'bigEndian';
fopen(tcpipClient);
disp("连接成功")
disp("数据发送")
send_data = [config.size(:);data(:)];
config_send = whos('send_data');
fwrite(tcpipClient,[config_send.bytes/2;send_data],'float32');
disp("数据接收")
recv_data = [];
%重复多次接收
h=waitbar(0,'正在接收数据');
while isempty(recv_data)
recv_data=fread(tcpipClient);%读取第一组数据
end
header = convertCharsToStrings(native2unicode(recv_data,'utf-8'));
recv_bytes = str2double(regexp(header,'(?<=(L": )).*?(?=(,|$))','match'))-2;%正则化提取数据大小
while length(recv_data)<recv_bytes
if recv_data(end)==125
break
end
waitbar(length(recv_data)/recv_bytes)
recv_package = [];
while isempty(recv_package)
try
recv_package=fread(tcpipClient);
catch
continue
end
end
recv_data = vertcat(recv_data,recv_package);
end
close(h)
chararray = native2unicode(recv_data,'utf-8');
str = convertCharsToStrings(chararray);
try
U = dic.U;
S = dic.S;
V = dic.V;
re = U*diag(S)*V;
catch
disp('WARNNING:接收不完全')
end
disp('连接断开')
fclose(tcpipClient);
多个矩阵的传输(例子:复数数据)
有时需要求解复数数据,把复数分解为实部和虚部两个矩阵。
Python代码
import socket
import numpy as np
import json
from progress.bar import Bar
import math
#json解析numpy数据类
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
addr = ('127.0.0.1',1008) #设置服务端ip地址和端口号
buff_size = 65535 #消息的最大长度
tcpSerSock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcpSerSock.bind(addr)
tcpSerSock.listen(1)
tcpSerSock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 65535)
while True:
print('等待连接...')
tcpCliSock, addr = tcpSerSock.accept()
print('连接到:', addr)
while True:
decode = [] #存放解码后的数据
recv_data = []
while not recv_data:
recv_data = tcpCliSock.recv(int(buff_size))
data_bytes = np.frombuffer(bytes(recv_data),count=1,dtype='>f4')
print('正在接收...')
while len(recv_data) < data_bytes[0]:
data = []
while not data:
data = tcpCliSock.recv(int(buff_size))
recv_data += data #数据拼接
data_bytes = np.frombuffer(bytes(recv_data),count=1,dtype='>f4')
print(data_bytes,len(recv_data))
decode = np.frombuffer(bytes(recv_data),dtype='>f4') #数据解码
print('接收到数据')
height = int(decode[1])
width = int(decode[2])
echo_r = decode[3:int(height*width+3)]
echo_i = np.array(decode[int(height*width+3):])*1.j
echo = echo_r + echo_i
mat = echo.reshape((height,width),order='F')
U, S, V = np.linalg.svd(mat, full_matrices=False) #奇异值分解
#U, S, V = svd(mat)
result={'L':2e8,
'U':np.real(U),
'S':np.real(S),
'V':np.real(V),
'Ui':np.imag(U),
'Si':np.imag(S),
'Vi':np.imag(V)}
send_result = json.dumps(result,cls=NpEncoder) #将字典编码
result['L'] = len(send_result)
matlab_buffer = 33554432
fill_space = math.ceil(result['L']/matlab_buffer+1)*matlab_buffer
send_result = json.dumps(result,cls=NpEncoder).ljust(fill_space).encode('utf-8')#重编码
print('需要发回%d字节的数据包'%len(send_result))
tcpCliSock.sendto(send_result,addr) #数据发送
print('发送完成')
break
tcpCliSock.close()
tcpSerSock.close()
Matlab代码
close all;clear;clc;
%data = double(rand(2048,2048));
data = load('Echo2048_4096.mat').Echo;
config = whos('data');
time_out = 60; % 投送数据包的等待时间
tcpipClient = tcpip('127.0.0.1',1008,'NetworkRole','Client');
set(tcpipClient,'OutputBufferSize',67108880+64);%2048*4096
set(tcpipClient,'Timeout',time_out);
%tcpipClient.InputBufferSize = 8388608;%8M
tcpipClient.InputBufferSize = 33554432;%32M
tcpipClient.ByteOrder = 'bigEndian';
fopen(tcpipClient);
disp("连接成功")
disp("数据发送")
type = 'complex';
if strcmp(type,'complex')
rmap = real(data);
imap = imag(data);
send_data = [config.size(:);rmap(:);imap(:)];
config_send = whos('send_data');
fwrite(tcpipClient,[config_send.bytes/2;send_data],'float32');
else
send_data = [config.size(:);data(:)];
config_send = whos('send_data');
fwrite(tcpipClient,[config_send.bytes/2;send_data],'float32');
end
disp("数据接收")
recv_data = [];
%重复多次接收
h=waitbar(0,'正在接收数据');
while isempty(recv_data)
recv_data=fread(tcpipClient);%读取第一组数据
end
header = convertCharsToStrings(native2unicode(recv_data,'utf-8'));
recv_bytes = str2double(regexp(header,'(?<=(L": )).*?(?=(,|$))','match'))-2;
while length(recv_data)<recv_bytes
if recv_data(end)==125
break
end
waitbar(length(recv_data)/recv_bytes)
recv_package = [];
while isempty(recv_package)
try
recv_package=fread(tcpipClient);
catch
continue
end
end
recv_data = vertcat(recv_data,recv_package);
end
close(h)
chararray = native2unicode(recv_data,'utf-8');
str = convertCharsToStrings(chararray);
try
dic = jsondecode(str);
if strcmp(type,'complex')
U = dic.U + dic.Ui*1.j;
S = dic.S + dic.Si*1.j;
V = dic.V + dic.Vi*1.j;
else
U = dic.U;
S = dic.S;
V = dic.V;
end
re = U*diag(S)*V;
catch
disp('WARNNING:接收不完全')
end
disp('连接断开')
fclose(tcpipClient);