LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2010, 09:17 PM   #1
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Rep: Reputation: 15
c++ HTTP GET retrieving phantom data (CPANEL server)


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
 
Old 11-03-2010, 11:15 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

There are lots of "yellow flags" here, including:

1. The redundant "connect()"
<= not really an issue

2. Not checking for error in "send()" or "recv()"
<= a BIG issue

3. Not checking for partial data reads
<= an even BIGGER issue

4. Etc.

Suggestion:
Why not just interact with your MySQL database on the server side?

Perhaps you could even do away with the C++ part altogether, and just do everything in PHP?

Just a thought...

PS:
As far as "Sockets 101", you really can't do any better than "Beej's Guide". Check it out:
http://beej.us/guide/bgnet/
 
Old 11-04-2010, 12:54 AM   #3
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks for the reply,
I know I'm not doing error checks, but if the functions were reporting an error, I probably wouldn't be getting a heap of HTML code as a response telling me about the 404 error, or the index.html file magically appearing out of nowhere ... but I'll take your point and put in error checks and see if that helps.

As for reading the MySQL directly, I'm also trying that in a different code fork and I'm being blocked by the server firewall. I've gone over the firewall and MySQL connection settings and can't find a reason for it yet, research continues ... but I only need one of these methods to work ... either the HTTP download, or the MySQL connection.

As for doing it all in PHP, good idea, except that this particular application is a program which analyses scanned documents, performs image recognition on them, and queries the database for information about what these documents should be called ... the C++ program can take several minutes to process a stream of 500 documents. Slightly longer than the 30 seconds allowed by PHP ... but it's a good suggestion in most cases, but not this one.

Anyway, thanks for your reply. I'll add in those error checks and see if it helps at all.
 
Old 11-04-2010, 01:21 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. Please do scan Beej's Guide

2. More important than error checks, please bear in mind that you might need *multiple* reads to fetch all the data you're expecting. This, too, is discussed in Beej's Guide (or any other worthwhile network programming tutorial).

3. You can still run your C++/image recognition code on the server side. CGI is one good alternative. Invoking your C++ exe from PHP is another. It's definitely worthwhile considering refactoring your application to use a client web browser for the front end, and all your "business logic" (including MySQL access and your specialized C++ code) at the back end.

Last edited by paulsm4; 11-04-2010 at 01:22 AM.
 
Old 11-04-2010, 06:27 AM   #5
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Original Poster
Rep: Reputation: 15
I've started reading that guide, and it contains a lot of useful information. With information from that guide, I've worked out what is going on, but I still don't have a solution ... but at least now I have a better idea of where a solution might be.

Apparently it's accessing another user account on the VPS. This other account is 'tpro' and is associated with the root user. Unfortunately, I haven't found where the root user's WWW files are stored (I've checked the /root and /var/www folders and it doesn't seem to be there). Anyway, if I telnet into the web site on port 80, and type in 'GET / HTTP/1.0[enter][enter]' I get the directory listing of the files I'm seeing in my C++ app. I don't know how Firefox/IE manage to find the contents of the 'tp' account, but I'm sure I'll find it somehow, or maybe if I find the location of the root user's WWW folder, I can put my PHP script in there.

Anyone have any suggestions on these issues?
Thanks.

Btw- regarding running the C++ program on the server ... I'm sure the server doesn't have the high-performance GPUs in it that we're using for the image recognition, so although it's a good suggestion, it's impractical in this scenario.

Last edited by fcdev; 11-04-2010 at 06:31 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cpanel/WHM Died at /usr/local/cpanel/Cpanel/Hulk.pm line 92. liang3391 Linux - Software 1 06-22-2009 02:02 PM
Retrieving data from MySQL clb Programming 5 08-12-2007 12:06 PM
Retrieving encrypted data fof3 Linux - Newbie 2 09-12-2006 08:56 AM
retrieving lost data mohtasham1983 Linux - General 3 08-31-2005 01:04 PM
Retrieving Data Bheki Linux - Newbie 1 05-21-2002 07:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration