Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 14:50

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ] 
Author Message
 Post subject: Open an url link from C ?
PostPosted: 27 Jun 2009 10:09 

Joined: 25 Jun 2009 22:34
Posts: 11
How can i open a link from Dev-C++ ?
I don't mean to display it in a browser, i just want to open a connection to the page for example: google.


Top
 Profile  
 
 
 Post subject: Re: Open an url link from C ?
PostPosted: 27 Jun 2009 10:47 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
your question is not clear because "opening a link" means exactly the launching of the web browser with that link while making a connection means nothing because HTTP (the url) is a protocol and opening a connection doesn't give a http result.


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 27 Jun 2009 12:13 

Joined: 25 Jun 2009 22:34
Posts: 11
ok..then my question becomes: How can i visit a webpage from C? i don't need to view it's contents .. i have an url like www.example.com/write-info-into-file.php.
When you visit this link from a browser, some information about your computer are automatically written into a file on server. The same thing i want to do it from my program. Meaning, i need a function like connect("htpp://www.example.com/blabla.php") which does the same thing as viewing it in a browser.


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 27 Jun 2009 12:21 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
you need to send a minimal HTTP header like the following through the socket's functions:
Code:
GET /write-info-into-file.php HTTP/1.1
Host: www.example.com
Connection: close



Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 27 Jun 2009 21:12 

Joined: 25 Jun 2009 22:34
Posts: 11
Hey man, first of all, thanks for your quick replies.
I tried as you said, so i've got a program like this:
Code:
#include <winsock2.h>       
#include <stdio.h>         
#include <conio.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")

int main()
{
    WSADATA WSAData;
    WSAStartup(MAKEWORD(1,1), &WSAData);
    SOCKET sock;
    SOCKADDR_IN sin;
    char msg[256];
   
    sin.sin_addr.s_addr = inet_addr("127.0.0.1");
    sin.sin_family      = AF_INET;
    sin.sin_port        = htons(4444);
    sock = socket(AF_INET,SOCK_STREAM,0);
    bind(sock, (SOCKADDR *)&sin, sizeof(sin));
    connect(sock, (SOCKADDR *)&sin, sizeof(sin));
    sprintf(msg, "%s\n%s\n%s\n", "GET / HTTP/1.1", "Host: localhost", "Connection: close");
    send(sock, msg, sizeof(msg), 0);
    getch();
}


Well, i have xampp installed and on localhost i have a small php file which increment a variable (counter) and save it in "counter.txt" each time i visit that page in a browser.
My program above, should visit http://localhost/index.php, so the number of views from "counter.txt" would have to increase by 1. But it doesn't...
So, can you help me, and show me what i am doing wrong in the code above ???


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 27 Jun 2009 22:18 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
the first error is the usage of bind which is wrong because you must perform only a connect() while bind() is used for listening on a specific interface and port.

then it's a good thing to verify the return values of the functions, for example after "sock = socket(AF_INET,SOCK_STREAM,0);" you should check if "sock" is minor than 0 (error) and the same for connect().

then the HTTP protocol uses carriage_return\line_feed which means that you must use \r\n as separator and not \n and moreover an additional \r\n at the end (like an empty line) which tells the server that you have finished to send the HTTP headers.

then use an "int" to get the return value of sprintf() and use it in the subsequent send() and moreover add a recv() and a close():
Code:
len = sprintf(msg, blahblahblah);
send(sock, msg, len, 0);
recv(sock, msg, sizeof(msg), 0);
closesocket(sock);
why you need to use recv()?
it's a characteristic of the sockets which is called lingering or something similar, practically if you use a close/closesocket after a send is highly probable that the other endpoint (for example the server) will receive only a part of what you sent or just nothing so exist 2 solutions to avoid this:
- if the other endpoint must send some data (like in this case) use a recv or an empty recv like "recv(sock, NULL, 0, 0);" so that the socket will be closed when it has transmitted all the data (the confirmation is given by recv which means that the server has received it and is replying)
- the alternative is the one that I prefer and should be used almost ever (with UDP sockets too!) because doesn't need to use recv if you want only to send data and is:
Code:
struct  linger  ling = {1,1};
setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling))
a good example of basic socket programming and usage of the linger structure is http://aluigi.org/poc/zilabzrcsdos.zip (but after all any of my recent proof-of-concepts is a good example of multiplatform socket programming)


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 28 Jun 2009 01:50 

Joined: 25 Jun 2009 22:34
Posts: 11
i've done it just as you told me, but now it seems that connect() function doesn't work..
Code:
int main()
{
    WSADATA WSAData;
    WSAStartup(MAKEWORD(1,1), &WSAData);
    SOCKET sock;
    SOCKADDR_IN sin;
    char msg[256];
    int con, len;
   
    sin.sin_addr.s_addr = inet_addr("127.0.0.1");
    sin.sin_family      = AF_INET;
    sin.sin_port        = htons(4444);
    sock = socket(AF_INET,SOCK_STREAM,0);
    if(sock < 0)
            printf("\nSocket error\n");
    con = connect(sock, (SOCKADDR *)&sin, sizeof(sin));
    if(con < 0)
           printf("\nConnect error\n");
    len = sprintf(msg, "%s\r\n%s\r\n%s\r\n\r\n", "GET / HTTP/1.1", "Host: localhost", "Connection: close");
    send(sock, msg, len, 0);
    recv(sock, msg, sizeof(msg), 0);
    closesocket(sock);
    getch();
}

It shows me "Connect error" in console, so the connect function returns -1.
Do you have any ideea why ?
LE: i've tried some different port numbers, but no effect..


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 28 Jun 2009 04:08 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
but are you running the http server on port 4444 on your local computer?


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 28 Jun 2009 10:03 

