How to send an array from client to server ?
5 次查看(过去 30 天)
显示 更早的评论
I am implementing cryptographic algorithm(DNA encryption algorithm).for that i'm using 2 matlab terminals,one acting as client and other acting as server. I have to send the array from client to server,but i'm getting error.
Here's my code:
server.m(server side)
clc;
t = tcpip('0.0.0.0', 30000, 'NetworkRole', 'server');
fopen(t);
while t.BytesAvailable == 0
pause(1)
end
data = fread(t, t.BytesAvailable);
disp(data);
fclose(t);
delete(t);
clear t;
client.m(client code):
clc;
t = tcpip('localhost', 30000, 'NetworkRole', 'client');
fopen(t);
ui=input('enter the message:','s')
decString = unicode2native(ui,'utf-8')
hexString = dec2hex(decString)
b= dec2bin(decString,8)-'0';
binaryArray=[b]
v=[hexString]
trans=v'
len=length(trans)
key= trans(len+1 :end)
%x=[b]
bases = 'ATGC';
for k = 1 : 2 : length(binaryArray)
% Convert these two digits into a number 1 - 4.
%index = 2 * binaryArray(k) + binaryArray(k+1) + 1;
index = 2 * binaryArray(:,1:2:end) + binaryArray(:,2:2:end) + 1;
% Use that index to assign a letter to our result.
%result((k+1)/2) = bases(index);
result = bases(index);
end
% Display in command window:
result
data=result;
pause(1);
fwrite(t, data);
i need to send result =
TGAC
TCAG
TCGT
TCAA
TCTA
TGCC
to server side,but im getting following error:
??? Error using ==> icinterface.fwrite
at 191
An error occurred during writing.
Error in ==> client at 39
fwrite(t, data);
Please help me.. Thank you
1 个评论
Samuel Gray
2021-3-4
编辑:Samuel Gray
2021-3-4
I believe that it's not working because you're not closing the port at the end of the client m-file, so when you re-run the m-file the port is already open but the reference is lost, then fopen throws an error and at least I have to restart Matlab to get this to work again.
Try adding an fclose(t) to your script as t is local to the script. Yeah, same with the server file.
I'd put everything in a try-catch loop so that if there is an error you can ensure that fclose(t) is run.
you can use try;catch Me; end and then print Me.message and look at the Me object
Yeah, do that and use this get(t,'OutputBufferSize') until you get all the bugs out, and then you can remove the try-catch block and speed it up. This toolkit has little patience for buggy code and you'll be glad when you get to the point where you won't have to restart Matlab, only restart the script.
回答(2 个)
Walter Roberson
2015-9-29
I suspect you might be hitting a buffersize limit, but it is difficult to tell as part of the error message appears to be missing.
One thing you need to keep in mind is that when you transmit an array, the order is "down columns first". Your char array "result" will be something by 4, but is going to be transmitted as if you had written result(:) . You will want to reshape() the data that is received.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interface-Based Instrument Communication 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!