We help IT Professionals succeed at work.

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.
Watch Question

Top Expert 2007
See http://www.php.net/manual/en/ref.sockets.php
The example should be nearly the same you are searching for.

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.


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...

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.


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.


cron ?
Top Expert 2007
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 Engineer
Distinguished Expert 2019
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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.