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. gethostname() function problems

gethostname() function problems

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharphelp
5 Posts 5 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.
  • K Offline
    K Offline
    koumodaki
    wrote on last edited by
    #1

    I am having trouble with gethostname(). The code compiles correctly but the function returns an unknown error. I have compiled on VC6 and .Net2005 platforms. While in VC6, the buffer remains empty, in .Net2005, it gives me unknown error. Why does this occur? char myname[256]; memset((void*)myname,0,sizeof(myname); if(!gethostname(myname, sizeof(myname))) { int _temp = WSAGetLastError(); if(_temp == WSAEFAULT) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEFAULT ","Lamia",MB_OK);/* who are we? */ if(_temp == WSANOTINITIALISED) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSANOTINITIALISED ","Lamia",MB_OK); if(_temp == WSAENETDOWN) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAENETDOWN ","Lamia",MB_OK); if(_temp == WSAEINPROGRESS) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEINPROGRESS ","Lamia",MB_OK); else MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; error unknown","Lamia",MB_OK); }

    S J S H 4 Replies Last reply
    0
    • K koumodaki

      I am having trouble with gethostname(). The code compiles correctly but the function returns an unknown error. I have compiled on VC6 and .Net2005 platforms. While in VC6, the buffer remains empty, in .Net2005, it gives me unknown error. Why does this occur? char myname[256]; memset((void*)myname,0,sizeof(myname); if(!gethostname(myname, sizeof(myname))) { int _temp = WSAGetLastError(); if(_temp == WSAEFAULT) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEFAULT ","Lamia",MB_OK);/* who are we? */ if(_temp == WSANOTINITIALISED) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSANOTINITIALISED ","Lamia",MB_OK); if(_temp == WSAENETDOWN) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAENETDOWN ","Lamia",MB_OK); if(_temp == WSAEINPROGRESS) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEINPROGRESS ","Lamia",MB_OK); else MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; error unknown","Lamia",MB_OK); }

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      That is because there is no error!! You should look carefully at the value retuned by gethostname. From MSDN - "If no error occurs, gethostname returns zero. Otherwise, it returns SOCKET_ERROR and a specific error code can be retrieved by calling WSAGetLastError". So you should not use if(!gethostname()) it will go into if when function is successful. -Saurabh

      1 Reply Last reply
      0
      • K koumodaki

        I am having trouble with gethostname(). The code compiles correctly but the function returns an unknown error. I have compiled on VC6 and .Net2005 platforms. While in VC6, the buffer remains empty, in .Net2005, it gives me unknown error. Why does this occur? char myname[256]; memset((void*)myname,0,sizeof(myname); if(!gethostname(myname, sizeof(myname))) { int _temp = WSAGetLastError(); if(_temp == WSAEFAULT) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEFAULT ","Lamia",MB_OK);/* who are we? */ if(_temp == WSANOTINITIALISED) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSANOTINITIALISED ","Lamia",MB_OK); if(_temp == WSAENETDOWN) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAENETDOWN ","Lamia",MB_OK); if(_temp == WSAEINPROGRESS) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEINPROGRESS ","Lamia",MB_OK); else MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; error unknown","Lamia",MB_OK); }

        J Offline
        J Offline
        Jijo Raj
        wrote on last edited by
        #3

        Did you called WSAStartup() ? See the code block.

        WORD wVersionRequested;
        wVersionRequested = MAKEWORD( 2, 2 );

        WSADATA wsaData;
        int err = WSAStartup( wVersionRequested, &wsaData );

        koumodaki wrote:

        char myname[256]; memset((void*)myname,0,sizeof(myname);

        BTW, one more suggestion, Instead of using memset, you can directly initialize the array. For instance,

        char myname[256] = { 0 };

        Regards, Jijo.

        _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

        1 Reply Last reply
        0
        • K koumodaki

          I am having trouble with gethostname(). The code compiles correctly but the function returns an unknown error. I have compiled on VC6 and .Net2005 platforms. While in VC6, the buffer remains empty, in .Net2005, it gives me unknown error. Why does this occur? char myname[256]; memset((void*)myname,0,sizeof(myname); if(!gethostname(myname, sizeof(myname))) { int _temp = WSAGetLastError(); if(_temp == WSAEFAULT) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEFAULT ","Lamia",MB_OK);/* who are we? */ if(_temp == WSANOTINITIALISED) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSANOTINITIALISED ","Lamia",MB_OK); if(_temp == WSAENETDOWN) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAENETDOWN ","Lamia",MB_OK); if(_temp == WSAEINPROGRESS) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEINPROGRESS ","Lamia",MB_OK); else MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; error unknown","Lamia",MB_OK); }

          S Offline
          S Offline
          sudhir_Kumar
          wrote on last edited by
          #4

          The Problem is only with the intialization of WINSOCK.

          Sudhir Kumar

          1 Reply Last reply
          0
          • K koumodaki

            I am having trouble with gethostname(). The code compiles correctly but the function returns an unknown error. I have compiled on VC6 and .Net2005 platforms. While in VC6, the buffer remains empty, in .Net2005, it gives me unknown error. Why does this occur? char myname[256]; memset((void*)myname,0,sizeof(myname); if(!gethostname(myname, sizeof(myname))) { int _temp = WSAGetLastError(); if(_temp == WSAEFAULT) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEFAULT ","Lamia",MB_OK);/* who are we? */ if(_temp == WSANOTINITIALISED) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSANOTINITIALISED ","Lamia",MB_OK); if(_temp == WSAENETDOWN) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAENETDOWN ","Lamia",MB_OK); if(_temp == WSAEINPROGRESS) MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; WSAEINPROGRESS ","Lamia",MB_OK); else MessageBox(NULL,"Socket creation failed; Attempt to get host name failed; error unknown","Lamia",MB_OK); }

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            See Getting Addresses IP informations[^] if its helpfuls for you.

            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