void geturl(char *url)
{
        WSADATA WSAData={0};
        SOCKET        sockfd;
        struct sockaddr_in        addr;
        struct hostent        *pURL;
        char        myurl[BUFSIZ];
        char        *pHost = 0, *pGET = 0;
        char        host[BUFSIZ], GET[BUFSIZ];
        char        header[BUFSIZ] = "";
        static char        text[BUFSIZ];
   //     int i;
        
        /*
         *        windows下使用socket必须用WSAStartup初始化,否则不能调用
         */
        if(WSAStartup(MAKEWORD(2,2), &WSAData))
        {
                printf("WSA failed\n");
                return;
        }
        
        /*
         *        分离url中的主机地址和相对路径
         */
        strcpy(myurl, url);
        for (pHost = myurl; *pHost != '/' && *pHost != '\0'; ++pHost);
        {
    int p1=(int)(pHost - myurl);
    int p2= (int)strlen(myurl);
    if (p1==p2)
                strcpy(GET, "/");
    else
                strcpy(GET, pHost);
   }
        *pHost = '\0';
        strcpy(host, myurl);
        printf("%s\n%s\n", host, GET);        /*
         *        设定socket参数,并未真正初始化
         */
        sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        pURL = gethostbyname(host);
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = *((unsigned long*)pURL->h_addr);
        addr.sin_port = htons(80);        /*
         *        组织发送到web服务器的信息
         *        为何要发送下面的信息请参考HTTP协议的约定
         */
        strcat(header, "GET ");
        strcat(header, GET);
        strcat(header, " HTTP/1.1\r\n");
        strcat(header, "HOST: ");
        strcat(header, host);
        strcat(header, "\r\nConnection: Close\r\n\r\n");
        
        /*
         *        连接到服务器,发送请求header,并接受反馈(即网页源代码)
         */
          connect(sockfd,(SOCKADDR *)&addr,sizeof(addr));
         send(sockfd, header, strlen(header), 0);
    Sleep(2);
         while ( recv(sockfd, text, BUFSIZ, 0) > 0)
         {        
                printf("%s", text);
                strnset(text, '\0', BUFSIZ);
         }
        closesocket(sockfd);        
        WSACleanup();
}
geturl("10.61.2.222/software/index.asp"); 
 
 你这段代码并没有读出内容 ,没有打印出来啊

解决方案 »

  1.   

    我这边可以打印出10.61.2.222/software/index.asp的源代码的
      

  2.   

    76.73.90.2
    /ip.htm
    HTTP/1.1 503 Service Unavailable
    Content-Type: text/html
    Date: Sun, 24 May 2009 16:03:10 GMT
    Connection: close
    Content-Length: 28<h1>Service Unavailable</h1>Press any key to continue
    我这边是这样的  ,没有  源码
    加我QQ494000816  讨论
      

  3.   

    我一般不用socket,都用一些现成的类,你不妨用WinInet
    http://download.csdn.net/source/992005 
    WinInet下载东西非常方便
      

  4.   


    #include <stdio.h>
    #include <windows.h>
    #include <wininet.h>
    #pragma comment(lib,"Wininet.lib")
    #include <vector>
    using namespace std; 
    int main(int argc, char* argv[])
    {
        vector<char> v;
        CHAR szUrl[] = "http://10.55.2.1/ip.htm";
        CHAR szAgent[] = "";
        HINTERNET hInternet1 = 
            InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL);
        if (NULL == hInternet1)
         {
            InternetCloseHandle(hInternet1);
            return FALSE;
         }
        HINTERNET hInternet2 = 
            InternetOpenUrl(hInternet1,szUrl,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE,NULL);
        if (NULL == hInternet2)
         {
            InternetCloseHandle(hInternet2);
            InternetCloseHandle(hInternet1);
            return FALSE;
         }
        DWORD dwMaxDataLength = 500;
        PBYTE pBuf = (PBYTE)malloc(dwMaxDataLength*sizeof(TCHAR));
        if (NULL == pBuf)
         {
            InternetCloseHandle(hInternet2);
            InternetCloseHandle(hInternet1);
            return FALSE;
         }
        DWORD dwReadDataLength = NULL;
        BOOL bRet = TRUE;
        do 
        {
            ZeroMemory(pBuf,dwMaxDataLength*sizeof(TCHAR));
            bRet = InternetReadFile(hInternet2,pBuf,dwMaxDataLength,&dwReadDataLength);
            for (DWORD dw = 0;dw < dwReadDataLength;dw++)
             {
                v.push_back(pBuf[dw]);
             }
         } while (NULL != dwReadDataLength);
        vector<char>::iterator i;
        for(i=v.begin(); i!=v.end(); i++)
            printf("%c",*i);
        return 0;
    }试试这个看看好不好用