1

Im trying to understand how a socket works in PHP.

Lets say I have a file called socket.php, and this creates a socket bound to my localhost on port 99.

Then I run the socket in a while loop so it's constantly connected.

is there a function in PHP to make calls to that socket while its listening?

Another question is: If I have another service such as Java running on a socket -- is it a bad idea to use PHP to connect to the socket to make a call. I ask because I could potentially be recreating new socket connections many, many times.

So is having to reconnect to a socket hundreds of times in PHP bad? Or should I re-use the same socket connection somehow? (I am thinking in terms of AJAX calls to PHP which connects to a Java Socket).


Edit: You can see the example code: https://github.com/JREAM/sandbox/tree/master/php

Im trying to communicate with in socket.php and socket_send.php -- I am leaving socket.php running and opening another console and running socket_send.php and trying to get a result into the console.

4
  • 1
    Please post example code. It's not clear what you mean or how you're executing socket_listen. It's fine to use another PHP script to connect to Port 99 and send data to the listening port, regardless of the service that is being used to listen on that port. Take SMTP for example, you can open a PHP Socket to Port 25 and communicate in SMTP with the listening service using fread and fwrite. Commented Nov 29, 2012 at 1:11
  • 1
    A small example I use for a SMTP Script I have:function cmd($str, $report=true){ //Assume connection to socket connection on $fp (File Stream Pointer) //Echo command, format it for socket, and write to stream global $fp; echo htmlspecialchars("> ".$str); echo "\n"; $ret = fwrite($fp, $str."\r\n"); if($report === true){ echo htmlspecialchars(fread($fp, 512)); echo "\n"; } /*else { fclose($fp); echo "</pre>\n"; die("<b>ERROR:</b> Unable to write content to File Pointer Stream.\n"); }*/ } Commented Nov 29, 2012 at 1:16
  • I added a link with some sample code regarding the first question -- give me a while to read through some of this and think hard :) Commented Nov 29, 2012 at 1:20
  • I don't see in your socket.php where any of the IF statements would become TRUE, thus your would never perform a socket_read(). Where do $read or $client get assigned any array data? Commented Nov 29, 2012 at 1:33

1 Answer 1

1

Answer to your first Question: I suggest going over here everything you need about sockets is there. Basically the function you want to use is socket_read or socket_recvfrom if using UDP.

Answer to your second Question: Sockets are just a way to send messages to services. It doesn't matter if a client is in php and the server is in Java. Think of it this way. Does it matter that you are viewing a web-page on a linux Web Server with a windows Box?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.