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. if problems

if problems

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 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.
  • D Offline
    D Offline
    dellthinker
    wrote on last edited by
    #1

    Hi all. I made some code that i just cant seem to figure out why its not working like intended. For some strange reason it was working the way i had hoped until i did a final test and it just seemed to do what it wanted, this may sound strange believe me because i think its strange myself, seeing how it worked one minute and then another it doesnt. I dont need to post the entire code but just the block thats giving me trouble. if(connect(dataSock, (sockaddr*)&structSock, sizeof(sockaddr)) == -1){ cout << "Connecting to backup server1: " << backupservers[0] << endl; ServerAddr1 = gethostbyname(backupservers[0]); structSock1.sin_family = AF_INET; structSock1.sin_port = htons(port); structSock1.sin_addr.s_addr = *((unsigned long*)ServerAddr1->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); connect(dataSock, (sockaddr*)&structSock1, sizeof(sockaddr)); } if(connect(dataSock, (sockaddr*)&structSock1, sizeof(sockaddr)) == -1){ cout << "Connecting to backup server2: " << backupservers[1] << endl; ServerAddr1 = gethostbyname(backupservers[1]); structSock1.sin_family = AF_INET; structSock1.sin_port = htons(port); structSock1.sin_addr.s_addr = *((unsigned long*)ServerAddr1->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); connect(dataSock, (sockaddr*)&structSock2, sizeof(sockaddr)); } The idea here is that if the first connection doesnt work try another, and if that doesnt work try another. The first if function works fine. But the second executes the same time the first if block, which isnt what i want. I want it to wait until the if standards have been met, ie: no connection (-1). But its executing when its not wanted. This is really strange considering i took a short break to come back for one last test just to find out that it wasnt working. Any suggestions? Thanx in advance!

    M D 2 Replies Last reply
    0
    • D dellthinker

      Hi all. I made some code that i just cant seem to figure out why its not working like intended. For some strange reason it was working the way i had hoped until i did a final test and it just seemed to do what it wanted, this may sound strange believe me because i think its strange myself, seeing how it worked one minute and then another it doesnt. I dont need to post the entire code but just the block thats giving me trouble. if(connect(dataSock, (sockaddr*)&structSock, sizeof(sockaddr)) == -1){ cout << "Connecting to backup server1: " << backupservers[0] << endl; ServerAddr1 = gethostbyname(backupservers[0]); structSock1.sin_family = AF_INET; structSock1.sin_port = htons(port); structSock1.sin_addr.s_addr = *((unsigned long*)ServerAddr1->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); connect(dataSock, (sockaddr*)&structSock1, sizeof(sockaddr)); } if(connect(dataSock, (sockaddr*)&structSock1, sizeof(sockaddr)) == -1){ cout << "Connecting to backup server2: " << backupservers[1] << endl; ServerAddr1 = gethostbyname(backupservers[1]); structSock1.sin_family = AF_INET; structSock1.sin_port = htons(port); structSock1.sin_addr.s_addr = *((unsigned long*)ServerAddr1->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); connect(dataSock, (sockaddr*)&structSock2, sizeof(sockaddr)); } The idea here is that if the first connection doesnt work try another, and if that doesnt work try another. The first if function works fine. But the second executes the same time the first if block, which isnt what i want. I want it to wait until the if standards have been met, ie: no connection (-1). But its executing when its not wanted. This is really strange considering i took a short break to come back for one last test just to find out that it wasnt working. Any suggestions? Thanx in advance!

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

      Assuming all your addresses are correct... What happens if the second connect() call (of 4 you show) succeeds?  Then you try to connect again(?). Take a look at your logic - why check for error on connect in some calls and not on others? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      D 1 Reply Last reply
      0
      • M Mark Salsbery

        Assuming all your addresses are correct... What happens if the second connect() call (of 4 you show) succeeds?  Then you try to connect again(?). Take a look at your logic - why check for error on connect in some calls and not on others? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        D Offline
        D Offline
        dellthinker
        wrote on last edited by
        #3

        Ok so picture a program trying to connect to a Domain 1: DNS 2: Connect But somethings wrong with the site/domain/server. All it can do is DNS the domain and retrieve a IP address but cannot connect to it. My code does just that. DNS, Connect. But if it cant connect the first server, switch to backup server1, if backupserver1 DNS's properly, but cannot connect, then switch to backup server2. Does this make sense?

        M 1 Reply Last reply
        0
        • D dellthinker

          Ok so picture a program trying to connect to a Domain 1: DNS 2: Connect But somethings wrong with the site/domain/server. All it can do is DNS the domain and retrieve a IP address but cannot connect to it. My code does just that. DNS, Connect. But if it cant connect the first server, switch to backup server1, if backupserver1 DNS's properly, but cannot connect, then switch to backup server2. Does this make sense?

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

          Makes sense but the code shown has logic like this:

          connect
          If failed
             connect
          connect     <-- shouldn't connect here if previous connect succeeded
          If failed
             connect

          when it should be

          connect
          if failed
             connect
             if failed
                connect
                if failed
                   connect

          That's what I was referring to :) Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          D 1 Reply Last reply
          0
          • M Mark Salsbery

            Makes sense but the code shown has logic like this:

            connect
            If failed
               connect
            connect     <-- shouldn't connect here if previous connect succeeded
            If failed
               connect

            when it should be

            connect
            if failed
               connect
               if failed
                  connect
                  if failed
                     connect

            That's what I was referring to :) Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            D Offline
            D Offline
            dellthinker
            wrote on last edited by
            #5

            Yes! Thats exactly what i was refering to. And thats how it was. I maybe typed an extra character when i shouldnt have. Any suggestions?

            M 1 Reply Last reply
            0
            • D dellthinker

              Yes! Thats exactly what i was refering to. And thats how it was. I maybe typed an extra character when i shouldnt have. Any suggestions?

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

              The key point I was trying to make is that you need to check for success/fail on all connect() calls. You can code it any way you want.  The simplest, least elegant way is nested "if"s if (SOCKET_ERROR == connect(..)) {    ... try next address    if (SOCKET_ERROR == connect(..))    {       ... try next address       if (SOCKET_ERROR == connect(..))       {          ... try next address          if (SOCKET_ERROR == connect(..))          {             // all four connect() attempts failed!          }       }    } } You can get as fancy as you want.  For example, a more elegant solution would be a configurable list of addresses/server names that you loop through until you get a successful connection. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              1 Reply Last reply
              0
              • D dellthinker

                Hi all. I made some code that i just cant seem to figure out why its not working like intended. For some strange reason it was working the way i had hoped until i did a final test and it just seemed to do what it wanted, this may sound strange believe me because i think its strange myself, seeing how it worked one minute and then another it doesnt. I dont need to post the entire code but just the block thats giving me trouble. if(connect(dataSock, (sockaddr*)&structSock, sizeof(sockaddr)) == -1){ cout << "Connecting to backup server1: " << backupservers[0] << endl; ServerAddr1 = gethostbyname(backupservers[0]); structSock1.sin_family = AF_INET; structSock1.sin_port = htons(port); structSock1.sin_addr.s_addr = *((unsigned long*)ServerAddr1->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); connect(dataSock, (sockaddr*)&structSock1, sizeof(sockaddr)); } if(connect(dataSock, (sockaddr*)&structSock1, sizeof(sockaddr)) == -1){ cout << "Connecting to backup server2: " << backupservers[1] << endl; ServerAddr1 = gethostbyname(backupservers[1]); structSock1.sin_family = AF_INET; structSock1.sin_port = htons(port); structSock1.sin_addr.s_addr = *((unsigned long*)ServerAddr1->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); connect(dataSock, (sockaddr*)&structSock2, sizeof(sockaddr)); } The idea here is that if the first connection doesnt work try another, and if that doesnt work try another. The first if function works fine. But the second executes the same time the first if block, which isnt what i want. I want it to wait until the if standards have been met, ie: no connection (-1). But its executing when its not wanted. This is really strange considering i took a short break to come back for one last test just to find out that it wasnt working. Any suggestions? Thanx in advance!

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Do you have a blocking or non-blocking socket?


                "A good athlete is the result of a good and worthy opponent." - David Crow

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                D 1 Reply Last reply
                0
                • D David Crow

                  Do you have a blocking or non-blocking socket?


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  D Offline
                  D Offline
                  dellthinker
                  wrote on last edited by
                  #8

                  No Mark Salsbery nested if's wont work either. They all seem to execute as long as connect() is in there. DavidCrow, no i dont believe i have a blocking socket. This is really getting under my skin because i've been working on it too long. Any more ideas? If i put the if in a for loop then it would loop, maybe once, but i dont think it would still accomplish the goal. I'll test it to see.

                  D 1 Reply Last reply
                  0
                  • D dellthinker

                    No Mark Salsbery nested if's wont work either. They all seem to execute as long as connect() is in there. DavidCrow, no i dont believe i have a blocking socket. This is really getting under my skin because i've been working on it too long. Any more ideas? If i put the if in a for loop then it would loop, maybe once, but i dont think it would still accomplish the goal. I'll test it to see.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    dellthinker wrote:

                    DavidCrow, no i dont believe i have a blocking socket.

                    So did you read the documentation on how connect() behaves with non-blocking sockets?


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    D 1 Reply Last reply
                    0
                    • D David Crow

                      dellthinker wrote:

                      DavidCrow, no i dont believe i have a blocking socket.

                      So did you read the documentation on how connect() behaves with non-blocking sockets?


                      "A good athlete is the result of a good and worthy opponent." - David Crow

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      D Offline
                      D Offline
                      dellthinker
                      wrote on last edited by
                      #10

                      I take that back, the nested if's worked. Thanks. Sorry for the mistake :)

                      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