Find the answer to your Linux question:
Results 1 to 6 of 6
Hello all, I am trying to run the following datagram server program with threading. But I am getting segmentation fault after the following line: if((n= recvfrom(rec, buf, 1024, 0, (struct ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    5

    Threading Segmentation fault

    Hello all,

    I am trying to run the following datagram server program with threading. But I am getting segmentation fault after the following line:

    if((n= recvfrom(rec, buf, 1024, 0, (struct sockaddr *)&from_addr, &Size)) < 0)

    Would be glad if anyone can make out where I am doing wrong.

    Thanks all in advance!

    HTML Code:
    [CODE]
    Following is the code:
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<netdb.h>
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<errno.h>
    #include<pthread.h>
    
    void *Recv_Handle(void *);
    
    int main(int argc, char *argv[])
    {
    	int length;
    	pthread_t thread;
    	struct sockaddr_in receive_addr, send_addr, addr;
    	int sendSOCKET, receiveSOCKET;
    	int rc;
    	printf("hey1");
    	//receiving stuff
    	if((receiveSOCKET = socket(AF_INET, SOCK_DGRAM, 0))<0)
    	{
    		error("Socket");
    		exit(1);
    	}
    	printf("hello2");
    
    	receive_addr.sin_family = AF_INET;
    	receive_addr.sin_port = htons(5000);
    	receive_addr.sin_addr.s_addr = INADDR_ANY;// local ip address
    	bzero(&receive_addr, length);
    	printf("hello 3");
    
    	length = sizeof(receive_addr);
    	if((bind(receiveSOCKET, (struct sockaddr *)&receive_addr, length)) <0)
    	{
    		error("unable to bind");
    		exit(1);
    	}
    	//creat6e a thread to handle the receiving stufff
    	rc = pthread_create(&thread, NULL, Recv_Handle, (void *)&receiveSOCKET);
    	printf("hello3");	
    		
    close(receiveSOCKET);
    pthread_exit(NULL);
    }
    
    void *Recv_Handle(void *received)
    {
    	int rec;
    	int n;
    	char buf[1024];
    	printf("hello 4");	
    	rec = *(int *)received;
    	struct sockaddr_in from_addr;
    	int Size;
    	printf("hello 6");
    	while(1)
    	{
    		Size = sizeof(from_addr);
    		printf("hello 7");
    		if((n= recvfrom(rec, buf, 1024, 0, (struct sockaddr *)&from_addr, &Size)) < 0)
    		{
    			error("recvfrom");
    			exit(1);
    		}
    		printf("hello 8");
    		if((n = sendto(rec, "got ur message\n", 17, 0, (struct sockaddr *)&from_addr, sizeof(struct sockaddr))) <0)
    		{
    		
    		error("sendto");
    		exit(1);
    		}
    	printf("hello 9");
    	}
    	pthread_exit(NULL);
    }
    
    }[/CODE]

  2. #2
    Just Joined!
    Join Date
    Jan 2010
    Posts
    5
    Thanku for replying, bt. now it is giving following error in the same statement when compiled:

    error: cannot convert to a pointer type.

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    8
    true .. it was my mistake

  4. #4
    Just Joined!
    Join Date
    Dec 2008
    Posts
    8
    bzero(&receive_addr, length);

    length ??? Please initialize the variable. I this this might have cause the prob

  5. #5
    Just Joined!
    Join Date
    Jan 2010
    Posts
    5
    Hey thanks a lott Parveen for replying.

    Bt. I did declare length, bt. the problem in the code was that the calling thread was exiting and with the that the thread I spawned was exiting as well.
    So used pthread_join and it is working fine now.

    Thanks for replying

  6. #6
    Just Joined!
    Join Date
    Dec 2008
    Posts
    8
    oh ... true.. thanks to u too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...