6

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

shiven
  • 421
  • 2
  • 8
  • 19
  • 1
    You need to set `$compilerhost` and `$compilerport` to whatever is appropriate for your application. They're the name and port number of the server you want to connect to. – Barmar May 06 '13 at 20:03
  • not my code googled it .I did not find any of these variables initiated anywhere in the code – shiven May 06 '13 at 20:05
  • Where did you see it? – Barmar May 06 '13 at 20:05
  • where did i see the code? – shiven May 06 '13 at 20:06
  • 1
    I guess you found it at this site: https://github.com/sankha93/codejudge. The variables are set in the file `dbinfo.php`, which is created by `install.php`. – Barmar May 06 '13 at 20:07
  • 2
    You're not being very specific with *what* you don't understand, which is why my answer is short and perhaps too vague for you. You say that you don't understand the documentation: Great, that means you have a question. Which part of the documentation don't you understand? Quote it and ask about it! – phant0m May 06 '13 at 20:10
  • Firstly was unclear about the functioning of fsockopen() in particular what does fwrite,fgets do wrt to a socket, secondly in the above code i was unable to find where the variables $compilerhost $ compilerport were initiated,but it was solved by @Barmar – shiven May 06 '13 at 20:16
  • Basically, fsockopen() is just like fopen(), except that it communicates with a network server instead of reading/writing a file. – Barmar May 06 '13 at 20:17
  • Guys, the problem is not the OP. The problem is you guys need very specific questions. Its like, he's getting barraged for asking general "How does a kettle work", because you guys are only capable of answering ultra specific questions like "Which way do I turn the knob to light the fire?". Please, with questions like these, help to shed some general light on the overall purpose, or practical use of fsockopen. Even the accepted answer (by phant0m) only 1 sentence long gives the OP an idea of what the purpose is and how it works in the background. The PHP.net explanation is far from friendly. – aDvo Jul 24 '14 at 06:56
  • Just googling fsockopen brings this SO question as the 3rd search result. I'm sure alot of people are wondering the same question. – aDvo Jul 24 '14 at 06:59
  • 1
    refer to this: https://www.studytutorial.in/asynchronous-process-multi-threading-or-multitasking-in-codeigniter-php – SNG Oct 10 '17 at 04:46

1 Answers1

12

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.

phant0m
  • 16,595
  • 5
  • 50
  • 82