Thread: need help to modify my code, about using socket in windows to read web page.

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Question need help to modify my code, about using socket in windows to read web page.

    Hi, Im new here, need a help.

    this works:
    Code:
    char host[] = "www.google.com";
    char *request = "GET / HTTP/1.0\r\nHost: www.google.com\r\nConnection: Close\r\n\r\n";
    struct hostent *hp;                                       // will keep the same
    hp = gethostbyname(host);                               // will keep the same
    ret = send(sock, request, strlen(request), 0);          // will keep the same

    both below 2 not working, who can make it work:
    Code:
    
     char host[] = "news.google.com/nwshp?hl=en&tab=wn&authuser=0";
    char *request = "GET / HTTP/1.0\r\nHost: news.google.com/nwshp?hl=en&tab=wn&authuser=0\r\n\r\n";
    

    Code:
    char host[] = "news.google.com";
    char *request = "GET / HTTP/1.0\r\nHost: news.google.com/nwshp?hl=en&tab=wn&authuser=0\r\n\r\n";
    Last edited by Ming Darknss; 12-17-2012 at 05:02 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well welcome, but take into consideration that this forum has among other useful threads, a C and C++ one. Obviously you posted on the wrong one thread

    These lines you say do not seem to have a problem by the way
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Quote Originally Posted by std10093 View Post
    Well welcome, but take into consideration that this forum has among other useful threads, a C and C++ one. Obviously you posted on the wrong one thread

    These lines you say do not seem to have a problem by the way
    the code is from my C++ program, I didnt post in wrong thread. The guy who can help me needs to know C++ and a bit about socket.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well in case you know about C++, how about using the C++ class string, instead of the C style string handling?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Quote Originally Posted by std10093 View Post
    Well in case you know about C++, how about using the C++ class string, instead of the C style string handling?
    yea yea I should post it on C thread, but C++ and C that much difference? If you are not helping, just leave my thread.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is a ton of difference between C and C++, since you want to know.
    C is a low-level language whose primary purpose is mostly to remain mean and lean while providing necessary features for low-level programming (which means it has to be very fast). That also means it lacks lots core features modern programming languages tend to have and its standard library is very slim; it lacks most data structures and algorithms, for example.
    C++ is a high-level language that serves to offer the best of both worlds: the low-level approach of C (very fast speed and don't pay for what you don't use) and high-level language features and a huge standard library to provide flexible, fast and efficient programming for modern computers with no trade-off between features, speed and mean and lean (except that C++ compilers are extremely complicated software and the standard library has complexity and size to match).

    Also, choosing the right forum for questions is not just for organizational purposes: it also serves to attract you the correct information you seek. C gurus who are not good at C++ do not necessarily check the C++ board and vice versa. Furthermore, even if C is a subset of C++, if you post C code in the C++ forum, you will be asked to correct it by transforming it into typical C++ code.
    So yes, the category matters.
    Last edited by Elysia; 12-17-2012 at 07:45 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Quote Originally Posted by Elysia View Post
    There is a ton of difference between C and C++,
    Also....
    my problem is more about socket
    C or C++ really doesn't matter in my case

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Ming Darknss View Post
    my problem is more about socket
    C or C++ really doesn't matter in my case
    then perhaps you should have posted in the network communication forum.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Quote Originally Posted by Ming Darknss View Post
    Hi, Im new here, need a help.

    this works:
    Code:
    char host[] = "www.google.com";
    char *request = "GET / HTTP/1.0\r\nHost: www.google.com\r\nConnection: Close\r\n\r\n";
    struct hostent *hp;                                       // will keep the same
    hp = gethostbyname(host);                               // will keep the same
    ret = send(sock, request, strlen(request), 0);          // will keep the same

    both below 2 not working, who can make it work:
    Code:
    
     char host[] = "news.google.com/nwshp?hl=en&tab=wn&authuser=0";
    char *request = "GET / HTTP/1.0\r\nHost: news.google.com/nwshp?hl=en&tab=wn&authuser=0\r\n\r\n";
    

    Code:
    char host[] = "news.google.com";
    char *request = "GET / HTTP/1.0\r\nHost: news.google.com/nwshp?hl=en&tab=wn&authuser=0\r\n\r\n";
    Only saying "not working" does not make any sense to us.
    What is the error you are getting with these two.. Whether it is a compilation error or run time error?? What is the error returned? Please specify and elaborate.
    S_ccess is waiting for u. Go Ahead, put u there.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elkvis View Post
    then perhaps you should have posted in the network communication forum.
    Agreed, this is not a C problem, and certainly not a C++ problem.
    The problem exists not for the lack of C knowledge, but for the lack of the knowledge of the HTTP protocol, and possibly even how it specifically is used with the website you are interested in.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Ming Darknss View Post
    both below 2 not working, who can make it work:
    Code:
     char host[] = "news.google.com/nwshp?hl=en&tab=wn&authuser=0";
    char *request = "GET / HTTP/1.0\r\nHost: news.google.com/nwshp?hl=en&tab=wn&authuser=0\r\n\r\n";
    The other comments in the forum mentioned already that this question seems more to do with the specifics of network protocols rather than programming, but look carefully at your request to see if it actually makes sense semantically

    GET / HTTP/1.0
    Host: news.google.com/nwshp?hl?en&tab=wn&authuser=0

    Is that actually a hostname? It looks to me like a hostname followed by a string of other characters. Maybe this is why your first example worked and your second one didn't.

    To the larger issue, there are many details involved in handling these this type of communication reasonably. Why not use a library designed to do it properly. Libcurl is a popular example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help modify code 6 x 24 to 10 x 24 led matrix
    By visualfx in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2012, 02:04 PM
  2. Open socket and read from web page
    By silversurfer515 in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2010, 01:39 PM
  3. can someone help me modify the code of Metapad?
    By Rigel in forum C Programming
    Replies: 2
    Last Post: 10-17-2009, 09:17 AM
  4. modify my code
    By BEN10 in forum C Programming
    Replies: 3
    Last Post: 03-28-2009, 01:48 AM
  5. Grant read/write/modify permissions to everyone
    By Elkvis in forum Windows Programming
    Replies: 3
    Last Post: 12-12-2008, 06:12 PM