Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. socket() programming question!

socket() programming question!

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminquestionhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    peter ho
    wrote on last edited by
    #1

    Hope that anyone can help me. I am new to socket() programming. I try to connect to a tcp server. However my program fails when its get to gethostbyaddr() function. It keeps returning "null". My questions are: 1) Is there a difference between AF_INET and PF_INET? 2) The gethostbyaddr() returns "null" because it can't not see the tcp server IP on the network for some reason ?(eventhough I can ping or telnet to tcp server) Many thanks! peter. unsigned int addr; struct hostent *hp; SOCKET conn; conn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); addr=inet_addr("10.23.50.133"); hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);

    J 1 Reply Last reply
    0
    • P peter ho

      Hope that anyone can help me. I am new to socket() programming. I try to connect to a tcp server. However my program fails when its get to gethostbyaddr() function. It keeps returning "null". My questions are: 1) Is there a difference between AF_INET and PF_INET? 2) The gethostbyaddr() returns "null" because it can't not see the tcp server IP on the network for some reason ?(eventhough I can ping or telnet to tcp server) Many thanks! peter. unsigned int addr; struct hostent *hp; SOCKET conn; conn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); addr=inet_addr("10.23.50.133"); hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);

      J Offline
      J Offline
      Joseph Dempsey
      wrote on last edited by
      #2

      well here are a couple of things: 1) Make sure you have initialized the winsock library. if you haven't that function would also return null. It sounds trivial but i have forgotten this many a time just to find out it was some simple mistake such as not loading the library. Load it like this:

      int nRet = 0;
      WSADATA wsd; //information object to hold info from library startup
      memset( &wsd, 0, sizeof( WSADATA ) );
      nRet = WSAStartup( MAKEWORD( 2, 2 ), &wsd ); //load and start the library.

      1. There is a difference between AF_INET and PF_INET even though they are numerically the same. You should not rely on that fact because one day those numbers may change. The PF is a protocol family and the AF is an address family. 3) If all else fails you can have a look at this. I wrote this code awhile ago for my own library and use it frequently so i know it works.

      BOOL ConnectServ( SOCKET* pSock, LPCSTR sServ, LPCSTR sPort, BOOL bOverlapped )
      {
      BOOL bConn = FALSE;
      ADDRINFO* pAI = NULL;
      int nRet = 0; // number known not to be SOCKET_ERROR
      DWORD dwFlags = 0;

      if( ! pSock ) {
      return FALSE;
      }

      if( getaddrinfo( sServ, sPort, NULL, &pAI ) != 0 ) {
      return FALSE;
      }

      if( bOverlapped ) {
      dwFlags = WSA_FLAG_OVERLAPPED;
      }

      *pSock = WSASocket( PF_INET, SOCK_STREAM, 0, NULL, 0, dwFlags );
      if( IsValidSocket( *pSock ) ) {
      nRet = WSAConnect( *pSock, pAI->ai_addr, ( int )pAI->ai_addrlen, NULL, NULL, NULL, NULL );
      if( nRet == SOCKET_ERROR ) {
      CloseConn( *pSock );
      }else{
      bConn = TRUE;
      }
      }

      freeaddrinfo( pAI );

      Good luck. Joseph Dempsey joseph_r_dempsey@yahoo.com "Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning." --anonymous

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups