Hi,
My application is irregularly receiving a SIGKILL from the system and i haven't been able to figure out how or where but I have a suspecion that my function connecttoserver() is responsible for this but i couldn't figure out why or what...
the function looks as pasted below. Can anyone see what may be leading to that kind of behaviour or should i be looking at something else?
Code:
int connecttoserver(char *hostname,int port)
{
struct hostent *he;
struct sockaddr_in server;
struct protoent *pr;
int MySocket=-1;
static int old_port=0;
//static char *old_hostname;
/* resolve localhost to an IP (should be 127.0.0.1) */
if ((he = gethostbyname(hostname)) == NULL) {
printf("error resolving hostname..\n");
syslog(LOG_NOTICE,"nlog: error resolving hostname..");
return -1;
}
/*
* copy the network address part of the structure to the
* sockaddr_in structure which is passed to connect()
*/
memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length);
server.sin_family = AF_INET;
server.sin_port = htons(port);
pr = getprotobyname("tcp");
if (!pr) {
printf("getpotobyname() failed...!\n");
syslog(LOG_NOTICE,"nlog: getpotobyname() failed...");
MySocket=-1;
return -1;
}
/* create socket */
if ( (MySocket = socket(PF_INET, SOCK_STREAM, pr->p_proto)) == -1) {
syslog(LOG_NOTICE,"nlog: error creating socket...,errno: %s!\n", strerror( errno ));
printf("error creating socket...,errno: %s!\n", strerror( errno ));
MySocket=-1;
return -1;
}
/* connect */
if (connect(MySocket, (struct sockaddr *)&server, sizeof(server))) {
if (old_port!=port){
syslog(LOG_NOTICE,"nlog: error connecting to %s on port %d...,errno: %s!\n", hostname, port, strerror( errno ));
old_port=port;
}
MySocket=-1;
return -1;
}
return MySocket;
}
Thank you!
roN