sharing a socket sonnection between c++ server and php script?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sharing a socket sonnection between c++ server and php script?
hiho@ll
has anybody an idea how i can share a connection between a c++ server who accept(); connections and forwards the connection to a php script which can use this connection
it would also be possible to use a c++ prog for the php script which handles reading/writing from/to the socket
that's my problem:
what do i have to share (which piece of information) between the server and the script so it can communicate through the socket?
what do i have to do to use the information shared?
PHP has its own socket support . So maybe it would be better to do the socketing in PHP itself instead of C++? Sharing the same socket is (close to) impossible.
The socket (ie some numbers in mem) belong to the program (ie process) that created/attached to it. I don't believe you can pass the info to another program...
1. It is indeed possible to share connections between processes (via sendmsg/recvmsg over "Unix sockets"), but there are
many pitfalls and complexities to do so effectively, It is *not* an undertaking for the novice ... but it *is* possible.
2. Thinking: could you please tell us a bit more about the requirements for this "router", and exactly why you can't
simply have one process (perhaps a multithreaded process)?
Perhaps if you could give us a few more details, somebody might be able to suggest some good design solutions for you.
3. Also, is C++ necessarily one of your requirements? Would, for example, PHP or Java eb options for you?
I know you can send data/info through a socket to another prog/process, but it sounded like Thinking was trying to create a socket in 1 program/process and then pass the actual socket itself to another program/process, which I didn't think was possible...
A quick look at man sendmsg doesn't say anything about sending the actual socket...
Hope this clarifies my prev comment.
coool
i now got the fundamental part run and i could test the whole stuff
look at this:
i start a php script in console mode:
./myscript.php
this script opens a fsockopen connection to a c++ server which accepts connections at the specific port
now to the c++ server:
the server does accept();
this means he gets a descriptor (an integer) the server can use to read/write from
BUT
i don't need the server to read/write to/from the socket but any prog i want!
int client=accept(...);
sprintf(buffer,"./scriptname %d",client);
system(buffer); // call the script "scriptname" using argv[1]=client
the above code works!!!!!
on the server side i can call any script i want
for example i on the server side i had a test php script (serverside.php) which reads and writes something
serverside.php uses a C++ prog for reading and writing to the socket i got through command line arguments!
the c++ prog only does write(client,buffer,bufferlength); or read(client,buffer,bufferlength);
now comes a question i'm not sure if this works:
if the above example works (and it works defentily for me) does this mean i can use opened connections system wide?
if this is true why does every process has it's own stdout stdin stderr at 1,2,3 descriptors?
maybe because if i use a system(); call every open descriptor is inheritated by the child from its parent except stdout,stdin and stderr?
Chrism01 - you're correct: that's exactly what he's trying to do (create a descriptor - e.g. a socket - in one process, and then use that same descriptor in another process).
In general ... *you can't do that*! In general, if you open a file in process A, you can't just pick a "magic number" and then start reading and writing from the same file in process B. It's exactly the same with sockets:
this works:
server gets connection and forwards to another process
this doesn't work:
a process asks a server who created a connection pool (so the server doesn't get the connection, but has already some connections opened) for such a connection and uses it
the only thing i want to do is the following:
let some scripts talk to each other independent of the language the script is written in!
this means i can create something like a tunnel from a php script to a perl script
or java to a bash script
or c to python script
and to be fast i don't want a tunnel which means a proxy process between them
but i want the progs talk directly to each other
As paulsm4 said, you can't create a 'global' socket id and pass it around..
What you have to do is have each prog act as both a server and a client.
Each one listens on a separate port num eg start at 8001 and count up 1 for each program...
If you are going to have a variable num of progs talking, maybe have a flat file with a list of port nums in use with prog name?
Each prog can then read the file to find out where everybody else is. If the num of progs varies with time, each prog will have to regularly re-read the the file.
Do you have a specific requirement for this?
I've never actually seen it done.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.