I am trying to test a Client-Server communication using Socket programming in PHP. After trying some samples given on PHP Reference site, the communication is working OK.
But, the problem is that I am getting some bogus or invalid characters in the buffer.
Of course, I am able to vanish them using Regex, but it would be more nice to know the reason behind this.
I think that this person is also having the same problem.
Source Code of Server:
error_reporting(E_ALL);
set_time_limit(0);
$address = "tcp://192.168.1.60:9160";
$sock = stream_socket_server($address, $errcode, $errstring);
if (!$sock) {
echo $errcode.": ".$errstring."\n";
}
else {
do {
while ($client = stream_socket_accept($sock)) {
fwrite($client, "*** Welcome ***".PHP_EOL);
$input = trim(fgets($client, 2048));
if (!empty($input)) {
fwrite($client, "Server: ".$input.PHP_EOL);
}
else {
fwrite($client, "--- NO INPUT ---".PHP_EOL);
}
echo trim($input).PHP_EOL;
if ($fp = fopen("log.txt", "a+")) {
fwrite($fp, trim($input).PHP_EOL);
fclose($fp);
}
fclose($client);
}
} while (true);
}
fclose($sock);
Any help would be appreciated :)