Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Open_clientfd_ts(char *hostname, int port)
- {
- int clientfd;
- struct sockaddr_in serveraddr;
- struct hostent hostent, *hp = &hostent;
- struct hostent *temp_hp;
- if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- return -1; /* check errno for cause of error */
- /*lock-and-copy for thread safety*/
- P(&mutex); /* lock P(&mutex) */
- temp_hp = gethostbyname(hostname);
- if (temp_hp != NULL)
- hostent = *temp_hp; /* copy */
- V(&mutex); /*V(&mutex)*/
- /* Fill in the server's IP address and port */
- if ((hp = gethostbyname(hostname)) == NULL)
- return -2; /* check h_errno for cause of error */
- bzero((char *) &serveraddr, sizeof(serveraddr));
- serveraddr.sin_family = AF_INET;
- bcopy((char *)hp->h_addr_list[0],
- (char *)&serveraddr.sin_addr.s_addr, hp->h_length);
- serveraddr.sin_port = htons(port);
- /* Establish a connection with the server */
- if (connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)) < 0)
- return -1;
- return clientfd;
- }
Add Comment
Please, Sign In to add comment