esock_new.c 539 B

123456789101112131415161718192021222324
  1. #include "extreme.h"
  2. int esock_new(char *ip, int port) {
  3. int sockfd;
  4. struct hostent *he;
  5. struct sockaddr_in their_addr;
  6. if ((he = gethostbyname(ip)) == NULL)
  7. return (-1);
  8. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  9. their_addr.sin_family = AF_INET;
  10. their_addr.sin_port = htons(port);
  11. their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  12. bzero(&(their_addr.sin_zero), 8);
  13. if ((connect(sockfd, (struct sockaddr *)&their_addr,
  14. sizeof(struct sockaddr))) == -1)
  15. return (-1);
  16. return (sockfd);
  17. }