8

ネットワーク上のデバイスへの接続に問題があります。getaddrinfo() を呼び出すたびに、11001 が返されます。IP_ADDRESS 文字列 (Global Var) 内の多数の異なる IP でこれを確認しました。nslookup ですべての非稼働番号を確認しましたが、ほとんどがそこに存在します。

getaddrinfo-returns-always-11001-host-not-found も同様の質問をしているようですが、そこには答えがありません。

現時点では、私のコードはリモート デバイスに接続しようとしてもおらず、IP を解決しようとしているだけです。それが機能したら、より大きくて厄介な問題に進むことができます。

実装:

int connectToDevice(char *sendbuf, char *recvbuf, SOCKET ConnectSocket)
{
WSADATA wsaData;
    struct addrinfo *result = NULL,
                *ptr = NULL,
                hints;
struct timeval tval;

fd_set rset, wset;


int iResult;
u_long mode = -1;

//Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) 
{
    printf("WSAStartup failed with error: %d\n", iResult);
    return 1;
}

ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

//Resolve the server address and port
iResult = getaddrinfo(IP_ADDRESS, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) 
{
    printf("getaddrinfo failed with error: %d\n", iResult);
    WSACleanup();
    return 1;
}



// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) 
{

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
    if (ConnectSocket == INVALID_SOCKET) 
    {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //set socket to non-blocking
    iResult = ioctlsocket(ConnectSocket, FIONBIO, &mode); //if mode is set to non-zero, socket set to non-blocking.
    if(iResult != NO_ERROR)
    {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }


    // Connect to server.
    iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR  ) //if an error and not WSAEWOULDBLOCK, then close socket and try next address
    {
        if(WSAEWOULDBLOCK != WSAGetLastError())
        {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;                           //this returns control to the For loop. I.e. if a socket error, try next address
        }
        else    //otherwise if the error was WSAEWOULDBLOCK, then use select to check for connections.
        {
            FD_ZERO(&rset); //initialise fd_sets for reading and writing; both the same.
            FD_SET(ConnectSocket, &rset);
            wset = rset;

            //set tval to timeout value
            tval.tv_sec = TIMEOUT;
            tval.tv_usec= 0;

            //select statement
            //select ignores first parameter
            //select takes 3xfd_sets, read set, write set, and exception set.
            //select's last parameter is timeout in the form of a timeval struct
            //if return == 0, timeout occured.
            //if return == SOCKET_ERROR, error occured, use WSAGetLastError to check for details.

            iResult = select(ConnectSocket, &rset, &wset, NULL, &tval);
            if (iResult ==0)
            {
                closesocket(ConnectSocket);
                printf("Timeout reached, closing socket");
                WSACleanup();
                return 1;
            }
            else if(iResult == SOCKET_ERROR)
            {
                printf("socket failed with error: %ld\n", WSAGetLastError());
                WSACleanup();
                return 1;
            }

        }

    }

    break;  //Breaks out of the for loop. Will only occur if continue not executed
}

freeaddrinfo(result);

if (ConnectSocket == INVALID_SOCKET)
{
    printf("Unable to connect to server!\n");
    WSACleanup();
    return 1;
}

return 0;}

このコードのほとんどは、msdn の Web サイトからロックとストックを取得したものですが、すべて問題ないように見えます。

4

4 に答える 4

3

これは、ホストが見つからないというエラー コードです。WinSock2.h を見てください。WSABASEERR+1001またはを検索するWSAHOST_NOT_FOUND

Microsoft は、ここでエラー コードgetaddrinfoが返す内容を説明しています。

于 2011-01-24T04:13:22.563 に答える
1

私もこの問題に遭遇しました...getaddrinfoそしてgethostbyname両方とも11001エラーで失敗していますが、ping/nslookupは同じホスト名に対して機能しています。

以前にシンボルサーバーを使用したことがあり、実行可能ファイルと同じディレクトリにあるすべてのWin32DLLのシンボルをダウンロードしました。すべての.pdbディレクトリを削除すると、問題が修正されました。

私の推測では、シンボルがあり、アプリケーションをデバッグしている場合は失敗しますgethostbynamegetaddrinfo

于 2012-05-07T04:43:01.720 に答える