to begin with the fputs works for sending chat from the PHP site to this gaming server. But when I use fgets to get all the chatlogs from the server it doesnt return it, bascially for example when I write something in the textfield and send it using the button in the following code, it sends message to the server, I want it to return that value back from the server, or get all the chatlogs from the server and put it into the client which is a php website, here is the code:
<?php
$host="192.168.0.6";
$port = 7777;
// open a client connection
$fp = fsockopen ($host, $port, $errno, $timeout);
if (!$fp){
$result = "Error: could not open socket connection";
}
else
{
echo '<center><form action="tcpclient.php "method="POST" /></center>';
echo '<center>'."Nickname: ".'<input type="text" name="nick" size="31" /></center>';
echo '<center>'." Text: ".'<input type="text" name="chat" size="150" /> <input type="submit" name="send" value="Send" /></center>';
if (isSet($_POST['send'])) $formvalue =$_POST['chat'];
//$name = $_POST['nick'];
//if (isSet($_POST['send'])) $formvalue + $_POST['chat'];
fputs ($fp, $formvalue);
}
$result = fgets ($fp, 7777);
echo $result;
?>
fgets()only reads one line (terminated by"\n") and will block until"\n"is received or the connection is closed. Make sure one of these things happens. (Usetelnetorncprograms)Oh and I’m not sure, but it might be expecting
"\r\n"on Windows, and won’t react to"\n"alone. Don’t take my word on it though.If there is no newline, use
freadand a combination ofstream_selectandstream_set_blocking(refer to the manual).If it’s anything even more complex, you might want to switch to
socket_*functions. Note that sockets and streams don’t mix.Sorry, I’m too lazy for an example. You can look here for a general example of
stream_selectin action (disclaimer: the code is ugly, I know.)Basically, you’d
stream_set_blockingto 0 and loop withstream_selectandfreaduntil you’ve read everything (if you can detect that) or hit a timeout. It’s hard to suggest anything not knowing the protocol.