Solved

How to prevent connect() function from core dumping?

Posted on 2002-03-20
5
211 Views
Last Modified: 2010-04-22
I'm trying to write a sockets client to access a remote server. The client program is on Red-Hat Linux Release 6.2 (kernel 2.2.14-5.0smp on an i686). If I enter a non existent ip address to connect to, the connect function core dumps. I want the connect program to accept any address.
How do I prevent this from happening and instead trap the error properly?

my code is something like:
..
        clnt.sin_family = AF_INET;
        clnt.sin_port = htons (5555);
        clnt.sin_addr.s_addr = inet_network("1.1.11.101");
        printf("addr = %s\n",inet_ntoa(clnt.sin_addr)); /* just check the address */

       if (connect(sock, (struct sockaddr *) &clnt, sizeof (clnt)) < 0)
         {
           perror ("connect error");
           return -1;
         }

0
Comment
Question by:ngcl9681
5 Comments
 
LVL 4

Accepted Solution

by:
newmang earned 100 total points
ID: 6884701
My experience is that connect() does not (should not?) core on an invalid ip address. Have you run the code through a debugger to see where the actual core is taking place (or run the core file through the debugger)?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6884838
> inet_ntoa(clnt.sin_addr)
should this be
  inet_ntoa(clnt.sin_addr.s_addr)
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6886851
ahoffmann,
inet_ntoa(clnt.sin_addr)  is correct.  clnt.sin_addr is a struct in_addr, which is the correct parameter for inet_ntoa.

ngcl,
I would recommend changing the following line due to byte ording issues:
   clnt.sin_addr.s_addr = inet_network("1.1.11.101");
to:
   inet_aton("1.1.11.101", &clnt.sin_addr);

This is because inet_network returns a long Host Byte Order, and you actually need it to be in network byte order for the assignment.  You could also accomplish the same thing with the following line (just for clarity), however, it is less efficient than the other example I gave you.

   clnt.sin_addr.s_addr = htonl(inet_network("1.1.11.101"));

Not sure why the core dump is happeneing though, but still looking.  Can you show us the code you are using to create the socket?


0
 

Author Comment

by:ngcl9681
ID: 6887437
Both newmanq and BlackDiamond are correct. The actual cause was not the connect() statement. Upon further investigation, I discovered that the return value of -1 was put into the list of sockets for a select statement. This was the actual cause of the dump.
How can I award the points for the answer? Can I click accept as answer twice or give to the first correct answer. I was not able to accept it earlier as I needed time to go through my code. Is there a way to share points? BlackDiamond's tip on the ip addressing is really helpful to improve the clarity of my program.
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6891470
I am glad the infomation was helpful.

As far as spiltting points, you can etiher post a new message with more points, or post a message in the community support area asking a moderator to split the points up for you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now