Socket curious error
-
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 supportSOCKET 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; }
-
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 supportSOCKET 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; }
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")
-
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")
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
-
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
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")
-
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")
No, there aren't an infinite loop on accept. And yeah, it just sits in the accept call.
-
No, there aren't an infinite loop on accept. And yeah, it just sits in the accept call.
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")
-
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")
-
Indeed! Dammit now I'm hungry.
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")