Thread: socket get web code whos know just get the web code only

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    socket get web code whos know just get the web code only

    Code:
    #include <iostream> 
    #include <stdio.h>
    #include <winsock.h>
    #include <string.h>
    #define URL "localhost/jacker.html"
    #pragma comment(lib, "ws2_32.lib")
    using namespace std;
    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);
            if ( (int)(pHost - myurl) == strlen(myurl) )
                    strcpy(GET, "/");
            else
                    strcpy(GET, pHost);
            *pHost = '\0';
            strcpy(host, myurl);
    		//测试url是否一致
           //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);
           
            while ( recv(sockfd, text, BUFSIZ, 0) > 0)
            {       
                    printf("%s\n", text);
    				//printf("anser\n",text);
                    strnset(text, '\0', BUFSIZ);
            }
    
            closesocket(sockfd);
           
            WSACleanup();
    }
    
    int main()
    {
            char        url[256];
           // printf("http://");
           //scanf("%s", url);
           //geturl(url);
    		geturl(URL);
            return 0;
    		/*FILE* pFile = NULL;
    		pFile = fopen(,"r");
    		_ASSERT(pFile != NULL);
    		char str[] = "X";
    		fread(str,1,sizeof(str),pFile);
    		if(strcmp(str,"Y")==0)
    		{
    			cout<<"Y";
    		}
    		else
    		{
    			cout<<"N";
            }*/
    }
    http://img62.imageshack.us/img62/341/123jeo.jpg

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Post the text of your problem and more people will help you. Using an image to ask your question is a bit odd. I can't understand the title of your thread which I'm sure others share that sentiment which is a good explanation for your zero responses thus far.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I think what the problem is is that he's getting the HTTP headers along with the actual page. He wants to ignore (not print) the headers.

    Here's a hint: there is always a blank line between the end of the headers and the start of the actual content.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    lolz= =how to delete this post.... i post new one = =write the problems

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Unhappy socket get web code whos know just get the web code only

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <winsock.h>
    #include <string.h>
    #define URL "localhost/index.html"
    #pragma comment(lib, "ws2_32.lib")
    using namespace std;
    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);
            if ( (int)(pHost - myurl) == strlen(myurl) )
                    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);
          
            while ( recv(sockfd, text, BUFSIZ, 0) > 0)
            {      
                    printf("%s\n", text);
                    //printf("anser\n",text);
                    strnset(text, '\0', BUFSIZ);
            }
    
            closesocket(sockfd);
          
            WSACleanup();
    }
    
    int main()
    {
        
            char        url[256];
            geturl(URL);
            return 0;
    }
    Why does run this code will show something extra web code my localhost/index.html just got Y ,be i run this code got show HTTP date excess information,i just wan show the Y only,got people know is what problem..
    my English is poor, do not understand please forgive me , I'm trying to learn English ..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > char url[256];
    > geturl(URL);
    Case matters.
    What is URL?

    Also, recv should be used like this
    Code:
    ssize_t n;
    while ( (n=recv(sockfd, text, BUFSIZ-1, 0)) > 0)
            {      
                    text[n] = '\0';
                    printf("%s\n", text);
                    //printf("anser\n",text);
                    // this does nothing useful anymore strnset(text, '\0', BUFSIZ);  
            }
    recv does NOT add a \0 for you, so you need to add one yourself.
    Filling the buffer with \0 beforehand doesn't help if your data contains \0 anyway. Also, if recv filled the buffer completely, any \0 you added would be out of bounds (this is why I used a -1)

    You should have a similar loop on send() to make sure ALL the data was transmitted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    ssize_t n;
    error ?
    i win7 can't user?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    ssize_t is like size_t - except it is a signed number.

    Post your latest recv() code, showing how you check the return result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Humorous Code
    By DavidP in forum General Discussions
    Replies: 16
    Last Post: 08-28-2010, 10:40 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. Socket code problems
    By Phan in forum Networking/Device Communication
    Replies: 10
    Last Post: 09-14-2005, 04:23 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Code for Client/Server Socket
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2002, 09:30 AM