Failed to establish a TCP connection between two computers on MATLAB

12 views (last 30 days)
I am try to establish a TCP connection between two computers using tcpserver and tcpclient so that the client send a number 0 every second and the server reveives and shows the input.
Below is the code on the client's side:
clear;clc
tcp_c = tcpclient('192.168.3.140',60000,'Timeout',30);% creat a client with port 60000
% tcp_c.OutputBufferSize = 1000;% set buffer size for output
fopen(tcp_c); %open the client
data = 0;
while true
%mtic
fwrite(tcp_c,data,'double')
pause(1)
% if fread(tcp_c,1,'double') == 1; % read the input data: each number takes 8 bytes
% t = timer('BusyMode','queue','ExecutionMode','fixedDelay','Period',1,'TimerFcn',"disp('q')");
% end
% data = fgetl(tcp_c)
% data = fscanf(tcp_c,'string',len)
%time = (toc-1)*1000;
end
% fclose(tcp_c);
Below is the code on the server's side:
clear;clc
tcp_s = tcpserver('192.168.3.147',60000,'Timeout',30);% creat a server with port 60000
% tcp_s.InputBufferSize = 1000;% set buffer size for input
fopen(tcp_s); %open the server
while true
d = fread(tcp_s,1,'double')
pause(1)
end
% fclose(tcp_a);
Everytime I run, matlab return an error say"
"The requested address is not valid in its context"
and refered to the documentation below for troubleshooting:
I followed all the step in the document, but it still did not work.
When I change the ip on the server to '0,0,0,0' so it listens to all that talk to port 60000, it returned a different errer saying the first input of fopen must be a file identifier or a file name.
Ive ran out of idea on how to fix it or even when went wrong. Please Help!!!!!

Answers (1)

Sameer
Sameer on 16 Feb 2024
Hi,
From my understanding you want to establish a TCP connection between two computers using ‘tcpserver’ and ‘tcpclient’.
The error message "The requested address is not valid in its context" usually indicates that the IP address you're trying to bind to on the server side is not an IP address of the machine where the code is running. Ensure that the IP address you use for the server is indeed one of the IP addresses of the server machine.
When you want the server to listen on all network interfaces, you don't need to specify an IP address. Instead, you can just provide the port number when creating the tcpserver. Additionally, the 'fopen' function is not used with tcpclient or tcpserver objects. These objects are ready to use once created, and you don't need to open them with fopen.
Example server and client code:
Server code:
clear; clc;
% Create a server that listens on port 60000 on all network interfaces
tcp_s = tcpserver('0.0.0.0', 60000, 'Timeout', 30);
% Server loop
while true
% Check if the server has an open connection
if tcp_s.NumBytesAvailable > 0
% Read data if available
d = read(tcp_s, tcp_s.NumBytesAvailable, 'double');
disp(d);
end
pause(1); % Pause for a moment to avoid busy waiting
end
Client code:
clear; clc;
% Create a client that connects to the server's IP and port
%replace '192.168.3.140' with server's actual ip address
tcp_c = tcpclient('192.168.3.140', 60000, 'Timeout', 30);
% Client loop
while true
% Send data to the server
write(tcp_c, 0, 'double');
pause(1); % Wait for 1 second before sending the next number
end
Make sure that the server is running before you start the client, and that there are no firewalls blocking the connection on port 60000. If you're still having issues, double-check that both machines are on the same network and can ping each other.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!