#include "StdAfx.h" #include <WINSOCK2.H> #include <stdio.h> #include <Windows.h> #include <string> #include <iostream> //#include <vector> #include <algorithm> #pragma comment(lib,"ws2_32.lib") typedef SOCKET socket_type; using namespace std; class response { public: response() { status_code = -1; } int status_code; }; enum { timed_wait_read = 1, timed_wait_write, timed_wait_read_write, timed_wait_error, timed_wait_read_error, timed_wait_write_error, timed_wait_all }; static int set_non_block(int fd, int enable) { #if defined(_WIN32) ULONG opt = enable ? 1 : 0; return ioctlsocket(fd, FIONBIO, (ULONG *) & opt); #elif defined(linux) int opts = 0; opts = fcntl(fd, F_GETFL); if (opts < 0) { return 1; } if (enable) { opts |= O_NONBLOCK; } else { opts |= ~O_NONBLOCK; } opts = fcntl(fd, F_SETFL, opts); if (opts < 0) { return -1; } return 0; #endif } int wgs_timed_wait(int fd, int wait_mode, int sec, int nsec) { int rv; fd_set fdr; fd_set fdw; fd_set fde; fd_set *pfdr; fd_set *pfdw; fd_set *pfde; struct timeval tv = { sec, nsec }; struct timeval *ptv = (sec == -1) ? NULL : &tv; FD_ZERO(&fdr); FD_ZERO(&fdw); FD_ZERO(&fde); FD_SET((socket_type) fd, &fdr); FD_SET((socket_type) fd, &fdw); FD_SET((socket_type) fd, &fde); pfdr = (wait_mode & timed_wait_read) == timed_wait_read ? &fdr : NULL; pfdw = (wait_mode & timed_wait_write) == timed_wait_write ? &fdw : NULL; pfde = (wait_mode & timed_wait_error) == timed_wait_error ? &fde : NULL; #if defined(_WIN32) rv = select(0, pfdr, pfdw, pfde, ptv); if (rv == 0) { return 0; } if (rv == 1) { return 1; } return -1; #endif // WIN32 #if defined(linux) rv = select(fd + 1, pfdr, pfdw, pfde, ptv); if (rv == 0) { return WGSETIMEOUT; } if (rv == 1) { return 0; } return -1; #endif // LINUX } int parse_http_response(char *host,u_short port,response &resp) { int err; WORD versionRequired; WSADATA wsaData; versionRequired=MAKEWORD(1,1); err=WSAStartup(versionRequired,&wsaData);//???????? if(err != 0) { // printf("???????????!/n"); return -1; } SOCKET clientSocket=socket(AF_INET,SOCK_STREAM,0); SOCKADDR_IN clientsock_in; // set_non_block(clientSocket, 1); struct hostent *remoteHost; remoteHost = gethostbyname(host); struct in_addr addr; addr.s_addr = *(u_long *) remoteHost->h_addr_list[0]; clientsock_in.sin_addr.s_addr=inet_addr(inet_ntoa(addr)); clientsock_in.sin_family=AF_INET; clientsock_in.sin_port=htons(port); connect(clientSocket,(SOCKADDR*)&clientsock_in,sizeof(SOCKADDR));//???? // int rv = wgs_timed_wait(clientSocket, timed_wait_write, 3, 0); // if (1 != rv) // { // // ok // cerr << "connect failed." << endl; // return -1; // } // set_non_block(clientSocket, 0); char receiveBuf[512]={0}; char str[128]={0}; sprintf(str,"HEAD / HTTP/1.1/r/nHost: %s/r/nConnection: Close/r/n/r/n", inet_ntoa(addr)); // cout<<"send:"<<endl<<str<<endl; send(clientSocket,str,strlen(str),0); string buf; int start_pos,end_pos; while(recv(clientSocket, receiveBuf, sizeof(receiveBuf) - 1 , 0) >0) { buf.append(receiveBuf); } // cout<<"recv:"<<buf<<endl; if((start_pos=buf.find("HTTP/")) != string::npos) { start_pos = buf.find(" ",start_pos); end_pos = buf.find(" ",start_pos+1); resp.status_code=atoi(buf.substr(start_pos+1 ,end_pos-start_pos-1).c_str()); } closesocket(clientSocket); WSACleanup(); return 0; } void main() { response r; parse_http_response("133.0.0.1",8111,r); // parse_http_response("192.168.100.188",8012,r); cout<<"code="<<r.status_code<<endl; }