MATLAB Answers

Matlab2020 tcp socket open

8 ビュー (過去 30 日間)
Saharsh Bansal
Saharsh Bansal 2021 年 6 月 16 日
回答済み: Kunal Kandhari 2021 年 6 月 19 日
Im trying to establish a server socket on Matlab2020a.
However when I try to open the socket, it seems to be stuck at that line.
It was working till a few days back, but keeps getting stuck at this point now.

回答 (1 件)

Kunal Kandhari
Kunal Kandhari 2021 年 6 月 19 日
The following code will create Server socket at port 5001 and infinitely allows clients to connect and receive data from them:
[~,hostname] = system('hostname');
hostname = string(strtrim(hostname));
address = resolvehost(hostname,"address");
server = tcpserver(address,5001,"ConnectionChangedFcn",@connectionFcn)
function connectionFcn(src, ~)
if src.Connected
disp("Client connected")
readData = read(src,src.NumBytesAvailable,'string');
disp(readData);
write(src,"Bye Bye","string");
end
end
Client code(for testing purpose):
client = tcpclient("172.31.120.98",5001,"Timeout",5);
% do change Ip address to Ip address at which server socket is created, you
% can find that from command line after runing server socket code
while(true)
rawData="Hello Server";
write(client,rawData,"string");
rawData = read(client,7,"string");
disp(rawData);
end
For more reference:

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by