| 发表于:2008-02-26 15:25:43 楼主 |
问一下,使用socket中的send和recv函数到底能不能得到网页的html代码? 比如放在recv函数中的参数变量中。 如果能,怎么做? 如下是我的一个代码例子,但得出来的是什么也没有,请各位帮忙看看: // initialize winsock wsadata wsadata; int iresult = wsastartup(makeword(2,2), &wsadata); if (iresult != no_error) { afxmessagebox(l"error at wsastartup()."); } //create a socket for connecting to server socket connectsocket; connectsocket = socket(af_inet,sock_stream,ipproto_tcp); if(connectsocket == invalid_socket) { afxmessagebox(l"invalid socket."); int i = wsagetlasterror(); cstring str; str.format(l"%ld",i); afxmessagebox(str); //this function terminates use of the ws2.dll. wsacleanup(); // //wsaenetdown } // the sockaddr_in structure specifies the address family, // ip address, and port of the server to be connected to. sockaddr_in clientservice; clientservice.sin_family = af_inet; clientservice.sin_addr.s_addr = inet_addr("59.60.28.117");//www.google.com.cn clientservice.sin_port = htons( 80 ); //inaddr_none // connect to server. if(connect(connectsocket,(sockaddr*) &clientservice, sizeof(clientservice)) == socket_error ) { afxmessagebox(l"connect error."); wsacleanup(); } // declare and initialize variables. int bytessent; int bytesrecv = socket_error; char sendbuf[32] = "client: sending data."; char recvbuf[32] = ""; // send and receive data. bytessent = send( connectsocket,sendbuf,strlen(sendbuf),0); if(bytessent == socket_error) { afxmessagebox(l"send error."); wsacleanup(); } cstring sendstr; sendstr.format(l"%d",bytessent); afxmessagebox(l"send bytes:"); afxmessagebox(sendstr); // bytesrecv = recv(connectsocket,recvbuf,32,0 ); cstring recvstr; recvstr.format(l"%s",recvbuf); afxmessagebox(recvstr); //afxmessagebox((lpctstr)recvbuf); if ( bytesrecv == 0 ¦ ¦ bytesrecv == wsaeconnreset ) { afxmessagebox( l"connection closed."); } wsacleanup(); |
|
|
|
|