How to use C to connect to a server

This is a discussion on How to use C to connect to a server within the Networking/Device Communication forums, part of the General Programming Boards category; I have a C program and i need to connect to a computer (i have its IP) and read a ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    How to use C to connect to a server

    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?
    Last edited by ltcabral; 03-18-2008 at 09:52 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    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

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    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:

    Code:
    <?
    $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?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    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...

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    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.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    i heard something about get n post methos... could that help me?

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    read about libCurl if you need to connect to that PHP script..

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    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.:
    Code:
    #!/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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    thx for the advices... ill take a look in these suggestions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to connect to a ftp server
    By Coding in forum Networking/Device Communication
    Replies: 6
    Last Post: 07-12-2010, 10:36 AM
  2. Can't get to connect clients to server
    By pyngz in forum C# Programming
    Replies: 2
    Last Post: 03-11-2009, 04:46 AM
  3. How to connect to an FTP server and download a file?
    By Comeback Kid in forum C Programming
    Replies: 1
    Last Post: 11-19-2005, 07:44 PM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 02:46 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21