There are multiple client programs that periodically connect to a port on my server and send a single line of text.

When a connection to the port is made I need to start a PHP CLI script that processes the data. There may be many of the remote scripts running/connecting at more or less the same time so I think it would be best if socat forked a process for each connection to run the script.

I've gotten socat to do most of what I need, using the command

socat tcp-l:myport,fork exec:mypath/socatTest.php

I can read the input on php://stdIn. All is good.

The problem is that the process doesn't seem to fork, so if a second external program sends data while another is doing the same it gets a connection refused error.

Where have I gone wrong?


Not using socat

why not use PHP to listen on that socket and process your information? If you check out socket_accept, you'll see a few examples below on listening to a socket and forking that process for multiple clients. there is also another example under here - custom-web-proxy-is-better-to-fork-process-or-make-http-requests

Regarding socat

first, by no means have a worked with socat. so this is completely based on review of the documentation. so from what i can tell by documentation here, it would appear that exec:<command-line> should fork a process regardless. it doesn't seem to appear that you need to explicitly say it in options.

EXEC or SYSTEM addresses invoke a program using a child process and transfer data between socat and the program.

have you gotten this to work before?

| improve this answer | |
  • Thanks for the comment Austin. The program in question currently operates as you suggest. I'm trying to segregate app functionality from network functionality as much as possible and use well maintained proven tools like socat instead of rolling my own. – RoyHB Dec 3 '12 at 19:22
  • @RoyHB - since i already updated with an answer, i figured i'd just edit my answer to include some socat stuff. this is completely based on review of the documentation. i've never worked with it... so it could be a crap answer. but hey i gave it a shot :). also, have you ever gotten this to work correctly? – au_stan Dec 4 '12 at 12:44
  • Your efforts spurred me to spend a bit of time digging deeper and, in the course of that, I threw my hands up and decided to spend the time making the daemonizable multi-client socket server routine I already had work well enough for production. Thanks again – RoyHB Dec 4 '12 at 19:42
  • not a problem. i too learned a little about socat :) – au_stan Dec 4 '12 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.