View Full Version : How to use C to connect to a server
ltcabral
03-18-2008, 09:43 AM
I have a C program and i need to connect to a computer (i have its IP) and read a php file... any suggestions on how can i do that?
matsp
03-18-2008, 09:47 AM
Can you describe in a "bigger picture" what you want to do? There are probably several things that you could do, but it would help to understand more of what you are actually trying to do, so we can get the right solution.
--
Mats
Your request is vague and leaves a lot to the imagination.
Your C program (running on your computer, a client) needs to connect to some Server, and then what needs to happen? You want the server to read a file? You want that file downloaded to your client program? You want to pretend that file is local to your machine and read it as a local stream?
Please clarify.
broncoslb
03-18-2008, 09:52 AM
Well the computer you're connecting to is going to have to be running a server of some sort. Also, your going to need the port that the server is listening on. I would read up on socket programming and that should get your going.
-Dustin
www.theCprogrammer.com (http://www.thecprogrammer.com)
ltcabral
03-18-2008, 10:06 AM
let me explain better...
i have a c program and a configuration file with the server IP (and port if needed) and i need my program to connect to that computer, open and read a php file like:
<?
$db_name = "xxxx";
$db_ip = "xxx.xxx.x.xxx";
$db_port = "xxxx";
$db_user = "xxxx";
$db_passwd = "xxxx";
?>
and get the info inside it to use in the C program... in the example i need the xxxx part so if i can read the file i can get these...
what i want to know is a way to connect to the server from the C program and go to file's path to read it... i suppose i can read the file with fopen / fread right?
matsp
03-18-2008, 10:50 AM
To read files from another machine, you need some sort of file transfer service on the remote machine.
If you have free access to the server, then you should set up an SSH server, and use a SSH client (specifically "scp") on the local machine to copy the file across. Then it's a matter of reading/writing the file format. Which is
If you haven't got free access to the server, you are either breaking the forum rules (cracking - not allowed to help you there), or you need to ask the owner of the server what services they supply when it comes to fetching/updating files, e.g. ftp or ssh for example.
--
Mats
ltcabral
03-18-2008, 02:01 PM
i have free access to the server... i must connect to this computer to read the info (usr, passwd, database etc) to use this info in my C program to connect to a mysql server automaticaly, but this info i need to read is in this specific computer and and i gota connect and read it automaticaly too...
brewbuck
03-18-2008, 02:07 PM
There are three parts to this problem.
1. Connecting to the server
2. Preventing the server from interpretting the PHP file
3. Parsing the PHP file.
#1 can be accomplished with wininet or libcurl, depending on your platform. #3 is a dirty parsing problem. And #2 might actually be impossible.
If this is a PHP file running on a web server, there is no way to cause the server to send you the actual text of the PHP file. It's going to interpret it as a PHP script and send you the output.
ltcabral
03-19-2008, 05:31 AM
i heard something about get n post methos... could that help me?
mikewagan
03-19-2008, 06:03 AM
read about libCurl if you need to connect to that PHP script..
matsp
03-19-2008, 06:27 AM
If this is a PHP file running on a web server, there is no way to cause the server to send you the actual text of the PHP file. It's going to interpret it as a PHP script and send you the output.
Assuming you attempt to access it via the web-server, you mean.
I have several times used SCP to copy a PHP file to/from a server. FTP also works, but is less secure.
Writing a small script-file to first copy the PHP file, then process it and copy it back is trivial, e.g.:
#!/bin/sh
scp server:/somedir/dbdefs.php dbdefs.php
processfile dbdefs.php
scp dbdefs.php server:/somedir/dbdefs.php
The above assumes that you have unix-like shell - if you are using Windows-based system, then the script would be slightly different, but only the first line would be noticeably different, really. [You may also use "pscp" instead of "scp", if you use "putty" as your SCP client - but that's minor detail].
If you have free access, you should be able to set things up so that you don't have to type passwords when copying too.
--
Mats
ltcabral
03-19-2008, 10:10 AM
thx for the advices... ill take a look in these suggestions
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.