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")