#include #include #include #include #include #include #include #include #include #include #include int make_connection(int port, char *machine) { struct sockaddr_in sin; /* internet endpoint address */ struct hostent *phe; /* name of host to which connection desired */ int descriptor; int flag; bzero((char *) &sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = (unsigned short) port; /* Map hostname to IP address, allowing for dotted decimal */ phe = gethostbyname(machine); bcopy(phe->h_addr, (char*)&sin.sin_addr, phe->h_length); /* allocate a socket */ descriptor = socket(AF_INET, SOCK_STREAM, 0); if( descriptor < 0 ) { printf("cannot allocate socket\n"); return NULL; } /* Connect the socket */ flag = connect(descriptor, (struct sockaddr *)&sin, sizeof(sin)); if(flag < 0) { printf("Connect has failed\n"); return NULL; } return descriptor; }