How to use fread from TCP/IP of a string?

17 visualizaciones (últimos 30 días)
Souarv De
Souarv De el 6 de Abr. de 2022
Respondida: Walter Roberson el 15 de Abr. de 2022
Hello guys
I am trying to send a string from client to server using the fwrite command and while fread it in the server side I get the numeric values instead string inspite mentioning the char.
client = tcpip('172.26.44.34',30000,'NetworkRole','client')
fwrite(client,'SOURAV','char')
In Server Side
server = tcpip('0.0.0.0',30000,'NetworkRole','server')
fread(server,6,'char')
At the end inspite of getting the string "SOURAV", I get number as [83 79 85 82 65 86]. I donot know why the character is not showing?

Respuesta aceptada

Prahlad Gowtham Katte
Prahlad Gowtham Katte el 14 de Abr. de 2022
Hello Sourav
As per my understanding of the question you want to read the string as a string instead of the ASCII values being returned. Using tcpserver and tcpclient instead of tcpipfunction would resolve the issue. When we use the tcpserver and tcpclientfunctions we can directly specify the datatype in read and writefunctions as string.
The following code snippet can help to fix the issue.
server = tcpserver('localhost',5000);
client = tcpclient(server.ServerAddress,server.ServerPort,"Timeout",5);
pause(1);
write(client,'SOURAV',"string");
rawData = read(server,6,"string");
For more information on tcpclient and tcpserverfunction please refer to the following links
Hope it helps!

Más respuestas (1)

Walter Roberson
Walter Roberson el 15 de Abr. de 2022
fread(server,6,'*char')
Or better yet in theory:
fread(server,6,'uint8=>char')

Categorías

Más información sobre Instrument Control Toolbox en Help Center y File Exchange.

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by