Results 1 to 5 of 5

Thread: Invitation to url via a program in C++

  1. #1
    Aaron Ruell is offline Junior Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default Invitation to url via a program in C++

    Hello,

    I would draw from a program in C + + a url on a remote web server, I thought of using a socket to communicate to the outside.

    I tested a piece of code found on the net to understand how a socket and I admit being a little lost, especially since I just react by calling google and only at the header, brief I need a little help!

    Here is the source

    Code:
    1.	# ifndef _WINSOCKAPI_
    2.	# include <winsock.h>
    3.	# endif
    4.	
    5.	# include <Stdafx.h>
    6.	
    7.	# include <windows.h>
    8.	# include <stdio.h>
    9.	
    10.	# include <initguid.h>
    11.	
    12.	
    13.	# include <errno.h>
    14.	# include <signal.h>
    15.	
    16.	# include <tchar.h>
    17.	
    18.	# pragma comment (lib, "ws2_32.lib)
    19.	
    20.	# define PACKET_SIZE 1024
    21.	
    22.	void bcopy (void * source, void * destination, int size)
    23.	(
    24.	char * src = (char *) source;
    25.	char * dst = (char *) destination;
    26.	
    27.	for (int i = 0; i <size; i + +)
    28.	dst [i] = src [i];
    29.	)
    30.	
    31.	
    32.	
    33.	void bzero (void * destination, int size)
    34.	(
    35.	char * dst = (char *) destination;
    36.	
    37.	for (int i = 0; i <size; i + +)
    38.	dst [i] = 0x00;
    39.	)
    40.	
    41.	
    42.	void init (void);
    43.	void apply ();
    44.	writen int (int fd, char * ptr, int n);
    45.	
    46.	to_server_socket int = -1;
    47.	char server_name [100] / * name of host server * /
    48.	int port;
    49.	char request_host [PACKET_SIZE];
    50.	char request_path [PACKET_SIZE];
    51.	
    52.	void main (int argc, char * argv [])
    53.	(
    54.	
    55.	struct sockaddr_in serverSockAddr / * address of socket * /
    56.	struct hostent * serverHostEnt / * description of the host server * /
    57.	struct in_addr addr;
    58.	Long hostaddr / * server's addr * /
    59.	WSADATA wsaData;
    60.	WORD wVersionRequested;
    61.	
    62.	WSADATA wsa_data;
    63.	int err = WSAStartup (MAKEWORD (2, 2) & wsa_data);
    64.	if (err)
    65.	(
    66.	/ * If (argc == 5) (
    67.	sprintf (server_name, argv [1]);
    68.	sscanf (argv [2], "% d", & port);
    69.	sprintf (request_host, argv [3]);
    70.	sprintf (request_path, argv [4]);
    71.	
    72.	)
    73.	else
    74.	exit (1);
    75.	* /
    76.	sprintf (server_name, "localhost");
    77.	sscanf ( "80", "% d", & port);
    78.	sprintf (request_host, "http://www.google.fr");
    79.	/ / sprintf (request_host, "209.85.227.106");
    80.	sprintf (request_path, "/");
    81.	
    82.	printf ( "CONF% s% d% s% s \ n", server_name, port, request_host, request_path);
    83.	
    84.	wVersionRequested MAKEWORD = (2, 2);
    85.	
    86.	err = WSAStartup (wVersionRequested, & wsaData);
    87.	if (err! = 0) (
    88.	printf ( "WSAStartup ca chie:% X \ n", WSAGetLastError ());
    89.	exit (1);
    90.	)
    91.	
    92.	
    93.	/ * Initializes to zero serverSockAddr * /
    94.	memset (& serverSockAddr, sizeof (serverSockAddr));
    95.	/ * Convert 9.100.1.1 ip address in long integer * /
    96.	hostaddr = inet_addr (server_name);
    97.	if ((long) hostaddr! = (long) -1)
    98.	bcopy (& hostaddr, & serverSockAddr.sin_addr, sizeof (hostaddr));
    99.	else / * if we give a name * /
    100.	(
    101.	serverHostEnt = gethostbyname (server_name);
    102.	if (NULL == serverHostEnt)
    103.	(
    104.	printf ( "ca craps gethost:% X \ n", WSAGetLastError ());
    105.	exit (0);
    106.	)
    107.	printf ( "host_ip:");
    108.	for (int i = 0; serverHostEnt-> h_addr_list [i]; i + +)
    109.	(
    110.	addr.s_addr = * (DWORD *) serverHostEnt-> h_addr_list [i];
    111.	printf ( "% s \ t", inet_ntoa (addr));
    112.	)
    113.	printf ( "\ n");
    114.	
    115.	bcopy (serverHostEnt-> h_addr,
    116.	& serverSockAddr.sin_addr, serverHostEnt-> h_length);
    117.	)
    118.	serverSockAddr.sin_port = htons (port) / * host to network port * /
    119.	serverSockAddr.sin_family = AF_INET; / * *** AF_: INET = Internet * /
    120.	
    121.	serverSockAddr.sin_addr.s_addr = inet_addr ( "209.85.227.106");
    122.	
    123.	/ * Socket creation * /
    124.	if ((to_server_socket = socket (AF_INET, SOCK_STREAM, 0)) <0)
    125.	(
    126.	printf ( "ca craps creation client socket \ n");
    127.	exit (0);
    128.	)
    129.	/ * Connection request * /
    130.	if (connect (to_server_socket, (struct sockaddr *) & serverSockAddr,
    131.	sizeof (serverSockAddr)) <0)
    132.	(
    133.	printf ( "ca craps connection request \ n");
    134.	exit (0);
    135.	)
    136.	
    137.	apply ();
    138.	
    139.	/ * Close connection * /
    140.	shutdown (to_server_socket, 2);
    141.	closesocket (to_server_socket);
    142.	WSACleanup ();
    143.	)
    144.	
    145.	)
    146.	
    147.	readn int (int fd, char * ptr, int n);
    148.	
    149.	void apply (void) (
    150.	char buffer [PACKET_SIZE 1];
    151.	char line [PACKET_SIZE 2];
    152.	int rc;
    153.	int i = 0;
    154.	static int isEntete = 1;
    155.	char * finEntete;
    156.	FILE * bulk * header;
    157.	
    158.	sprintf (buffer, "empty \ n");
    159.	sprintf (line, "GET% s HTTP/1.1 \ r \ n"
    160.	"Host:% s \ r \ n"
    161.	/ / "Accept: image / gif, image / x-xbitmap, image / jpeg"
    162.	/ / "Image / pjpeg, image / png, * / *"
    163.	"\ r \ n \ r \ n"
    164.	, request_path, request_host);
    165.	
    166.	if ((bulk = fopen ( "bulkhttp", "wb")) == NULL) (
    167.	printf ( "Can not open file to write bulk \ n");
    168.	exit (1);
    169.	)
    170.	if ((header = fopen ( "header", "wb")) == NULL) (
    171.	fclose (bulk);
    172.	printf ( "Can not open file to write header \ n");
    173.	exit (1);
    174.	)
    175.	
    176.	send (to_server_socket, line, strlen (line) +1.0);
    177.	
    178.	do (
    179.	rc = readn (to_server_socket, buffer, PACKET_SIZE);
    180.	if (isEntete == 1) (
    181.	finEntete = (char *) strstr (buffer, "\ r \ n \ r \ n");
    182.	if (finEntete! = NULL) (
    183.	* finEntete = 0x00;
    184.	fwrite (buffer, 1, strlen (buffer), header);
    185.	fwrite (finEntete 4.1, rc, strlen (buffer) -4, bulk);
    186.	isEntete = 0;
    187.	)
    188.	else
    189.	fwrite (buffer, 1, rc, bulk);
    190.	fclose (header);
    191.	)
    192.	else
    193.	fwrite (buffer, 1, rc, bulk);
    194.	) While (rc = 0);
    195.	fclose (bulk);
    196.	)
    197.	
    198.	writen int (int fd, char * ptr, int n)
    199.	(
    200.	int nl, nw;
    201.	nl = n;
    202.	while (nl> 0) (
    203.	nw = send (fd, ptr, nl, 0);
    204.	if (nw <= 0)
    205.	return nw; / * error * /
    206.	nl -= nw;
    207.	ptr + = nw;
    208.	)
    209.	return (n-nl);
    210.	)
    211.	
    212.	readn int (int fd, char * ptr, int n) (
    213.	int nl, nr;
    214.	
    215.	nl = n;
    216.	while (nl> 0) (
    217.	nr = recv (fd, ptr, nl, 0);
    218.	if (nr <0)
    219.	return nr; / * error * /
    220.	else
    221.	if (nr == 0)
    222.	break;
    223.	nl -= nr;
    224.	ptr + = nr;225.	)226.	227.	)
    If I do not force the line 121 ip address, the name is not resolved and I get an error.
    But even with this hack, only the header file generated is fed.

    Here are the contents of the file header:

    Code:
    1.	HTTP/1.1 302 Found
    2.	Location: http://www.google.in/
    3.	Cache-Control: private
    4.	Content-Type: text / html; charset = UTF-8
    5.	Set-Cookie: PREF = ID = 4861fc427cd617d0: TM = 1256465687: LM = 1256465687: S = v0S3inq215Ip8WMs; expires = Fri, 25-Oct-2011 10:14:47 GMT; path = /; domain =. google.com
    6.	Set-Cookie: NID = 28 = BZTeQcdHXt9dsERJzLxy0-jz05dZs95-SmLtSLT-TxK1vWzZkTBOUcLSWZbfLW2QC8LKjcCpoiiOPTtU2A8-e8RZEGTr255JHHFFlHz8AmBpiTTY8MDVggeYoVd927R_; expires = Fri, 26-Apr-2010 10:14:47 GMT; path = /; domain =. google.com; HttpOnly
    7.	Date: Sun, 25 Oct 2009 10:14:47 GMT
    8.	Server: gws
    9.	Content-Length: 218
    10.	X-XSS-Protection: 0

  2. #2
    Aaron Ruell is offline Junior Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    We must remove the line 121.

    After executing the page is well known, sometimes the response time may exceed 1 second.

    Then in the header file I have a result like this:

    Code:
    1.	HTTP/1.1 200 OK
    2.	Date: Sun, 25 Oct 2009 12:06:43 GMT
    3.	Server: Apache/2.2.X (OVH)
    4.	X-Powered-By: PHP/5.2.10
    5.	Vary: Accept-Encoding
    6.	Transfer-Encoding: chunked
    7.	Content-Type: text / html

  3. #3
    Christopher Scott is offline Junior Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    The buffer is a string not a byte buffer, there's u 0 terminal copy. Hence the utilsiation of strcpy instead of memcpy.

    And proceed to Boost:: Asio ca you say rather than what you paluche infamous code?

  4. #4
    Michael Buscemi is offline Junior Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    In debug mode, everything is initialized to 0, it can most often find cares memories faster than optimized.

    In the latter mode, however, it is in memory ... almost anything. Because nothing is initialized automatically, so you did not then the contents of your memory is unknown.

    That is why there is a difference in behavior between the two modes of compilation.

  5. #5
    Aaron Ruell is offline Junior Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Thank you for your return.

    Indeed the code is far from fine but my goal was to quickly validate the feasibility. I'll rewrite it now.

    As cons go to Boost:: Asio (which I do not know) is not really considered for this portion of code must be integrated into an existing program with its own libraries.

Similar Threads

  1. Need the Best Program
    By Davisricky in forum Programming
    Replies: 1
    Last Post: 02-23-2010, 01:07 PM
  2. C++ program
    By PerrySullivan in forum Programming
    Replies: 1
    Last Post: 01-27-2010, 02:45 PM
  3. What to Look for in a CRM Program
    By niks54 in forum Software Jargons
    Replies: 0
    Last Post: 12-14-2009, 05:57 PM
  4. Help for a Program
    By Gabriel Carlos in forum Programming
    Replies: 2
    Last Post: 10-22-2009, 11:38 AM
  5. Using several web communities in one program
    By Easton Fletcher in forum General Internet Terms
    Replies: 1
    Last Post: 07-14-2009, 07:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48