close

SimpleSockets http://sockets.carrierlabs.com/

// a example to grab a web page from yahoo and write to output.html
#
include "ActiveSocket.h" int test_socket() { int strip_http_header = 1; #define TEST_PACKET "GET / HTTP/1.0\n\n" HANDLE hFile = CreateFile((LPCWSTR)_T("output.html"), // name of the write GENERIC_WRITE, // open for writing 0, // do not share NULL, // default security CREATE_ALWAYS, // overwrite existing FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr. template CActiveSocket client; client.Initialize(); //client.SetNonblocking(); if (client.Open((uint8 *)"tw.yahoo.com", 80)) { if (client.Send((uint8 *)TEST_PACKET, strlen(TEST_PACKET))) { int numBytes = -1; client.Shutdown(CSimpleSocket::CShutdownMode::Sends); //client.Select(); while ( (numBytes = client.Receive(1024)) > 0 ) { int offset = 0; char *find; if( strip_http_header ) { int linetype = 0; char *data = (char*)client.GetData(); if( ((find = strstr(data, "\r\n\r\n")) != NULL && (linetype = 4) != 0) || ((find = strstr(data, "\n\n")) != NULL && (linetype = 2) != 0) || ((find = strstr(data, "\n\r\n")) != NULL && (linetype = 3) != 0) || ((find = strstr(data, "\r\n\n")) != NULL && (linetype = 3) != 0) ) { strip_http_header = 0; offset = find - data + linetype; } } else offset = 0; if(FALSE == WriteFile(hFile, // open file handle client.GetData() + offset, // start of data to write numBytes, // number of bytes to write (LPDWORD)&numBytes, // number of bytes that were written NULL) // no overlapped structure ) { CloseHandle(hFile); return -1; } } CloseHandle(hFile); return 0; } } CloseHandle(hFile); return -1; }
arrow
arrow
    全站熱搜

    Person 發表在 痞客邦 留言(0) 人氣()