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("22.214.171.124");
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");