[asterisk-dev] Fastagi question

Yelson Vivas yvivas at gmail.com
Mon Sep 25 14:39:06 MST 2006


Hi Guys
i'm developing my first  fast-agi in C but i'm getting trouble to make it work
My problem is that i don't know how can i  get  the initial stream
when i start the socket client connection, the server sockets reads
the asterisk information but i need to know when it stops to start
sending commands to the pbx, here is my code
--------------------------------------------------------------------------

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>


void err_quit(char *msg){
	perror(msg);
	exit(EXIT_FAILURE);
}

int main(void){
	struct sockaddr_in serv_addr1;
	int desc_socket, desc_archivoDatos, cnt;
	socklen_t tamano_dir;
	char line[480];
	char buffer[80];	
	int i =1;
	//create socket
	if((desc_socket = socket(PF_INET, SOCK_STREAM,0)) < 0)
		err_quit("socket");
	memset(&serv_addr1,0,sizeof(serv_addr1));	
	setsockopt(desc_socket,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));			
	serv_addr1.sin_family = AF_INET;
	serv_addr1.sin_port = htons(4573);
	inet_aton("192.168.222.18", &(serv_addr1.sin_addr));
	tamano_dir = sizeof(serv_addr1);	
	if((bind(desc_socket,(struct sockaddr *)&serv_addr1,tamano_dir) ) < 0)
		err_quit("bind");
	if((listen(desc_socket,5)) < 0)
		err_quit("listen");
	fprintf(stderr,"Socket TCP/IP  \n");	
	fprintf(stderr,"\tPort %d\n",ntohs(serv_addr1.sin_port));
	fprintf(stderr,"\tIP %s\n", inet_ntoa(serv_addr1.sin_addr));
	
	if((desc_archivoDatos = accept(desc_socket,(struct sockaddr
*)&serv_addr1,&tamano_dir)) >= 0 ){
		setsockopt(desc_archivoDatos,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));
		fprintf(stderr,"socket coonected \n");
	int pos = -1;	
	int i=1;
	if ((cnt = recv(desc_archivoDatos, line , sizeof(line), 0)) > 1)
		{
		 cnt = recv(desc_archivoDatos, line , sizeof(line) , 0 );
		 fprintf(stderr,line);
		}
	}else{
			fprintf(stderr,"Error socket \n");
			close (desc_archivoDatos);
			exit(EXIT_FAILURE);
	}
 /* Send asterisk a command */
    char orden[] = {"SAY NUMBER 123 \"\"\n"};
    send ( desc_archivoDatos , orden , sizeof(orden), 0 );
    recv(desc_archivoDatos, buffer , sizeof(buffer) , 0 );
	 fprintf(stderr,buffer);
	 char orden1[] = {"HANGUP \n"};
    send ( desc_archivoDatos , orden , sizeof(orden), 0 );
	 close (desc_archivoDatos);
    exit(EXIT_SUCCESS);
}

--------------------------------------------------------------------------
Ps: Sorry if it's to simple question, i'm newbie =]
Thanks in advance
BR
-- 
Yelson E Vivas C


More information about the asterisk-dev mailing list