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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Socket curious error

Socket curious error

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadminhelpquestion
8 Posts 3 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.
  • A Offline
    A Offline
    Alex Cutovoi
    wrote on last edited by
    #1

    Hi I have a server app that I'm developing. When I call the bind function that is in my BindStructOnSocket function, depending how I filled my sockaddr_in structure, occurs an different error. If I fill my sockaddr_in.sin_addr field like this : SocketStruct->sin_addr.s_addr = INADDR_ANY; When I call accept function my app enters in a infinite loop. If I fill my sockaddr_in.sin_addr field like this : char cName[30]; gethostname(cName, 30); hostent * theHost; theHost = gethostbyname(cName); SocketStruct->sin_family = AF_INET; SocketStruct->sin_port = htons(2500); SocketStruct->sin_addr = *((in_addr*)theHost->h_addr_list); When I call accept I get the WSAEADDRNOTAVAIL error. My queswtion is WHY? I'm developing this with Visual C++ 6 Below my code Tks for the support SOCKET CreateSocket() { SOCKET theSocket; WORD wVersion = MAKEWORD(2, 0); WSADATA wsStartup; WSAStartup(wVersion, &wsStartup); theSocket = socket(AF_INET, SOCK_STREAM, 0); if(theSocket == INVALID_SOCKET) { MessageBox(NULL, "Socket não criado", "Erro", MB_ICONERROR|MB_OK); return INVALID_SOCKET; } return theSocket; } void CreateSocketStruct(sockaddr_in * SocketStruct) { //char cName[30]; //gethostname(cName, 30); //hostent * theHost; //theHost = gethostbyname(cName); SocketStruct->sin_family = AF_INET; SocketStruct->sin_port = htons(2500); SocketStruct->sin_addr.s_addr = INADDR_ANY; // *((in_addr*)theHost->h_addr_list); for(int i = 0 ; i < 8 ; i++) SocketStruct->sin_zero[i] = 0; char * cTemp; cTemp = inet_ntoa(SocketStruct->sin_addr); MessageBox(NULL, cTemp, "", MB_OK); } int BindStructOnSocket(SOCKET currSocket, sockaddr_in * SocketInfo) { int iTemp = bind(currSocket, (sockaddr*)SocketInfo, sizeof(sockaddr)); if(iTemp != 0) { int i = WSAGetLastError(); char cTemp[5]; if(i == WSAEADDRNOTAVAIL)MessageBox(NULL, "", "Erro", MB_OK); } return iTemp; }

    M 1 Reply Last reply
    0
    • A Alex Cutovoi

      Hi I have a server app that I'm developing. When I call the bind function that is in my BindStructOnSocket function, depending how I filled my sockaddr_in structure, occurs an different error. If I fill my sockaddr_in.sin_addr field like this : SocketStruct->sin_addr.s_addr = INADDR_ANY; When I call accept function my app enters in a infinite loop. If I fill my sockaddr_in.sin_addr field like this : char cName[30]; gethostname(cName, 30); hostent * theHost; theHost = gethostbyname(cName); SocketStruct->sin_family = AF_INET; SocketStruct->sin_port = htons(2500); SocketStruct->sin_addr = *((in_addr*)theHost->h_addr_list); When I call accept I get the WSAEADDRNOTAVAIL error. My queswtion is WHY? I'm developing this with Visual C++ 6 Below my code Tks for the support SOCKET CreateSocket() { SOCKET theSocket; WORD wVersion = MAKEWORD(2, 0); WSADATA wsStartup; WSAStartup(wVersion, &wsStartup); theSocket = socket(AF_INET, SOCK_STREAM, 0); if(theSocket == INVALID_SOCKET) { MessageBox(NULL, "Socket não criado", "Erro", MB_ICONERROR|MB_OK); return INVALID_SOCKET; } return theSocket; } void CreateSocketStruct(sockaddr_in * SocketStruct) { //char cName[30]; //gethostname(cName, 30); //hostent * theHost; //theHost = gethostbyname(cName); SocketStruct->sin_family = AF_INET; SocketStruct->sin_port = htons(2500); SocketStruct->sin_addr.s_addr = INADDR_ANY; // *((in_addr*)theHost->h_addr_list); for(int i = 0 ; i < 8 ; i++) SocketStruct->sin_zero[i] = 0; char * cTemp; cTemp = inet_ntoa(SocketStruct->sin_addr); MessageBox(NULL, cTemp, "", MB_OK); } int BindStructOnSocket(SOCKET currSocket, sockaddr_in * SocketInfo) { int iTemp = bind(currSocket, (sockaddr*)SocketInfo, sizeof(sockaddr)); if(iTemp != 0) { int i = WSAGetLastError(); char cTemp[5]; if(i == WSAEADDRNOTAVAIL)MessageBox(NULL, "", "Erro", MB_OK); } return iTemp; }

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      The second method won't work if there's no adapter associated with the address you specify. The first method (using INADDR_ANY) should be correct. There's maybe something wrong with your accept processing. Is this the server's listening socket you are using the above code on? Mark

      "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        The second method won't work if there's no adapter associated with the address you specify. The first method (using INADDR_ANY) should be correct. There's maybe something wrong with your accept processing. Is this the server's listening socket you are using the above code on? Mark

        "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

        A Offline
        A Offline
        Alex Cutovoi
        wrote on last edited by
        #3

        Mark Salsbery wrote:

        Is this the server's listening socket you are using the above code on?

        Exactly, I'm testing this code in my house but the problem persists

        M 1 Reply Last reply
        0
        • A Alex Cutovoi

          Mark Salsbery wrote:

          Is this the server's listening socket you are using the above code on?

          Exactly, I'm testing this code in my house but the problem persists

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          So once your listening socket is bound and listening, there's an infinite loop on accept? Do you mean it just sits (blocked) in the accept() call? Assuming your socket(), bind(), and listen() calls all succeed (no errors) then this is the correct behavior for a blocking socket when you call accept(). accept() will block until there's a connect request. Mark

          "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

          A 1 Reply Last reply
          0
          • M Mark Salsbery

            So once your listening socket is bound and listening, there's an infinite loop on accept? Do you mean it just sits (blocked) in the accept() call? Assuming your socket(), bind(), and listen() calls all succeed (no errors) then this is the correct behavior for a blocking socket when you call accept(). accept() will block until there's a connect request. Mark

            "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

            A Offline
            A Offline
            Alex Cutovoi
            wrote on last edited by
            #5

            No, there aren't an infinite loop on accept. And yeah, it just sits in the accept call.

            M 1 Reply Last reply
            0
            • A Alex Cutovoi

              No, there aren't an infinite loop on accept. And yeah, it just sits in the accept call.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Then it is working as written. I'm guessing you need that to be asynchronous instead of halting the thread, correct? If so, you can do one of these: 1) Leave the listener socket in blocking mode and put the accept call in a different thread. 2) Use WSAAsyncSelect() to place the socket in non-blocking mode and get a window message when a connection attempt is received. Then call accept() in response. 3) Use WSAEventSelect() to place the socket in non-blocking mode and have an event signalled when a connection attempt is received. Then call accept() in response (also requires a separate thread to be effective). 4) Use an IO Completion Port (advanced). *edit* Fixed some typnig. Mark -- modified at 17:33 Thursday 1st March, 2007

              "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

              L 1 Reply Last reply
              0
              • M Mark Salsbery

                Then it is working as written. I'm guessing you need that to be asynchronous instead of halting the thread, correct? If so, you can do one of these: 1) Leave the listener socket in blocking mode and put the accept call in a different thread. 2) Use WSAAsyncSelect() to place the socket in non-blocking mode and get a window message when a connection attempt is received. Then call accept() in response. 3) Use WSAEventSelect() to place the socket in non-blocking mode and have an event signalled when a connection attempt is received. Then call accept() in response (also requires a separate thread to be effective). 4) Use an IO Completion Port (advanced). *edit* Fixed some typnig. Mark -- modified at 17:33 Thursday 1st March, 2007

                "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7
                1. Buy your Fish Filet at McDonalds :-D

                led mike

                M 1 Reply Last reply
                0
                • L led mike
                  1. Buy your Fish Filet at McDonalds :-D

                  led mike

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  Indeed! Dammit now I'm hungry.

                  "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

                  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