Jeg fandt et eksempel på netttet.
Det virker ikke via C++ programmet, men hvis jeg gør det via Internet Explorer virker det fint. Håber i kan se fejlen...
PHP:
- <?php
- $con = mysql_connect("localhost","*******","*******");
-
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
-
- mysql_select_db("trynerne_dk", $con);
- mysql_query("INSERT INTO Point (point) VALUES ('$_GET[point]')");
- echo "Finish";
- mysql_close($con);
- ?>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Test</title>
- </head>
-
- <body>
- </body>
- </html>
C++:
#include <WinSock2.h>
#include <iostream>
#include <stdio.h>
using namespace std;
BOOL InitConnection(SOCKET *wSock, char *SERV);
BOOL InitWSA();
BOOL InitSocket(SOCKET *wSock);
int main()
{
char buf[300];
int point;
SOCKET sock;
cin >> point;
sprintf_s(buf, "GET /test.php?point=%d HTTP/1.0", point);
if (InitWSA())
cout << "WS2_32.DLL loaded\n";
Sleep(20);
if (InitSocket(&sock))
cout << "Socket created\n";
Sleep(20);
if (InitConnection((SOCKET*)sock, "http://www.trynerne.dk"))
cout << "Connected\n";
Sleep(20);
send(sock, buf, sizeof(buf), 0);
cout << "Sending headers\n";
system("PAUSE");
return 0;
}
BOOL InitConnection(SOCKET *wSock, char *SERV)
{
int port = 80;
struct hostent *host;
struct sockaddr_in sin;
int error;
host = gethostbyname(SERV);
memset(&sin, 0, sizeof sin);
sin.sin_family = AF_INET;
sin.sin_addr = *((in_addr*)host->h_addr);
sin.sin_port = htons(port);
error = connect((SOCKET)wSock, (sockaddr*)&sin, sizeof sin);
if (error != 0)
return false;
return true;
}
BOOL InitWSA()
{
WSADATA wsaData;
WORD version;
version = MAKEWORD(2, 2);
int error;
error = WSAStartup(version, &wsaData);
if (error != 0)
return false;
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wHighVersion) != 2)
{
WSACleanup();
return false;
}
return true;
}
BOOL InitSocket(SOCKET *wSock)
{
*wSock = socket(AF_INET, SOCK_STREAM, 0);
if ((SOCKET)wSock == INVALID_SOCKET)
return false;
return true;
}
Indlæg senest redigeret d. 12.02.2009 21:34 af Bruger #14541