Guest User

Untitled

a guest
Jul 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. int Open_clientfd_ts(char *hostname, int port)
  2. {
  3. int clientfd;
  4. struct sockaddr_in serveraddr;
  5. struct hostent hostent, *hp = &hostent;
  6. struct hostent *temp_hp;
  7.  
  8.  
  9. if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  10. return -1; /* check errno for cause of error */
  11.  
  12. /*lock-and-copy for thread safety*/
  13. P(&mutex); /* lock P(&mutex) */
  14. temp_hp = gethostbyname(hostname);
  15. if (temp_hp != NULL)
  16. hostent = *temp_hp; /* copy */
  17. V(&mutex); /*V(&mutex)*/
  18.  
  19.  
  20. /* Fill in the server's IP address and port */
  21. if ((hp = gethostbyname(hostname)) == NULL)
  22. return -2; /* check h_errno for cause of error */
  23. bzero((char *) &serveraddr, sizeof(serveraddr));
  24. serveraddr.sin_family = AF_INET;
  25. bcopy((char *)hp->h_addr_list[0],
  26. (char *)&serveraddr.sin_addr.s_addr, hp->h_length);
  27. serveraddr.sin_port = htons(port);
  28.  
  29. /* Establish a connection with the server */
  30. if (connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)) < 0)
  31. return -1;
  32. return clientfd;
  33. }
Add Comment
Please, Sign In to add comment