PHP Sockets and TCP/IP comms

I need to write the following process and would like some guidance on where to start with this and if there are any useful libraries I should be looking at.

1. The process will sit on a Linux box
2. It will listen for incoming connections on two ports, say 60000 and 60001
3. Once both incoming connections are up, the process needs to pass everything from port 60000 to 60001 and visa-versa
4. When either of the connections is dropped, communication should stop.

Help gratefully received.


PS: I am not adverse to use another scripting language if that will be better for the job.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The example should be nearly the same you are searching for.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
correct me if I'm wrong, but isn't that for outgoing connections only?

can it really listen freely on a given port for incoming connections?

like the way apache does on port 80, or what ever port you decide?

if that's the case, I would setup apache to listen on port 60000 or what ever port you want to use, then use @hernst42 example to make the outgoing connection.
sheepfarmerAuthor Commented:
the trouble with the apache solution I would have thought would be that the connecting client would have to talk HTTP or similar.  This needs to be a raw data pass...
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

It would be worth a try,

I honestly don't know.

to do what you are asking for, just isn't quite possible. That wasn't the intended purpose for PHP.

You would have to build a custom application using something like c++. PHP is only executed when something calls it. You can build a c++ program that listens on what ever ip and port you want then calls the php script. But that's about it.

I'm not much of a c++ programmer myself, so I can't help you there.

I would give the apache thing a try.

it couldn't hurt.
http is just a protocol that tells the server that the client is requesting resources from port 80 on a given ip address.
sheepfarmerAuthor Commented:
As I said I am happy to use another scripting language, perhaps Perl would be more suitable.
That said, PHP is used for some diverse applications and can be run from the command line, so I don't see any reason why this can't be done in PHP.

I wonder if Pear has any comms libraries.
because it doesn't sit there resident in memory listening on ports.

It doesn't have any classes or assemblies or anything that will allow it to do it.

that's why I said you can make another application that calls the php script. like c++. it can call to the command line and run php. is for cgi applications. It also wouldn't help you here.

unfortunately this is a question for the c/c++. There isn't anything php can do on it's own that will help you.
sheepfarmerAuthor Commented:
cron ?
To accomplish that task you can run php from the command line with the socket-functions. Basically it goes like this:

$s1 = socket_create_listen(60000);
$s2 = socket_create_listen(60001);
// wait for connection on port 60000
$c1 = socket_accept($s1);
//wait for connection on port 60001
$c2 = socket_accept($s2);
//  copy data from port 60000 to 60001
while ($data = socket_read($s1, 100000)) {
    socket_send($s2, $data, strlen($data));

Open in new window

nociSoftware EngineerCommented:
The tool you are looking for is (i think) socat.
The previous solution is one way.

The following would suffice:
socat TCP4-LISTEN:60000 TCP-LISTEN:60001
and to repeat it...:
See snippet.
while true
   socat TCP4-LISTEN:60000 TCP-LISTEN:60001

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.