MATLAB Answers

How to send an array from client to server ?

48 views (last 30 days)
Meghashree G
Meghashree G on 27 Sep 2015
Edited: Samuel Gray on 4 Mar 2021
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)
t = tcpip('', 30000, 'NetworkRole', 'server');
while t.BytesAvailable == 0
data = fread(t, t.BytesAvailable);
clear t;
client.m(client code):
t = tcpip('localhost', 30000, 'NetworkRole', 'client');
ui=input('enter the message:','s')
decString = unicode2native(ui,'utf-8')
hexString = dec2hex(decString)
b= dec2bin(decString,8)-'0';
key= trans(len+1 :end)
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);
% Display in command window:
fwrite(t, data);
i need to send result =
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 Comment
Samuel Gray
Samuel Gray on 4 Mar 2021
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.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 29 Sep 2015
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!