Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

for now I'm using:

int connect(const String& address, int port) {
    struct sockaddr_in servAddr;
    struct hostent* host;            /* Structure containing host information */
    /* open socket */
    if((handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        return ERROR;
    //TODO: gethostbyname is obsolete.
    if((host = (struct hostent*) gethostbyname(address)) == 0)
        return ERROR;
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family      = AF_INET;
    servAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)(host -> h_addr_list[0])));
    servAddr.sin_port        = htons(port);
    if(::connect(handle, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
        return ERROR;
    return OK;
    }

this procedure but everytime I compile it I'm getting:

socket.cpp:(.text+0x374): warning: gethostbyname is obsolescent, use getnameinfo() instead.

getname info is still being confusing stuff for me. here is my try to implement it:

struct sockaddr_in servAddr;
struct hostent *host;        /* Structure containing host information */

/* open socket */
if ((handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    return ERROR;

memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family      = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(address.ptr());
servAddr.sin_port        = htons(port);

char servInfo[NI_MAXSERV];
if ( ( host = (hostent*) getnameinfo(
                 (struct sockaddr *) &servAddr
                 ,sizeof (struct sockaddr)
                 ,address.ptr(), address.size()
                 ,servInfo, NI_MAXSERV
                 ,NI_NUMERICHOST | NI_NUMERICSERV )  ) == 0)
    return ERROR;

if (::connect(handle, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
    return ERROR;

-- yes this doesn't work :(

Maybe I should use getaddrinfo instead?

getaddrinfo(hostname, NULL, &hints, &res) - can I use it alike gethostbyname? but where is host actually here? hints?

Working recode based on answer:

int Socket::connect(const String& address, int port) {
    struct sockaddr_in servAddr;
    /* open socket */
    if((handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        return ERROR;
    //gethostbyname by getaddrinfo replacement
    addrinfo hints = {sizeof(addrinfo)};
    hints.ai_flags = AI_ALL;
    hints.ai_family = PF_INET;
    hints.ai_protocol = 4; //IPPROTO_IPV4
    addrinfo* pResult = NULL;
    int errcode = getaddrinfo(address, NULL, &hints, &pResult);
    if(errcode != 0)
        return ERROR;
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family      = AF_INET;
    servAddr.sin_addr.s_addr = *((uint32_t*) & (((sockaddr_in*)pResult->ai_addr)->sin_addr));
    servAddr.sin_port        = htons(port);
    if(::connect(handle, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
        return ERROR;
    return OK;
    }
share|improve this question
Not a code review question. Code posted her is expected to be working, but needing improvements. – Winston Ewert Oct 24 '12 at 17:15

closed as off topic by Winston Ewert Oct 24 '12 at 17:14

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

up vote 3 down vote accepted

This works for me:

int connect2(const CStringA& address, int port) {
  struct sockaddr_in servAddr;
  /* open socket */
  SOCKET handle;
  if((handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    return ERROR;
  //gethostbyname by getaddrinfo replacement
  ADDRINFO hints;
  ZeroMemory(&hints, sizeof(hints));
  hints.ai_flags = AI_ALL;
  hints.ai_family = PF_INET;
  hints.ai_protocol = IPPROTO_IPV4;
  ADDRINFO* pResult = NULL;
  int errcode = getaddrinfo((LPCSTR)address, NULL, &hints, &pResult);
  if(errcode != 0)
    return ERROR;
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family      = AF_INET;
  servAddr.sin_addr.S_un.S_addr = *((ULONG*)&(((sockaddr_in*)pResult->ai_addr)->sin_addr));
  servAddr.sin_port        = htons(port);
  if(::connect(handle, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
    return ERROR;
  return OK;
}
share|improve this answer
Works now \o/ w/o warnings. added recode to question, thank you. – Heather Oct 24 '12 at 9:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.