?
Solved

How to prevent connect() function from core dumping?

Posted on 2002-03-20
5
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Accepted Solution

by:
newmang earned 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

764 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