0
BOOL sendHTTPdata(char* data){
    //MessageBox(NULL,data, "aaa!",MB_ICONEXCLAMATION | MB_OK);

    char  *s=malloc(sizeof(char)*5000);
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result= NULL,
                    *ptr = NULL,
                    hints={0};
    char recvbuf[DEFAULT_BUFLEN];
    int iResult;
    int recvbuflen = DEFAULT_BUFLEN;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        //printf("WSAStartup failed with error: %d\n", iResult);
        MessageBox(NULL,"WSAStartup failed with error: %d\n", "Error!",MB_ICONEXCLAMATION | MB_OK);
        return FALSE;
    }

    memset (&hints, 0, (sizeof (hints)+1000));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    // Resolve the server address and port
    //MessageBox(NULL,"test2", "Error!",MB_ICONEXCLAMATION | MB_OK);

    iResult = getaddrinfo("example.com","80", &hints, &result);
    //return FALSE;
    if ( iResult != 0) {
        sprintf(s, "getaddrinfo failed with error: %d\n",iResult);
        MessageBox(NULL,s, "Error!",MB_ICONEXCLAMATION | MB_OK);
        WSACleanup();
        return FALSE;
    }
    MessageBox(NULL,"test", "Error!",MB_ICONEXCLAMATION | MB_OK);
    // Attempt to connect to an address until one succeeds
    //return FALSE;
    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) {
           sprintf(s, "socket failed with error: %ld\n",WSAGetLastError());
        MessageBox(NULL,s, "Error!",MB_ICONEXCLAMATION | MB_OK);
        WSACleanup();
        return FALSE;
        }

        // Connect to server.
        iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
    }

    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
       MessageBox(NULL,"Unable to connect to server!\n", "Error!",MB_ICONEXCLAMATION | MB_OK);
        WSACleanup();
        return FALSE;

    }

    // Send an initial buffer
    iResult = send( ConnectSocket, data, (int)strlen(data), 0 );
    if (iResult == SOCKET_ERROR) {
        sprintf(s,"send failed with error: %d\n",WSAGetLastError());
        MessageBox(NULL,s, "Error!",MB_ICONEXCLAMATION | MB_OK);
        closesocket(ConnectSocket);
        WSACleanup();
        return FALSE;
    }

    //printf("Bytes Sent: %ld\n", iResult);
    sprintf(s,"Bytes Sent: %ld\n",iResult);
    MessageBox(NULL,s, "Error!",MB_ICONEXCLAMATION | MB_OK);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        //printf("shutdown failed with error: %d\n", WSAGetLastError());
        sprintf(s,"shutdown failed with error: %d\n", WSAGetLastError());
        MessageBox(NULL,s, "Error!",MB_ICONEXCLAMATION | MB_OK);

        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
        {
            MessageBox(NULL,recvbuf, "success!",MB_ICONEXCLAMATION | MB_OK);
        }
        else if ( iResult == 0 )
            MessageBox(NULL,"Connection closed\n", "Error!",MB_ICONEXCLAMATION | MB_OK);
        else {
            sprintf(s,"recv failed with error: %d\n", WSAGetLastError());
            MessageBox(NULL,s, "Error!",MB_ICONEXCLAMATION | MB_OK);
        }
    } while( iResult > 0 );

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return TRUE;
}

Visual C++ 2010 でコンパイルするとアクセス違反エラーが発生しますが、Visual C++ 6.0 でコンパイルすると動作します...

これは私がエラーログで見ることができるものです:

First-chance exception at 0x1027d22f (msvcr100d.dll) in Work Time Managment.exe: 0xC0000005: Access violation writing location 0x00130000.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
First-chance exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
Unhandled exception at 0xfefefefe in Work Time Managment.exe: 0xC0000005: Access violation reading location 0xfefefefe.
The program '[1456] Work Time Managment.exe: Native' has exited with code 0 (0x0).

なにが問題ですか... ?Visual C++ 2010 で getaddrinfo が機能しないのはなぜですか ???? コードで使用するこのライブラリ:

#define WIN32_LEAN_AND_MEAN
#define _WSPIAPI_COUNTOF
#include "windows.h"
#include "winsock2.h"
#include "ws2tcpip.h"
#include "stdio.h"
#pragma comment (lib, "Ws2_32.lib")
4

2 に答える 2

2

これは問題です:

memset (&hints, 0, (sizeof (hints)+1000)); 

1000メモリのバイトに書き込むため、未定義の動作を引き起こします。すでに初期化されているため、への呼び出しmemset()は不要です。hintsただし、正しい使い方は次のとおりです。

memset (&hints, 0, sizeof(hints));

getaddrinfo()問題は、 VC2010 を使用して正常に実行されたコードの別の場所にあると思います。これは私が使用したソースです:

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <string.h>

int main()
{
    WSADATA wsaData;
    struct addrinfo *result= NULL, hints={0}; 
    int iResult;

    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);

    if (iResult != 0)
    {
        fprintf(stderr, "Startup failure: %d\n", iResult);
    }
    else
    {
        /* Identical behaviour with and without the memset(). 
        memset(&hints, 0, sizeof(hints)); */

        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;

        iResult = getaddrinfo("localhost", "80", &hints, &result);
        if (0 != iResult)
        {
            fprintf(stderr, "failure: %d\n", iResult);
        }
        else
        {
            printf("Success\n");
        }

        WSACleanup();
    }
    return 0;
}
于 2012-09-05T13:45:41.820 に答える