LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2004, 10:45 PM   #1
slackwarefan
Member
 
Registered: Oct 2003
Location: Florida
Distribution: Slackware
Posts: 273

Rep: Reputation: 30
Why does this segmntation fault?


I don't understand why, but this segmentation faults after printing hello.
Any help appreciated.

#include <stdio.h>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>

#define MYPORT 23

class sockets
{
public:

int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in dest_addr;
sockets();
~sockets();
int smake(void);
int sconnect(void);
protected:

};

int sockets::smake()
{
printf("works");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
return sockfd;
}

int sockets::sconnect(void)
{
int stats;
printf("Enter Destination Address\n");
scanf("%s", &dest_addr.sin_addr.s_addr);
stats = connect(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
return stats;
}

sockets::sockets()
{
printf("Hello2");
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = inet_addr(INADDR_ANY);
memset(&(my_addr.sin_zero), '\0', 8);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(MYPORT);
dest_addr.sin_addr.s_addr = inet_addr("255.255.255.255");
memset(&(my_addr.sin_zero), '\0', 8);

}

sockets::~sockets()
{
}

class ttext
{
public:

ttext();

~ttext();

char out(char *intext);

protected:

};

ttext::ttext()
{
}

ttext::~ttext()
{
}

char ttext:ut(char *intext)
{

printf("%s\n", intext);

}

int main()
{

char testt[] = "Hello";

ttext t;

t.out(testt);

sockets sock1;

//sock1.smake();
//sock1.sconnect();

return 0;

}
 
Old 06-08-2004, 01:34 AM   #2
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
I think the error lies here -
my_addr.sin_addr.s_addr = inet_addr(INADDR_ANY);

I don't think you can use INADDR_ANY as an argument for inet_addr. The actual value of INADDR_ANY is actually equal to 0. The argument to inet_addr should be an IP address in the form of A.B.C.D. Try reading the related man page.
 
Old 06-08-2004, 02:28 AM   #3
slackwarefan
Member
 
Registered: Oct 2003
Location: Florida
Distribution: Slackware
Posts: 273

Original Poster
Rep: Reputation: 30
Thanks, It apperas as thought that fixed it.
 
Old 06-08-2004, 02:34 AM   #4
ashwinipahuja
Member
 
Registered: Mar 2004
Posts: 79

Rep: Reputation: 15
segmentation fault occurs whenever the memory address passed is not correct . debug the code and u will see that there is some garbage value instead of the correct address
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
segmentation fault pippet Programming 4 01-24-2005 01:02 AM
Segmentation fault sin-x Slackware 2 01-12-2005 03:01 PM
What is segmentation fault? koyi Programming 3 08-30-2003 12:03 AM
segmentation fault... perdesiz Linux - Software 1 08-18-2003 01:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration