Thursday 15 March 2012

How to use winsock in C++ to pass $_get superglobal to a .php on localhost -



How to use winsock in C++ to pass $_get superglobal to a .php on localhost -

i trying utilize c++ script pass informations script local server (localhost) using socket connection.

as interact db , more comfortable php, pass info through $_get superglobal sending finish url correctly , straight formatted $_get[''] variables (not much hack issues on stand lone machine).

i read many posts using winsock2 cpp, , step step implemented examples in cpp programme experiencing 2 troubles:

1) set in host construction localhost through gethostbyname() function. sure ip adress right one, printf of converted-into-char host->h_addr sent me not looking ip_adress. have improve way see host got function? safe utilize gethostbyname() on localhost?

2) know how can correctly write info sent server perform action on database opening of dedicated php web page. interested right result, me much more interested finish , right reference (website nice, or book) know right syntax message send server on socket.

hereafter code:

socket socket = socket(af_inet, sock_stream, ipproto_tcp); struct hostent *host; host = gethostbyname("localhost"); sockaddr_in sockaddr; sockaddr.sin_port = htons(80); sockaddr.sin_family = af_inet; sockaddr.sin_addr.s_addr = *((unsigned long*)host->h_addr); printf("connecting...\n"); if (connect(socket, (sockaddr*)(&sockaddr), sizeof(sockaddr)) != 0) { printf("could not connect"); system("pause"); homecoming 1; } else { printf("connected!\n\n"); } char url[100] = "get / http/1.1\r\nhost:localhost/coffeetime/remove.php?scard_id="; strcat(url, scardid); strcat(url, "&remove=1\r\nconnection: close\r\n\r\n"); int url_len = strlen(url); printf((char*)url); printf("\n"); send(socket, url, url_len, 0); closesocket(socket); wsacleanup();

as see, nil new under sun, took of code on stackoverflow.

thank you!

facing huge number of answers :) , seek give best reply succeeded reach server through connection.

the solution in string sent server : utilize xampp server see through netstat connection ok (gethostbyname works "localhost" in arg no need utilize inet_addr static localhost ip people noob am.

looking @ xampp apache server log , after opening adressed php page i've seen @ end of file string , no need give /localhost/ before finish link. moreover, string ends http 1.1 instead of starting it.

finally, right syntax

char url[100] = "get /coffeetime/remove.php?scard_id="; strcat(url, scardid); strcat(url, "&remove=1\r\n74ad45c8&remove=1 http/1.1 \r\n connection: close\r\n\r\n");

so no big deal, no huge proclamation can interesting people utilize xampp see more useful stuff going xampp command panel.

thanks guy downgrade me no comment way :o) .

php c++ sockets

No comments:

Post a Comment