Lew Pitcher
2024-02-13 01:44:53 UTC
I'm trying to write a client for an existing server (an Asterisk
"Call Manager" server) that converses in text over a bog-standard
bi-directional TCP connection. I haven't done much sockets programming
and need some advice.
I successfully establish my socket connection to the server using
getaddrinfo() (to build the IPV4 address, socket() (to establish
an AF_INET,SOCK_STREAM socket) and connect(). The protocol requires
me to read a text line from the socket before commencing my client
requests.
For the line read, I decided to try stdio, so I fdopen(,"r") using
the acquired socket. The next steps are the ones that are currently
causing me issues.
IF I
char vrm[16];
fscanf(fd,"Asterisk Call Manager/%s\r\n",vrm);
to extract the AMI version number that follows the text slash, my
fscanf() hangs.
BUT, if I
char buffer[1024], vrm[16];
fgets(buffer,sizeof buffer,fd);
sscanf(buffer,"Asterisk Call Manager/%s\r\n",vrm);
I manage to obtain the appropriate data. A dump of the
buffer array shows that I did indeed capture the introduction line
from the server.
So, why the difference in behaviour? Obviously, in the fscanf() version,
I've not set something up right. Any hints as to what I've done wrong
would be greatly appreciated.
Thanks in advance for your help
"Call Manager" server) that converses in text over a bog-standard
bi-directional TCP connection. I haven't done much sockets programming
and need some advice.
I successfully establish my socket connection to the server using
getaddrinfo() (to build the IPV4 address, socket() (to establish
an AF_INET,SOCK_STREAM socket) and connect(). The protocol requires
me to read a text line from the socket before commencing my client
requests.
For the line read, I decided to try stdio, so I fdopen(,"r") using
the acquired socket. The next steps are the ones that are currently
causing me issues.
IF I
char vrm[16];
fscanf(fd,"Asterisk Call Manager/%s\r\n",vrm);
to extract the AMI version number that follows the text slash, my
fscanf() hangs.
BUT, if I
char buffer[1024], vrm[16];
fgets(buffer,sizeof buffer,fd);
sscanf(buffer,"Asterisk Call Manager/%s\r\n",vrm);
I manage to obtain the appropriate data. A dump of the
buffer array shows that I did indeed capture the introduction line
from the server.
So, why the difference in behaviour? Obviously, in the fscanf() version,
I've not set something up right. Any hints as to what I've done wrong
would be greatly appreciated.
Thanks in advance for your help
--
Lew Pitcher
"In Skills We Trust"
Lew Pitcher
"In Skills We Trust"