I have an odd problem, I have spent several months writing a C++ application to interact with a MySQL database. Everything works fine on my Ubuntu development box, but when I try the hosted web server, I run into difficulties.
My first attempt was a remote SQL connection, but the web-host's firewall won't let me in.
Next I tried having my C++ code download a PHP file which will generate the information I seek, but I'm getting data which appears to be from another web site.
For a quick test, I have set up a page called 'echo.php', you can access it with this URL ...
http://www.trainingprofessionals.com....php?msg=Hello
You get a simple response saying Hello, followed by usage information.
Now, my C++ code to download that file (Keep in mind that this code works when I set the URL to my Ubuntu development box ...
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{ int sockfd, c;
struct sockaddr_in addr;
// struct hostent *host = gethostbyname("192.168.1.4");
struct hostent *host = gethostbyname("www.trainingprofessionals.com.au");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr = *((struct in_addr *)host->h_addr);
memset(addr.sin_zero, '\0', sizeof addr.sin_zero);
c = connect(sockfd, (struct sockaddr *)&addr, sizeof addr);
while(c == -1)
{ sleep(5);
c = connect(sockfd, (struct sockaddr *)&addr, sizeof addr);
}
const char *packet = "GET /echo.php?msg=Hello HTTP/1.0\r\n\r\n";
send(sockfd, packet, strlen(packet), 0);
char buffer[4096];
int r = recv(sockfd, buffer, 4096, 0);
char *text = strstr(buffer, "\r\n\r\n");
text +=4;
r -= text-buffer;
text[r]=0;
printf("%s\n",text);
return 0;
}
I get a 404 (file not found) error.
Interestingly, if the file I download is 'index.html' I receive text that looks like a stand-in page for when nothing has been uploaded to the web host. My web site doesn't even have an 'index.html' file, it has 'index.htm'.
The server is running CPANEL on a VPS. The account name with this site is called 'tp'.
If anybody has any ideas on how to fix this, or work around it, I'd be really grateful.
Regards,
Stephen Fraser