how to connect a c program with a website?

This is a discussion on how to connect a c program with a website? within the C Programming forums, part of the General Programming Boards category; i am trying to make a program (a gaming zone) which requires subscription on a website which is my website. ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    how to connect a c program with a website?

    i am trying to make a program (a gaming zone) which requires subscription on a website which is my website.


    so just dont know what function is used to order browser to link to the website?

    can anybody tell me with the proper syntax (i mean how to use this function )

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your question isn't clear. do you mean you want a link on your website to invoke a C program on the server side that would do whatever?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I think you're talking about making a HTTP request to a website? If so, all you have to do is connect a TCP socket to the site's address (find it with getaddrinfo) and port 80, then send the request. The format for POST requests is easy to make and send, but if you need more complex functions (e.g., cookies), use libCURL.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545
    Quote Originally Posted by danishzaidi View Post
    order browser to link to the website?
    my interpretation is that he wants the C program to open a browser to a specified URL. if that is the case, and if you are using windows, you can use ShellExecute/ShellExecuteEx. I don't know how you would do it on mac or unix/linux.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by Elkvis View Post
    my interpretation is that he wants the C program to open a browser to a specified URL. if that is the case, and if you are using windows, you can use ShellExecute/ShellExecuteEx. I don't know how you would do it on mac or unix/linux.
    Do the same thing as what I said above (connecting a TCP socket on port 80 to the website's ip), but send a HTTP GET request. The format for a GET request is easily found online.

    After you've sent it, recv() on the socket until it dumps the page contents in HTML to you. Some sites will actually send multiple packets, and it's your job to join them all for coherent code.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    can i connect it with a website at ucoz.com.only those users who have logged in on that website can play the games in the gaming zone that is present in my computer.??

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by danishzaidi View Post
    can i connect it with a website at ucoz.com.only those users who have logged in on that website can play the games in the gaming zone that is present in my computer.??
    That restriction comes way after the actual socket connection part. Logging into the website is something you're responsible for, after you have the TCP part set up.

    I'll show you some of my old code that does relatively the same as what you're looking for:

    This will actually connect to the website, once you give it an IP (e.g., "60.60.60.60") and a port (use 80 for TCP).

    Code:
    int init_tcp_conn(char *ip, char *port) {
    	
    	int fd; 
    	fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	
    	struct sockaddr_in use;
    	memset(&use,0x00,sizeof(struct sockaddr_in));
    	
    	use.sin_family = AF_INET;
    	use.sin_port = htons(atoi(port));
    	inet_pton(AF_INET, ip, &use.sin_addr);
    	
    	if (connect(fd, (struct sockaddr *)&use, sizeof(struct sockaddr)) < 0)
    		perror("connect");
    	
    	return fd;
    }
    The other function will actually retrieve the dots-and-numbers IP from a website URL (e.g., "www.google.com"). From there, you can pass it to the above function ^

    Code:
    char *get_peer_ip(char *hostname, char *port) {
    	char *tmp = malloc(20);
    	
    	struct hostent *he;
    	struct in_addr **addr_list;
    	
    	if (he = gethostbyname(hostname) < 0) {
    		perror("gethost");
    	}
    	addr_list = (struct in_addr **)he->h_addr_list;
    	
    	inet_ntop(AF_INET, (void *)addr_list[0], tmp, INET_ADDRSTRLEN);
    	
    	return tmp;
    }
    A quick example on how to implement this?

    Code:
    	char *host = "www.google.com";
    	char *port = "80";
    	
    	char *dst = get_peer_ip(host,port);
    	printf("Got peer IP %s\n", dst);
    	int fd = init_tcp_conn(dst, port);
    	printf("Connected.\n");
    From here, you can use the send() and recv() calls to make the needed requests to log into the website.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    Quote Originally Posted by memcpy View Post
    That restriction comes way after the actual socket connection part. Logging into the website is something you're responsible for, after you have the TCP part set up.

    I'll show you some of my old code that does relatively the same as what you're looking for:

    This will actually connect to the website, once you give it an IP (e.g., "60.60.60.60") and a port (use 80 for TCP).

    Code:
    int init_tcp_conn(char *ip, char *port) {
        
        int fd; 
        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        
        struct sockaddr_in use;
        memset(&use,0x00,sizeof(struct sockaddr_in));
        
        use.sin_family = AF_INET;
        use.sin_port = htons(atoi(port));
        inet_pton(AF_INET, ip, &use.sin_addr);
        
        if (connect(fd, (struct sockaddr *)&use, sizeof(struct sockaddr)) < 0)
            perror("connect");
        
        return fd;
    }
    The other function will actually retrieve the dots-and-numbers IP from a website URL (e.g., "www.google.com"). From there, you can pass it to the above function ^

    Code:
    char *get_peer_ip(char *hostname, char *port) {
        char *tmp = malloc(20);
        
        struct hostent *he;
        struct in_addr **addr_list;
        
        if (he = gethostbyname(hostname) < 0) {
            perror("gethost");
        }
        addr_list = (struct in_addr **)he->h_addr_list;
        
        inet_ntop(AF_INET, (void *)addr_list[0], tmp, INET_ADDRSTRLEN);
        
        return tmp;
    }
    A quick example on how to implement this?

    Code:
        char *host = "www.google.com";
        char *port = "80";
        
        char *dst = get_peer_ip(host,port);
        printf("Got peer IP %s\n", dst);
        int fd = init_tcp_conn(dst, port);
        printf("Connected.\n");
    From here, you can use the send() and recv() calls to make the needed requests to log into the website.
    thank u so very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (C++) Winsock connect and listen program. Need Help!
    By azjherben in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-05-2009, 08:44 PM
  2. Replies: 1
    Last Post: 10-13-2004, 01:15 PM
  3. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  4. launch program from website
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 07:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21