Why cannot communicate more than twice between server and client?

1 visualización (últimos 30 días)
I want to construct more than twice communication between server and client.
I make two code Client and Server.
I achieve a single communication, but I cannot second communication.
Error message is "icinterface/fopen (line 83) Unsuccessful open: Connection refused: connect"
Client Code :
clc;
clear ;
close all;
% Configuration and connection
client = tcpip('localhost',30000,'NetworkRole','client');
disp('Wating for connection');
% Open socket and wait before sending data
fopen(client);
disp('Connection OK');
disp('Send Data')
fwrite(client, 'test')
disp('Complete send')
fclose(client);
delete(client);
clear client
Server Code :
%% TCP/IP Receiver
% Clear console and workspace
try
delete(server);
clear server
end
echotcpip('off');
close all;
clear all;
clc;
% Configuration and connection
disp ('Receiver started');
server=tcpip('localhost', 30000,'NetworkRole','server');
set(server, 'Timeout',inf);
set(server, 'InputBufferSize', 300000);
% Wait for connection
disp('port open. Waiting for connection');
fopen(server);
while true
while server.BytesAvailable == 0
end
% Read data from the socket
DataReceived=fread(server,server.BytesAvailable);
disp(strcat(char(DataReceived)'));
disp('Connection OK');
disp('complete read data');
end
fclose(server);
disp('port closed');

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Dic. de 2018
If you need multiple simultaneous connections, they will have to be on different ports.
One strategy in common use (by some of the key daemons, but not so much with user programs) is to publish a port for the client to make the connection to (e.g., the 30000), but instead of holding on to that port for the entire conversation, instead the server creates a new listener on another port, and responds to the client telling it what port the listener was created on, and then ends the connection. All of the clients need to talk to the same master port to get told what port the conversation will take place on, but those conversations are short and any refused connection can be retried a short time later to get the system a chance to finish the previous (short) conversation.
  3 comentarios
Walter Roberson
Walter Roberson el 30 de Dic. de 2018
Not at all easily, not with tcpip() from the Instrument Control Toolbox. Connections make that way are not listened on until fopen() is done, but the provided fopen() is synchronous (cannot be done in background) and offers no timeout at the MATLAB level (it creates Java objects.)
Naoya Inoue
Naoya Inoue el 30 de Dic. de 2018
Thank you very much !!
I refer to your proposition URL. This toolbox meets my exception.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by