fsockopen() how does it actually work? [closed]
Asked Answered
F

1

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

Fluky answered 6/5, 2013 at 20:1 Comment(11)
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.Horus
not my code googled it .I did not find any of these variables initiated anywhere in the codeFluky
Where did you see it?Horus
where did i see the code?Fluky
I guess you found it at this site: github.com/sankha93/codejudge. The variables are set in the file dbinfo.php, which is created by install.php.Horus
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!Overlay
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 @HorusFluky
Basically, fsockopen() is just like fopen(), except that it communicates with a network server instead of reading/writing a file.Horus
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.Stallfeed
Just googling fsockopen brings this SO question as the 3rd search result. I'm sure alot of people are wondering the same question.Stallfeed
refer to this: studytutorial.in/…Adonai
O
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.

Overlay answered 6/5, 2013 at 20:8 Comment(2)
fgets($socket) would enable me to read from the stream?Fluky
@Fluky Yes, it does.Overlay

© 2022 - 2024 — McMap. All rights reserved.