|
楼主 |
发表于 2008-1-17 18:54:51
|
显示全部楼层
-
- void main(int argc, char* argv[])
- {
- WORD version = MAKEWORD(1,1);
- WSADATA wsaData;
- int err = WSAStartup(version,&wsaData);
- if(err != 0)
- return;
- if(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
- {
- WSACleanup();
- return;
- }
- SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
- SOCKADDR_IN sockadrr;
- char *host_server = "www.163.com";
- hostent *pHost = gethostbyname(host_server);
- LPCSTR psz = inet_ntoa (*(in_addr*)pHost->h_addr_list[0]);
- sockadrr.sin_addr.S_un.S_addr = inet_addr(psz);
- sockadrr.sin_family = AF_INET;
- sockadrr.sin_port = htons(80);
- char *Command = "GET /index.html HTTP/1.1 \r\nHost: www.163.com\r\nAccept: image/gif \r\n\r\n";
- int len = sizeof(SOCKADDR);
- connect(sock, (SOCKADDR*)&sockadrr, len);
- send(sock, Command, strlen(Command), 0);
- std::ofstream ous("163com.html", std::ios::binary);
- char buf[4096];
- /*
- std::cout<<strlen(buf)<<std::endl
- 而且这一句老是输出4104。。。
- 为什么啊?
- 用memset 一下就OK!
- */
- memset(buf, 0, 4096);
- while(recv(sock, buf, 4096, 0)>0)
- {
- std::cout << strlen(buf) << std::endl;
- if(strlen(buf) < 4096)
- buf[strlen(buf)] = 0;
- ous << buf;
- memset(buf, 0, 4096);
- }
- ous.close();
- int wsaerr = WSAGetLastError();
- std::cout << wsaerr << std::endl;
- }
复制代码
看一看吧!已经没有问题了!HTTP协议是以TCP协议为基础的,记得TCP,不能用sendto |
|