http.c 2.92 KB
#include "http.h"
#include "rssi_list.h"


void error(char* err, int sock){
	printf("%s %d", err, sock);
	close(sock);
}

int main(int argc , char *argv[]){
    //udp_listening(char *msg,char *host);
    pthread_t  pcap; 
	pthread_t  udp;//our  thread for listen to UDP
	char *mess_udp = "UDP";
	
	//struct thread_params tp;
	int * my_ret = 0;
	//We create the thread
	 pthread_create(&pcap, NULL, &pcap_function, NULL);//(void *) &tp );
	
	//and launch the threads
	pthread_join(pcap, NULL); //void(**) &my_ret);
   	printf("pcap function launched\n");
	printf("yes2");
	int sock, sockansw;
	struct sockaddr_in my_addr;    // my address
	struct sockaddr_in their_addr; // connected address
	int addr_len, numbytes;
	char buf[MAXBUFLEN];
        char *message="";
	Element *rssi_list = malloc(sizeof(Element));

        /* We create the socket */
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1){
        error("ERROR creating socket", sock);
        exit(1);
    }

    struct hostent *hostinfo = NULL;
    struct sockaddr_in server = { 0 }; /* initialise the structure */
   /*
 const char *hostname = host;

    hostinfo = gethostbyname(hostname); /* get host informations */
    //if (hostinfo == NULL){ /* host doesn't exist */
      /*  fprintf (stderr, "ERROR,Unknown host %s.\n", hostname);
        exit(1);
    }*/

    /* fill in the structure */
    memcpy(&server.sin_addr, hostinfo->h_addr, hostinfo->h_length);
    server.sin_port = htons(PORT);
    server.sin_family = AF_INET;

    //Bind
    if( bind(sock,(struct sockaddr *)&server , sizeof(server)) < 0){
        fprintf (stderr, "ERROR,Bind fail %s.\n");
        exit(1);
    }
    printf("bind done\n");
    
	sleep(1); //We wait a little bit
	   printf("pcap function launched\n");

    addr_len = sizeof(struct sockaddr);

    while (1) { //listening
		printf("Waiting... \n");
		if ((numbytes=recvfrom(sock, buf, MAXBUFLEN, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { //wait for reception
		    perror("recvfrom");
		    exit(1);
		}

		printf("Packet received\n");
		buf[numbytes] = '\0';

		if ((sockansw = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { //We create the answer socket
		    perror("socket");
		    exit(1);
		}
		their_addr.sin_family = AF_INET;         // host byte order
		their_addr.sin_port = htons(THEIRPORT);     // short, network byte order
		bzero(&(their_addr.sin_zero), 8);

		//We send the answer
		char * answer;
		char * buffer="";

		answer =  build_buffer_full(rssi_list, buffer, message);
		 if ((numbytes=sendto(sock, answer, strlen(answer), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
			    perror("sendto");
			    exit(1);
		}
		close(sockansw); //We close the answer socket
    }


	/* close the socket */
	close(sock);

	pthread_exit(0); //We exit the thread
	printf("Complete");
	//printf("%d",&my_ret);
	
    return 0;
}