if problems
-
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! -
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!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:
-
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:
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?
-
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?
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
connectwhen it should be
connect
if failed
connect
if failed
connect
if failed
connectThat's what I was referring to :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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
connectwhen it should be
connect
if failed
connect
if failed
connect
if failed
connectThat's what I was referring to :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
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?
-
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?
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:
-
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!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
-
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
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.
-
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.
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
-
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
I take that back, the nested if's worked. Thanks. Sorry for the mistake :)