Thread: in a constant loop with threads using socket

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    in a constant loop with threads using socket

    hi so this is my server.c

    Code:
    #include <sys/socket.h> 
    
    #include <sys/ioctl.h>
    
    #include <sys/time.h>
    
    
    
    
    #include <asm/types.h>
    
    
    #include "server.h"
    
    #include "util.h"
    
    
    
    #include <math.h>
    
    #include <string.h>
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <unistd.h>
    
    #include <signal.h>
    
    #include <pthread.h>
    
    
    #include <linux/if_packet.h>
    
    #include <linux/if_ether.h>
    
    #include <linux/if_arp.h>
    
    
    
    #define BUF_SIZE ETH_FRAME_TOTALLEN
    
    
    void * thread2(void*);
    void * thread1(void*);
    
    int	commpipe[2];		
    int	sndpipe[2];
    unsigned char* buf;
    unsigned char* bug;
    
    #define NUM_THREADS 2
    pthread_t tid[NUM_THREADS];      /* array of thread IDs */
    
    
    int s = 0; /*Socketdescriptor*/
    
    void* buffer = NULL;
    
    long total_packets = 0;
    
    long answered_packets = 0;
    
    
    
    int main(void) {
    
    	buffer = (void*)malloc(BUF_SIZE); 	/*Buffer for Ethernet Frame*/
    
    	unsigned char* etherhead = buffer;	/*Pointer to Ethenet Header*/
    
    	struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to ethernet header*/
    
    
    
    	unsigned char src_mac[6];		/*our MAC address*/
    
    
    
    	struct ifreq ifr;
    
    	struct sockaddr_ll socket_address;
    
    	int ifindex = 0;			/*Ethernet Interface index*/
    
    	int i;
    
    	int length;				/*length of received packet*/
    
    	int sent;
    
    	if (pipe( commpipe ) ) {
    		fprintf(stderr,"Pipe error!\n");
    		exit(1);
    	}
    	if (pipe( sndpipe ) ) {
    		fprintf(stderr,"Pipe error!\n");
    		exit(1);
    	}
    
    
    
    
    	printf("Server started, entering initialiation phase...\n");
    
    
    
    	/*open socket*/
    
    	s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    
    	if (s == -1) {
    
    		perror("socket():");
    
    	        exit(1);
    
    	}
    
    	printf("Successfully opened socket: %i\n", s);
    
    	
    
    	/*retrieve ethernet interface index*/
    
    	strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
    
    	if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
    
    		perror("SIOCGIFINDEX");
    
    		exit(1);
    
    	}
    
    	ifindex = ifr.ifr_ifindex;
    
    	printf("Successfully got interface index: %i\n", ifindex);
    
    	
    
    	/*retrieve corresponding MAC*/
    
    	if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
    
    		perror("SIOCGIFINDEX");
    
    		exit(1);
    
    	}
    
            for (i = 0; i < 6; i++) {
    
    		src_mac[i] = ifr.ifr_hwaddr.sa_data[i];
    
    	}
    
    	printf("Successfully got our MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n", 
    
    			src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);
    
    
    
    	/*prepare sockaddr_ll*/
    
    	socket_address.sll_family   = PF_PACKET;
    
    	socket_address.sll_protocol = htons(ETH_P_IP);
    
    	socket_address.sll_ifindex  = ifindex;
    
    	socket_address.sll_hatype   = ARPHRD_ETHER;
    
    	socket_address.sll_pkttype  = PACKET_OTHERHOST;
    
    	socket_address.sll_halen    = ETH_ALEN;
    
    	socket_address.sll_addr[6]  = 0x00; 
    
    	socket_address.sll_addr[7]  = 0x00;
    
    														
    
    	
    
    	/*establish signal handler*/
    
    	signal(SIGINT, sigint);
    
    	printf("Successfully established signal handler for SIGINT\n");
    
    		
    
    	printf("We are in production state, waiting for incoming packets....\n");
    	pthread_create(&tid[1], NULL, thread1, NULL);
    	pthread_create(&tid[0], NULL, thread2, NULL);
    
    
    
    	while (1) {
    
    		/*Wait for incoming packet...*/	
    
    		length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
    
    		if (length == -1) {
    
    			perror("recvfrom():");
    
    			exit(1);
    
    		}
    
    		
    
    		/*See if we should answer (Ethertype == 0x0 && destination address == our MAC)*/
    
    		if (eh->h_proto == ETH_P_NULL && memcmp( (const void*)eh->h_dest, (const void*)src_mac, ETH_MAC_LEN) == 0 ) {
    
    			//printf("%02X sent\n", (void*)etherhead[14]);
    			
    			if((void*)etherhead[14] == 00){
    				write(sndpipe[1],(void*)etherhead,BUF_SIZE);
    			}
    			if((void*)etherhead[14] == 01){
    				write(commpipe[1],(void*)etherhead,BUF_SIZE);
    			}
    
    			
    
    			/*exchange addresses in buffer*/
    
    			memcpy( (void*)etherhead, (const void*)(etherhead+ETH_MAC_LEN), ETH_MAC_LEN);
    
    			memcpy( (void*)(etherhead+ETH_MAC_LEN), (const void*)src_mac, ETH_MAC_LEN);
    
    			
    
    			/*prepare sockaddr_ll*/
    
    			socket_address.sll_addr[0]  = eh->h_dest[0];
    
    			socket_address.sll_addr[1]  = eh->h_dest[1];
    
    			socket_address.sll_addr[2]  = eh->h_dest[2];
    
    			socket_address.sll_addr[3]  = eh->h_dest[3];
    
    			socket_address.sll_addr[4]  = eh->h_dest[4];
    
    			socket_address.sll_addr[5]  = eh->h_dest[5];
    
    
    
    			/*send answer*/
    
    			sent = sendto(s, buffer, length-4, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
    
    			if (sent == -1) {
    
    				perror("sendto():");
    
    				exit(1);
    
    			}
    
    			
    
    			answered_packets++;
    			
    
    		}
    
    
    
    		total_packets++;
    		
    
    	}
    
    }
    
    
    void * thread1(void* parm){
    	while(1){
    		read(commpipe[0], buf, BUF_SIZE);
    		//printf("%s  thread1\n",buf);
    		printf("thread1\n");
    		//pthread_exit(0);
    		
    	}
    
    }
    
    void* thread2(void* parm){
    	while(1){
    		read(sndpipe[0], bug,BUF_SIZE);
    		//printf("%s  thread2\n",bug);
    		printf("thread2\n");
    		//pthread_exit(0);
    	}
    }
    
    
    
    
    
    
    
    void sigint(int signum) {
    
    	/*Clean up.......*/
    
    
    
    	struct ifreq ifr;
    
    
    
            if (s == -1)
    
            	return;
    
    	
    
    	strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
    
    	ioctl(s, SIOCGIFFLAGS, &ifr);
    
    	ifr.ifr_flags &= ~IFF_PROMISC;
    
    	ioctl(s, SIOCSIFFLAGS, &ifr);
    
    	close(s);
    
    
    
    	free(buffer);
    
    	
    
    	printf("Server terminating....\n");
    
    
    
    	printf("Totally received: %d packets\n", total_packets);
    
    	printf("Answered %d packets\n", answered_packets);
    
    	exit(0);
    
    }


    and client.c code

    Code:
    #include <sys/socket.h>
    
    #include <sys/ioctl.h>
    
    #include <sys/time.h>
    
    
    
    #include <asm/types.h>
    #include "client.h"
    
    #include "util.h"
    
    #include <math.h>
    
    #include <string.h>
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <unistd.h>
    
    #include <signal.h>
    
    
    
    #include <linux/if_packet.h>
    
    #include <linux/if_ether.h>
    
    #include <linux/if_arp.h>
    
    
    
    #define BUF_SIZE ETH_FRAME_TOTALLEN
    
    #define NUMBER_OF_MESUREMENTS_PER_AMOUNT_OF_DATA 100000 /*how often to measure travelling time with one certain amount of data*/
    
    
    
    int s = 0; /*Socketdescriptor*/
    
    void* buffer = NULL;
    
    long total_sent_packets = 0;
    
    
    
    int main(void) {
    
    	buffer = (void*)malloc(BUF_SIZE); 	/*Buffer for ethernet frame*/
    
    	unsigned char* etherhead = buffer;	/*Pointer to ethenet header*/
    
    	unsigned char* data = buffer + 14;	/*Userdata in ethernet frame*/
    
    	struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to ethernet header*/
    
    
    
    	unsigned char src_mac[6];		/*our MAC address*/
    
    	unsigned char dest_mac[6] = {0x00, 0x19, 0xB9, 0x3D, 0xB7, 0x7B};	/*other host MAC address, hardcoded...... :-(*/
    
    
    
    	struct ifreq ifr;
    
    	struct sockaddr_ll socket_address;
    
    	int ifindex = 0;			/*Ethernet Interface index*/
    
    	int i,j,k;
    
    	int length;				/*length of received packet*/
    
    	int sent;				/*length of sent packet*/
    
    
    
            unsigned long long allovertime;
    
    				
    
    
    
    	printf("Client started, entering initialiation phase...\n");
    
    
    
    	/*open socket*/
    
    	s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    
    	if (s == -1) {
    
    		perror("socket():");
    
    	        exit(1);
    
    	}
    
    	printf("Successfully opened socket: %i\n", s);
    
    	
    
    	/*retrieve ethernet interface index*/
    
    	strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
    
    	if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
    
    		perror("SIOCGIFINDEX");
    
    		exit(1);
    
    	}
    
    	ifindex = ifr.ifr_ifindex;
    
    	printf("Successfully got interface index: %i\n", ifindex);
    
    	
    
    	/*retrieve corresponding MAC*/
    
    	if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
    
    		perror("SIOCGIFINDEX");
    
    		exit(1);
    
    	}
    
            for (i = 0; i < 6; i++) {
    
    		src_mac[i] = ifr.ifr_hwaddr.sa_data[i];
    
    	}
    
    	printf("Successfully got our MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n", 
    
    			src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);
    
    
    
    	/*prepare sockaddr_ll*/
    
    	socket_address.sll_family   = PF_PACKET;
    
    	socket_address.sll_protocol = htons(ETH_P_IP);
    
    	socket_address.sll_ifindex  = ifindex;
    
    	socket_address.sll_hatype   = ARPHRD_ETHER;
    
    	socket_address.sll_pkttype  = PACKET_OTHERHOST;
    
    	socket_address.sll_halen    = ETH_ALEN;
    
    	socket_address.sll_addr[0]  = dest_mac[0];
    
    	socket_address.sll_addr[1]  = dest_mac[1];
    
            socket_address.sll_addr[2]  = dest_mac[2];
    
            socket_address.sll_addr[3]  = dest_mac[3];
    
            socket_address.sll_addr[4]  = dest_mac[4];
    
            socket_address.sll_addr[5]  = dest_mac[5];
    
    	socket_address.sll_addr[6]  = 0x00; 
    
    	socket_address.sll_addr[7]  = 0x00;
    
    														
    
    	
    
    	/*establish signal handler*/
    
    	signal(SIGINT, sigint);
    
    	printf("Successfully established signal handler for SIGINT\n");
    
    
    
    	/*init random number generator*/
    
    	srand(time(NULL));
    
    		
    
    	printf("We are in production state, sending packets....\n");
    
    
    
    	for (i = 50; i <= 1500; i += 50) {
    
    
    
    		allovertime = 0;
    
    		sleep(1);
    
    		//for (k = 0; k < NUMBER_OF_MESUREMENTS_PER_AMOUNT_OF_DATA; k++) {
    
    			/*prepare buffer*/
    
    			memcpy((void*)buffer, (void*)dest_mac, ETH_MAC_LEN);
    
    			memcpy((void*)(buffer+ETH_MAC_LEN), (void*)src_mac, ETH_MAC_LEN);
    
    			eh->h_proto = ETH_P_NULL;
    
    			/*fill it with random data....*/
    
    			for (j = 0; j < 20; j++) {
    
    				data[j] = (unsigned char)((int) (255.0*rand()/(RAND_MAX+1.0)));
    				
    
    			}
    
    			data[0] = (unsigned char)(rand()%2);
    
    			/*send packet*/
    
    			sent = sendto(s, buffer, i+ETH_HEADER_LEN, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
    
    			if (sent == -1) {
    
    				perror("sendto():");
    
    				exit(1);
    
    			}
    
    				
    
    			/*Wait for incoming packet...*/
    
    			length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
    
    			if (length == -1) {
    
    				perror("recvfrom():");
    
    				exit(1);
    
    			}
    
    
    
    	
    
    			total_sent_packets++;
    
    		//}
    
    
    		printf("total sent packets: %d\n", total_sent_packets);
    
    	}
    
    }
    
    
    
    void sigint(int signum) {
    
    	/*Clean up.......*/
    
    
    
    	struct ifreq ifr;
    
    
    
            if (s == -1)
    
            	return;
    
    	
    
    	strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
    
    	ioctl(s, SIOCGIFFLAGS, &ifr);
    
    	ifr.ifr_flags &= ~IFF_PROMISC;
    
    	ioctl(s, SIOCSIFFLAGS, &ifr);
    
    	close(s);
    
    
    
    	free(buffer);
    
    	
    
    	printf("Client terminating....\n");
    
    
    
    	printf("Totally sent: %d packets\n", total_sent_packets);
    
    	exit(0);
    
    }

    So the issue is that I'm just trying to have it so that once the client sends a packet to the server its going ot be random data. But I want the server to pretend to process the packets by forwarding then to the two threads. but using a compare on one of hte data[0] And right now the threads just print whether they go up or not. My client is sending a packet every 1 second i think because i sleep(1) before it sends. so in my client terminal it just counts down 1 ... 2 ...3 every second. But in my server terminal Its constantly printing out thread1/thread2 none stop even after my client program finished aand stop sending I wanted my server code to just pop thread1/ thread2 .........thread1/thread2 ...... every second now infinitely. but i also dont want to close any threads cuz i want them to be continously running y didnt the pipes stop its from constantly printing?

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Wow that's a lot of code. I skimmed through it and this is just a guess, but the server needs to check if recvfrom returns 0. That, not -1, is the indication that the client has closed its connection. The server repeatedly processes the last data it received from the client.

    It's not necessary to send data through a pipe to threads. The threads and the server are all in the same memory space. Do you have a reason for using the pipes?

    I hope that helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with socket descriptors
    By McKracken in forum C Programming
    Replies: 1
    Last Post: 07-22-2009, 08:51 AM
  2. Using threads for control input (keyboard, serial)
    By synthetix in forum C Programming
    Replies: 1
    Last Post: 07-06-2009, 07:43 PM
  3. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  4. Socket program
    By mhetfield in forum C Programming
    Replies: 5
    Last Post: 04-03-2007, 03:46 PM
  5. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 01:38 PM