Threading Segmentation fault


 
Thread Tools Search this Thread
Top Forums Programming Threading Segmentation fault
# 1  
Old 01-12-2010
Threading Segmentation fault

Hello All,

I am getting segmentation fault after the following lien when I try to run my datagram socket server program using threads:

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

I am not able to figure out what exactly is causing the problem.
Would be glad if anyone can help me figure it out!

Following is my server code:

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);
}

# 2  
Old 01-12-2010
Size should be a socklen_t.

I also don't see that main() is waiting for the receiving thread to finish... It should. You can't create a thread and quit the same way you can fork a process and quit because when main's thread quits, the entire process is gone.
# 3  
Old 01-12-2010
Try pthread_join() to wait for the thread to end.
# 4  
Old 01-12-2010
yes, it works with with pthread_join and changed my Size type to socklen_tSmilie

thanks a lotttttttttttt to everyone for inputsSmilieSmilie
# 5  
Old 01-14-2010
actually it dosen't work properlySmilie..................I'm getting SIGSEGV, after segmentation faultSmilie..............even thought I joined the thread!
# 6  
Old 01-14-2010
Any idea where the segfault happens? compile with -ggdb, and run with 'gdb ./filename', start it with 'run' inside gdb, then 'bt f' after it crashes to see a backtrace.
# 7  
Old 01-14-2010
I'm getting an error:

"no symbol table info avalaible" : no idea what it isSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

3. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

4. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

5. UNIX for Dummies Questions & Answers

Segmentation fault

#include<stdio.h> #include<malloc.h> #include<unistd.h> #include<stdlib.h> void *start_1(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); pthread_exit((void*)1); } void *start_2(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); return (void*)2; }... (2 Replies)
Discussion started by: vincent__tse
2 Replies

6. Programming

segmentation fault

If I do this. Assume struct life { char *nolife; } struct life **life; // malloc initialization & everything if(life->nolife == 0) Would I get error at life->nolife if it is equal to 0. wrong accession? (3 Replies)
Discussion started by: joey
3 Replies

7. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies

8. Linux

Segmentation fault

Hi, on a linux Red HAT(with Oracle DB 9.2.0.7) I have following error : RMAN> delete obsolete; RMAN retention policy will be applied to the command RMAN retention policy is set to redundancy 2 using channel ORA_DISK_1 Segmentation fault What does it mean ? And the solution ? Many thanks. (0 Replies)
Discussion started by: big123456
0 Replies

9. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

10. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies
Login or Register to Ask a Question

Featured Tech Videos