Joined: 26 Apr 2008 21:50
Posts: 27
You could use libcurl for quick N dirty tools. Its pretty complete, it follows http redirect, it can save the cookies and more, so you dont have to bother to have to.


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 28 Jun 2009 11:25 

Joined: 25 Jun 2009 22:34
Posts: 11
@ aluigi - I changed "127.0.0.1" with my real IP address and it seems that everything goes right in program, but after i execute it, the number of my page views remains the same.

@ Francis - i downloaded the libcurl and compiled it. I tried to make a small example with header "include <curl/curl.h>", but it gives me some errors like:
[Linker error] undefined reference to `_imp__curl_easy_init'
[Linker error] undefined reference to `_imp__curl_easy_setopt'
..and so on.

I copied the content of lib and include from libcurl into the Dev-C++ instalation, but nothing..
Can you tell me how can i solve this linker errors ??


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 28 Jun 2009 13:51 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
uhmmm in your example you used "GET / HTTP/1.1" which means that the counter is located in index.php, right?
or is it an external counter which works on another link? (like the usual counters which are loaded by the browser when loads the externals links)

while for libcurl it "seems" only the lack of linking the library for example adding -lcurl or c:\mingw\lib\libcurl.a in your compiling command


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 28 Jun 2009 22:39 

Joined: 25 Jun 2009 22:34
Posts: 11
yes, counter is in index.php.
it seems that recv() gives an error. Error number: 10054. Connection reset by peer.
What should i do to fix it ?
Code:
#include <winsock2.h>       
#include <stdio.h>         
#include <conio.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")

int main()
{
    WSADATA WSAData;
    WSAStartup(MAKEWORD(2,2), &WSAData);
    SOCKET sock;
    SOCKADDR_IN sin;
    char msg[256];
    int con, len, xx;
   
    sin.sin_addr.s_addr = inet_addr("127.0.0.1");
    sin.sin_family      = AF_INET;
    sin.sin_port        = htons(8080);
    sock = socket(AF_INET,SOCK_STREAM,0);
    if(sock < 0)
            printf("\nSocket error\n");
    con = connect(sock, (SOCKADDR *)&sin, sizeof(sin));
    if(con < 0)
           printf("\nConnect error\n");
    len = sprintf(msg, "%s\r\n%s\r\n%s\r\n\r\n", "GET / HTTP/1.1", "Host: www.google.com", "Connection: close");
    send(sock, msg, len, 0);
    recv(sock, msg, sizeof(msg), 0);
    printf("\nRecv error number: %d\n",WSAGetLastError());
    closesocket(sock);
    getch();
}


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 28 Jun 2009 22:51 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
is the "Host" field correct for your website? because it must be the same of your website so the virtual hosting redirects the request on your page (so if you used "localhost" it didn't work)
because for the rest it looks correct


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 28 Jun 2009 23:06 

Joined: 25 Jun 2009 22:34
Posts: 11
yes, its correct ..


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 29 Jun 2009 12:28 

Joined: 24 Sep 2007 02:12
Posts: 1114
Location: http://sethioz.co.uk
Quote:
i need a function like connect("htpp://www.example.com/blabla.php") which does the same thing as viewing it in a browser.


eem .. is this your only goal ? trying to increase counter ? if so, then there's already tons of programs which can massively visit a page (with proxy support)
if yes, then why the heck you messing with C ? use a packet generator and send a simple packet with header. Like Luigi already showed :

Code:
GET /write-info-into-file.php HTTP/1.1
Host: www.example.com
Connection: close


this is the packet you need to generate and then send it, no need for C program. CommView can create packets out of scratch. ofcourse you need to input all the necessary things in commview, because commview does not have "connect" function, it can build a packet from scratch, so you need to specify source IP, source port, dest IP, dest port..etc, but it all takes min or two if you know that info.

or you can just capture the first connect packet and save it :)


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 29 Jun 2009 18:41 

Joined: 16 Aug 2007 06:25
Posts: 367
edit: I was playing with your code and the following works for me:
Code:
#include <stdio.h>
#include <windows.h>
#include <winsock2.h>

int main()
{
    WSADATA WSAData;
    WSAStartup(MAKEWORD(1,1), &WSAData);
    SOCKET sock;
    SOCKADDR_IN sin;
    char msg[256];
    int con, len;
   
    sin.sin_addr.s_addr = inet_addr("127.0.0.1");
    sin.sin_family      = AF_INET;
    sin.sin_port        = htons(80);
    sock = socket(AF_INET,SOCK_STREAM,0);
    if(sock < 0)
            printf("\nSocket error\n");
    con = connect(sock, (SOCKADDR *)&sin, sizeof(sin));
    if(con < 0)
           printf("\nConnect error\n");
    len = sprintf(msg, "%s\r\n%s\r\n%s\r\n\r\n", "GET / HTTP/1.1", "Host: 127.0.0.1", "Connection: close");
    send(sock, msg, len, 0);
    closesocket(sock);
}


Just make sure your web server is running on the port that you assigned in your code (usually it's port 80 unless you manually change it). You might be getting the reset because there is no web server running on 4444.


Top
 Profile  
 
 Post subject: Re: Open an url link from C ?
PostPosted: 16 Sep 2010 07:20 

Joined: 16 Sep 2010 07:09
Posts: 2
This will also open the URL in the default browser. Some people might get annoyed when you pop up an URL in IE rather than Firefox or Opera when they have one of those browsers set as default.
There was also another thread with ShellExecute back some time on how to open a web page I think. You can check that too.

___________________________________________________________


*edit* no spam


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: