LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-14-2005, 12:38 PM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
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?

or anybody has another idea how this can be done?

thx@ll
 
Old 09-14-2005, 01:16 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 09-14-2005, 01:43 PM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
do you mean i should do the socket server in php?

well for my project this is impossible!
there must be a solution
i'm searching already for a few days and nothing found
 
Old 09-14-2005, 07:57 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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...
 
Old 09-14-2005, 09:59 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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?

Thanx in advance .. PSM
 
Old 09-15-2005, 02:50 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 09-15-2005, 04:39 AM   #7
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
IT WORKS

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?

thx@ll

Last edited by Thinking; 09-15-2005 at 04:41 AM.
 
Old 09-15-2005, 09:45 AM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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:

http://www.developerweb.net/sock-faq/detail.php?id=125
 
Old 09-15-2005, 10:38 AM   #9
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
well
but it works that way!

but only the way i described

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
 
Old 09-15-2005, 07:41 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
  


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
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Fedora 3 - PHP - Socket timeout b_whelan Linux - Software 2 09-29-2005 02:41 AM
Socket doesn't time out. (PHP) laserbeamninja Programming 0 04-18-2005 03:23 PM
mysql - php socket error. ldp Linux - Software 9 10-22-2004 03:40 AM
Red Hat 9 server can't write PHP script, problem in httpd.conf file? x_menno_x Linux - Newbie 27 06-29-2004 04:44 PM

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

All times are GMT -5. The time now is 08:40 PM.

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