[asterisk-dev] C Code to connect to Asterisk Manager
Russell Bryant
russell at digium.com
Sun Mar 2 02:36:05 CST 2008
Soumya Kat wrote:
> I have changed the code as you said with strcpy and now I can connect
> with Asterisk Manager Interface. I can correctly see that admin is
> trying to login in from 192.168.0.150 <http://192.168.0.150>. The
> problem that I am facing now is after authentication if I try to ping
> the server the response I get is Response: Error and states that there
> is no action.
>
> Here is my code:
>
> #define MAX_MSG_SIZE 3072
> #define SERVER_ADDRESS "192.168.0.150 <http://192.168.0.150/>"
> #define CLIENT_ADDRESS "192.168.0.150 <http://192.168.0.150/>"
> #define SERVER_PORT 5038
> #define CLIENT_PORT 5100
>
> int main()
> {
> int sd;
> struct sockaddr_in serveraddr, clientaddr;
> char msg[MAX_MSG_SIZE];
> char resp[MAX_MSG_SIZE];
>
> bzero((char *) &serveraddr, sizeof(serveraddr));
> serveraddr.sin_family = AF_INET;
> serveraddr.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
> serveraddr.sin_port = htons(SERVER_PORT);
>
> bzero((char *) &clientaddr, sizeof(clientaddr));
> clientaddr.sin_family = AF_INET;
> clientaddr.sin_addr.s_addr = INADDR_ANY;
> clientaddr.sin_port = htons(CLIENT_PORT);
>
> sd = socket(AF_INET, SOCK_STREAM, 0);
>
> bind(sd,(struct sockaddr *) &clientaddr, sizeof(clientaddr));
Setting up clientaddr and calling bind() is useless for a client application,
but that is not the cause of your problem ...
>
> connect(sd,(struct sockaddr *) &serveraddr, sizeof(serveraddr));
>
> strcpy(msg,"Action: Login\r\nUsername: admin\r\nSecret: admin\r\n\r\n");
> send(sd,msg,strlen(msg)+1,0);
... this is. Take off the "+1" here and in your next call to send().
Be sure to try to understand why these changes have been necessary and don't
just make them. :)
> recv(sd,resp, sizeof resp,0);
> printf("%s",resp);
> strcpy(resp," ");
>
> sleep(1);
>
> strcpy(msg,"Action: Ping\r\n\r\n");
> send(sd,msg,strlen(msg)+1,0);
> recv(sd,resp,sizeof resp,0);
> printf("%s",resp) // In here it prints Response: Error.
> close(sd);
>
> return(1);
> }
>
> Now why is it so ? It should return Response: Pong as I am still
> connected with Asterisk Manager.
--
Russell Bryant
Senior Software Engineer
Open Source Team Lead
Digium, Inc.
More information about the asterisk-dev
mailing list