LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   segmentation fault while sending UDP packets! (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-while-sending-udp-packets-740494/)

bizoo 07-16-2009 05:35 AM

segmentation fault while sending UDP packets!
 
The following piece of code is suppose to send a UDP packet.
but inside function udpsocketinit , i get a segmentation fault and i can not understand why!!
please help me

Code:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
   
#define BUFLEN 512
#define NPACK 10
#define PORT 9930
#define SRV_IP "127.0.0.1" 
void udpsocketinit (struct sockaddr_in *sok)
{
        struct in_addr *xyz;
//        memset((char *) sok, 0, sizeof(sok));
        sok->sin_family = AF_INET;
        sok->sin_port = htons(PORT);
        xyz=&(*sok).sin_addr;
        if (inet_aton(SRV_IP, xyz)==0)
        {
          printf("the ip address is wrong\n");
        }

}

   
int main(void)
 {
        struct sockaddr_in *si_other;
        int s, i, slen=sizeof(si_other);
        char buf[BUFLEN];
   
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
            printf("socket is not initialised\n");
        udpsocketinit(si_other);

   
        for (i=0; i<NPACK; i++)
        {
          printf("Sending packet %d\n", i);
          sprintf(buf, "This is packet %d\n", i);
          if (sendto(s, buf, BUFLEN, 0, si_other, slen)==-1)
                printf("socket bind failed!\n");
        }
   
        close(s);
        return 0;
}


David1357 07-16-2009 08:30 AM

Try this:
Code:

int udpsocketinit(struct sockaddr_in *sok)
{
    struct in_addr xyz;

    if (inet_aton(SRV_IP, &xyz) == 0)
    {
        printf("the ip address is wrong\n");
        return -1;
    }

    sok->sin_addr = xyz;
    sok->sin_family = AF_INET;
    sok->sin_port = htons(PORT);
    return 0;
}

int main(void)
{
    struct sockaddr_in si_other;
    int s, i, slen = sizeof(si_other);
    char buf[BUFLEN];
    int ret;

    ret = -1;

    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        printf("socket is not initialised\n");
        goto socket_err;
    }

    if (udpsocketinit(si_other))
    {
        goto init_err;
    }

    for (i = 0; i < NPACK; i++)
    {
        printf("Sending packet %d\n", i);
        sprintf(buf, "This is packet %d\n", i);
        if (sendto(s, buf, BUFLEN, 0, &si_other, slen) == -1)
        {
            printf("socket bind failed!\n");
            goto send_err;
        }
    }

    ret = 0;
    goto done;

send_err:
init_err:
done:
    close(s);

socket_err:
    return(ret);
}


johnsfine 07-16-2009 08:42 AM

Quote:

Originally Posted by bizoo (Post 3609340)
struct sockaddr_in *si_other;

That just defines a pointer to that structure type. It does not make the pointer point at anything.

Quote:

int s, i, slen=sizeof(si_other);
That sets slen to the size of a pointer. Is that what you intend, or did you intend slen to be the size of the type of object that the pointer would point to?

Quote:

udpsocketinit(si_other);
You use that pointer as if it is the address of an actual object, but it is just an uninitialized pointer.

Wim Sturkenboom 07-16-2009 08:43 AM

In main you declare a pointer to struct sockaddr_in (si_other) that is never initialized (so pointing to somewhere in memory). Next you try (in udpsockinit) to assign a value to an element of that struct so writing to memory that more than likely is not 'yours'.

David1357 07-16-2009 11:19 AM

bizoo,

I made some changes to my earlier post. They should fix your problems. However I have not tested them.

Good luck.

bizoo 07-17-2009 03:33 AM

thanks all esp. David
Yup the problem indeed was initializing a pointer but not directing it to some struct


All times are GMT -5. The time now is 04:12 PM.