[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to prevent connect() function from core dumping?

Posted on 2002-03-20
5
Medium Priority
?
229 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
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…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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