ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
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'.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.