[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

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.

Regards
Gary

PS: I am not adverse to use another scripting language if that will be better for the job.
0
sheepfarmer
Asked:
sheepfarmer
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
hernst42Commented:
See http://www.php.net/manual/en/ref.sockets.php
The example should be nearly the same you are searching for.
0
 
nplibCommented:
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.
0
 
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...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nplibCommented:
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.
0
 
nplibCommented:
http is just a protocol that tells the server that the client is requesting resources from port 80 on a given ip address.
0
 
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.
0
 
nplibCommented:
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.
0
 
sheepfarmerAuthor Commented:
cron ?
0
 
hernst42Commented:
To accomplish that task you can run php from the command line with the socket-functions. Basically it goes like this:

<?php
$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

0
 
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.
#/bin/bash
while true
do 
   socat TCP4-LISTEN:60000 TCP-LISTEN:60001
done

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now