sockets

sockets

Post by E.. » Tue, 28 Jan 1992 12:24:29



I am trying to write a programm in which a client tries connecting
to a server which may or may not be listening. If the server is not
listening then I want that the client should keep on trying to establish
a connection every, say , x secs. How can I make my C programm call
a procedure every x seconds or so till a connection is established ?
     Thanks in advance for any help ||
 
 
 

sockets

Post by Marcus J. Ran » Tue, 28 Jan 1992 12:06:21



>I am trying to write a programm in which a client tries connecting
>to a server which may or may not be listening. If the server is not
>listening then I want that the client should keep on trying to establish
>a connection every, say , x secs. How can I make my C programm call
>a procedure every x seconds or so till a connection is established ?

#include        <stdio.h>
#include        <ctype.h>
#include        <sys/types.h>
#include        <sys/socket.h>
#include        <netdb.h>
#include        <netinet/in.h>

main(ac,av)
int     ac;
char    *av[];
{
        int     server_fd = -1;

        while((server_fd = conn_server(av[1],25)) < 0) {
                if(server_fd == -2) {
                        fprintf(stderr,"unknown host: %s\n");
                        exit(1);
                }

                sleep(60);
        }

        printf("connected to %s: port %d\n",av[1],25);
        exit(0);

Quote:}

/* connect to a named server - return(-2) if host unknown, other return
values can be based on errno.*/
conn_server(srv,port)
char    *srv;
short   port;
{
        struct  sockaddr_in     addr;
        struct  servent         *sp;
        struct  hostent         *hp;
        int                     fd;
        char                    *p;

        p = srv;
        while(*p != '\0' && (*p == '.' || isdigit(*p)))
                p++;

        /* not all digits or dots */
        if(*p != '\0') {
                if((hp = gethostbyname(srv)) == (struct hostent *)0)
                        return(-2);
                (void)bcopy(hp->h_addr,(char *)&addr.sin_addr,hp->h_length);
        } else {
                unsigned long   f;
                if((f = inet_addr(srv)) == -1L)
                        return(-2);
                (void)bcopy((char *)&f,(char *)&addr.sin_addr,sizeof(f));
        }
        addr.sin_port = htons(port);
        addr.sin_family = AF_INET;
        if((fd = socket(AF_INET,SOCK_STREAM,0)) < 0)
                return(-1);
        if(connect(fd,&addr,sizeof(addr)) < 0)
                return(-1);
        return(fd);

Quote:}

--
        "Fault Tolerant Computing" means having redundant coffee machines
and at least two good Szechuan restaurants within easy walking distance.
                                                -notebooks of a heretic

 
 
 

sockets

Post by Dan Bernste » Tue, 28 Jan 1992 13:40:57



Quote:>    if((fd = socket(AF_INET,SOCK_STREAM,0)) < 0)
>            return(-1);
>    if(connect(fd,&addr,sizeof(addr)) < 0)
>            return(-1);
>    return(fd);

One problem with this code is that if the connect fails then fd will
still be a valid socket. You'll run out of file descriptors after
calling this routine enough times.

---Dan