connection.c
changeset 111 2247473fd12d
parent 84 f59d8a8e786f
child 131 b5ad49852adc
equal deleted inserted replaced
110:e1bc09732fa3 111:2247473fd12d
    10 #include <string.h>
    10 #include <string.h>
    11 
    11 
    12 #include <netdb.h>
    12 #include <netdb.h>
    13 #include <arpa/inet.h>
    13 #include <arpa/inet.h>
    14 
    14 
       
    15 #include <netinet/in.h>
       
    16 #include <sys/types.h>
       
    17 #include <sys/socket.h>
       
    18 
    15 int connection (char *ip, unsigned short port)
    19 int connection (char *ip, unsigned short port)
    16 {
    20 {
    17     struct hostent *hs;
    21     struct hostent *hs;
    18     struct sockaddr_in sock;
    22     struct sockaddr_in sock;
    19 
    23 
    20     int sockfd;
    24     int sockfd;
    21 
    25 
    22     memset (&sock, 0, sizeof (sock));
    26     memset (&sock, 0, sizeof (sock));
    23     sock.sin_family = AF_INET;
    27     sock.sin_family = PF_INET;
    24     sock.sin_port = htons (port);
    28     sock.sin_port = htons (port);
    25 
    29 
    26     if ((sock.sin_addr.s_addr = inet_addr (ip)) == -1) {
    30     if ((sock.sin_addr.s_addr = inet_addr (ip)) == -1) {
    27         if ((hs = gethostbyname (ip)) == NULL) {
    31         if ((hs = gethostbyname (ip)) == NULL) {
    28             perror ("[-] Error");
    32             perror ("[-] Error");
    30         }
    34         }
    31         sock.sin_family = hs->h_addrtype;
    35         sock.sin_family = hs->h_addrtype;
    32         memcpy (&sock.sin_addr.s_addr, hs->h_addr_list[0], hs->h_length);
    36         memcpy (&sock.sin_addr.s_addr, hs->h_addr_list[0], hs->h_length);
    33     }
    37     }
    34 
    38 
    35     if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
    39     if ((sockfd = socket (PF_INET, SOCK_STREAM, 0)) < 0) {
    36         perror ("[-] Error");
    40         perror ("[-] Error");
    37         return -1;
    41         return -1;
    38     }
    42     }
    39 
    43 
    40     if (connect (sockfd, (struct sockaddr *)&sock, sizeof (sock)) < 0) {
    44     if (connect (sockfd, (struct sockaddr *)&sock, sizeof (sock)) < 0) {