Here is the code:

#include <iostream> #include <sys/types.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> using namespace std; int main(int argc, char* argv[]) { int sock; struct sockaddr_in addr; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { cout << "Socket ERROR!" << endl; return 1; } addr.sin_family = AF_INET; addr.sin_port = htons(7771); char host1[] = "example.com"; //здесь коректное имя хоста struct hostent* hostA = gethostbyname(host1); addr.sin_addr.s_addr = inet_addr(hostA->h_addr); cout << hostA->h_addr << endl; //выводит 2 символа на 2 строки(какая-то кракозябра) cout << addr.sin_addr.s_addr << endl; //число (для google.com вышло 4294967295) if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { cout << "Connect ERROR!" << endl; //тут все заканчивается. return 1; } ... return 0; } 

    4 answers 4

     HOSTENT *hosten; hosten = gethostbyname( name ); sa.sin_addr.s_addr = ((in_addr*)hosten->h_addr_list[0])->s_addr; 

    that's the way you can

    • So earned. Is it because hosten-> h_addr is in_addr_t and was in_addr-> s_addr needed? - Jakeroid
    • Yes, as you can see - Andrey Kuzmenko

    Instead

     cout << addr.sin_addr.s_addr << endl; //число (для google.com вышло 4294967295) 

    Write:

     cout << inet_ntoa(addr.sin_addr) << endl; //число (для google.com вышло 4294967295) 

    Displays 255.255.255.255. Which is quite logical. Because 4294967295 - the maximum long int number.

    PS: do we write under Linux / Windows or is it just portable?

    PPS: a working example is on http://msdn.microsoft.com/en-us/library/ms738524 (v = vs.85)

    • We write under Linux. - Jakeroid
     struct addrinfo hints, *res; char ipstr[INET6_ADDRSTRLEN]; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if((status = getaddrinfo(argv[1], NULL, &hints, &res)) != NO_ERROR) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 3; } for(p = res; p != NULL; p = p->ai_next) { void *addr; char *ipver; if(p->ai_family == AF_INET) { struct sockaddr_in *ipv4 = (struct sockaddr_in*)p->ai_addr; addr = &(ipv4->sin_addr); ipver = "IPv4"; } else { struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)p->ai_addr; addr = &(ipv6->sin6_addr); ipver = "IPv6"; } inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); printf(" %s: %s\n", ipver, ipstr); } freeaddrinfo(res); 

    Something like that. Works on IPv6.

      A piece of a really working program. Call: int sock = Connect ("host: port");

      Host can be by name or by type 10.1.3.122

      Includs are actually a bit much, probably many are not needed for this piece.

       #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/time.h> #include <unistd.h> #include <fcntl.h> #include <ctype.h> #include <errno.h> #ifdef WIN32 // gcc ... -lws2_32 -lwsock32 #include <winsock2.h> #include <ws2tcpip.h> #include <windows.h> #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #endif #ifdef WIN32 void Clean_tcp () { while (WSACleanup() == 0); } #endif static int cleanup = 0; int Connect (char *addr) { char *port, host[100]; struct sockaddr_in sin; struct hostent *h; short p; int conn; if (!cleanup) { #ifdef WIN32 WSADATA wsaData; WSAStartup (MAKEWORD(2,2),&wsaData); atexit (Clean_tcp); #endif cleanup = 1; } strncpy (host,addr,99); host[99] = 0; if (!(port = strchr(host,':'))) return -1; *port++ = 0; conn = socket ( AF_INET, SOCK_STREAM , 0 ); if ( conn < 0 ) { perror("socket"); exit (1); } memset ((char *)&sin,0,sizeof(sin)); sin.sin_family = AF_INET; if (!host[0]) sin.sin_addr.s_addr = inet_addr("127.0.0.1"); else { if ((sin.sin_addr.s_addr = inet_addr(host)) == -1) { errno = 0; if (h = gethostbyname(host)) memcpy((char *)&sin.sin_addr.s_addr,h->h_addr,h->h_length); else { if (errno) perror ("gethostbyname"); else fprintf (stderr,"Unknown host: [%s]\n",host); Err:; close (conn); return -1; } } } sin.sin_port = htons ( p = atoi(port) ); /* printf ("Ready to connect: %x:%d socket = %d (%s)\n", sin.sin_addr.s_addr,sin.sin_port,conn,inet_ntoa(sin.sin_addr)); */ if (connect(conn,(struct sockaddr *)&sin,sizeof(sin)) < 0) return -1; return conn; } 

      TIRED FORMAT