phpsocketsfsockopenphp-socket

fsockopen() how does it actually work?


I am trying to develop a code judge software in PHP for programming assignments. I would try o compile codes on a java server which would require socket programming.I have litle knowledge of socket programming in PHP. I googled and found a code doing some similar work.But i am still unable to get the jist of things..how does it actually work? The manuals are too technical to get a good understanding

here's a piece of the code,i have written comments as per my understanding:

  $socket = fsockopen($compilerhost, $compilerport); // creates connection it returns the file pointer

if($socket) {  // if it returns a file pointer
fwrite($socket, $_POST['filename']."\n");  //write the filename in the file pointer returned by socket and chagne line

$query = "SELECT time, input, output FROM problems WHERE sl='".$_POST['id']."'"; // select input,output,for the problem

$result = mysql_query($query);

$fields = mysql_fetch_array($result);

fwrite($socket, $fields['time']."\n");  // write the time  in the file pointer returned

I do not understand how does fsockopen works?How is fwrite used here?

a note: while going through the code i nowhere found $compilerhost, $compilerport variables.

rectify my doubts.Thanks in advance and pardon for the bad english


Solution

  • fsockopen()[PHP.net] allows you to communicate with a server by providing you a handle to a stream you can read from and write to.

    This "handle" is stored in the $socket variable, it's just an identifier for the fwrite[PHP.net] function and brothers and sisters such that it knows which stream to write to etc.

    When you fwrite() something, it gets sent to the server.