Communication between PHP script and C++ daemon

This is a discussion on Communication between PHP script and C++ daemon within the Tech Board forums, part of the Community Boards category; So i have a web server running apache, with php enabled, and a game server running a C++ daemon that ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Communication between PHP script and C++ daemon

    So i have a web server running apache, with php enabled, and a game server running a C++ daemon that i botched together myself fairly quickly. Now i need to make a PHP script which sends a command to the daemon on the other server. They're not on the same network by the way, theyre miles apart. How would i go about this? I fear this involves a bunch of socket-programming and a TCP connection. Is there an easy way to do this?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    You're going to have to go into more detail. What type of data do you need to send to the "C++" server? If it is sent in some common protocol, there is most likely some library available to assist you.

    But if the protocol is something of your own, then just try googling "PHP socket tutorial" or something similar and then come back here if you have any problems.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you haven't found this yet:

    PHP: cURL - Manual
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    Low level socket programming is a good thing to be acquainted with, even if you don't absolutely need it in this project. It's really pretty easy. I learned a lot from Beej's guide, it's part of my recommend reading
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  5. #5
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Seems like socket programming on Linux isn't as bad as i thought, i just have a few questions:
    Our webhost only allows outgoing connections through port 80, so the C++ daemon would have to be listen()'ing on port 80, could this be a problem? Isn't port 80 reserved?

    How does the listen() call actually work? Does it just setup the socket to listen on the given port and then returns execution once that is complete, or does it only return once there actually is an incoming connection?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    listen() tells the operating system to accept connections on that socket.

    to accept incoming connections in your application, you need to call accept()

  7. #7
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Right, so i'd call listen() on a socket bound to port 80 when the daemon start, and then in the main-loop i would call accept() once in a while? If the connection queue is empty accept() does nothing i presume?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  8. #8
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    What you should probably do is put accept() into asynchronous mode, or throw it in a new thread. It's a blocking function so that could screw up the rest of your program. So if you "call" it every once in a while, your program will hang on it.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Maybe you can make use of Mongoose?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Neo1 View Post
    Seems like socket programming on Linux isn't as bad as i thought, i just have a few questions:
    Our webhost only allows outgoing connections through port 80, so the C++ daemon would have to be listen()'ing on port 80, could this be a problem? Isn't port 80 reserved?
    Not in a strict way. It is the normative port for http connections, and you will regularly get hits on it from various bots and other creepy crawly things.

    The problem is that because it's the normal http port, that's where web clients connect to*, and where apache listens by default. So if you can only use one port, you won't be able to have more than one server online. Seems like a weird restriction, but ISP's discourage people from running servers unless you pay (more) for doing it.

    * unless you specify something else, eg,
    Code:
    http://wherever.com:8182
    Quote Originally Posted by Neo1 View Post
    in the main-loop i would call accept() once in a while? If the connection queue is empty accept() does nothing i presume?
    Use select() or poll(). All read requests on your server socket (the one you called listen() on) are connect() from a client, accept() returns a new fd for the accepted connection.

    You can add fds to the select() or poll() and set non-block on everything to do stuff asynchronously with queues and closures/callbacks*, but it is much easier to just use threads or forks, as long as you are not expecting too many simultaneous connections.

    * in this case what happens is you cycle around, reading and writing as much as possible at once, which is usually the size of the socket buffer, I think 16 KB for read and 8 KB for write by default.
    Last edited by MK27; 12-27-2011 at 11:17 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Is the C++ daemon running on a Linux system? If so, it will not be able to bind to port 80 unless it is running as root.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Is the C++ daemon running on a Linux system? If so, it will not be able to bind to port 80 unless it is running as root.
    Yes, but you can setuid to a non-privileged user after the bind(), which is a common approach, because having a root privileged server open to the outside is definitely a very dumb idea.

    You can also apparently use setcap() to get around this, altho I have never tried:

    Miscellanous Information
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-16-2011, 05:17 AM
  2. Replies: 0
    Last Post: 02-05-2009, 05:59 PM
  3. Daemon problem
    By gandalf_bar in forum Linux Programming
    Replies: 3
    Last Post: 07-20-2004, 06:23 AM
  4. Writting a daemon...
    By Unregistered in forum Linux Programming
    Replies: 2
    Last Post: 08-20-2002, 10:13 AM
  5. Daemon Programming
    By gnu-ehacks in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-24-2001, 03:10 PM

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