Solved

How to prevent connect() function from core dumping?

Posted on 2002-03-20
5
208 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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