...

socket_read()

closer
08-03-2002, 12:59 AM
Im trying to read input from an open port using socket_read(). Its actually the exact demo script in the manual (changed to my IP address). Um, The problem im having is that its only reading one letter at a time and not reading returns or spaces. Im thinking it may have something to do with IIS or just the fact that im on a windows machine. Can someone give me a pointer or two?

-Scott

This is the code:


<?php
error_reporting (E_ALL);

set_time_limit (0);
ob_implicit_flush ();

$address = '10.0.0.198';
$port = 23;

if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($sock) . "\n";
}
if (($ret = socket_bind ($sock, $address, $port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
}
if (($ret = socket_listen ($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) . "\n";
break;
}
/* Send instructions. */
$msg = "\r\nWelcome to PHP telnet server. \r\n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\r\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (FALSE === ($buf = socket_read ($msgsock, 2048, PHP_NORMAL_READ))) { // added PHP_NORMAL_READ
echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\r\n";
break 2;
}
if (!$buf = trim ($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close ($msgsock);
break 2;
}
$talkback = "$buf";
socket_write ($msgsock, $talkback, strlen ($talkback));
echo "$buf\r\n";
} while (true);
socket_close ($msgsock);
} while (true);

socket_close ($sock);
?>

firepages
08-03-2002, 06:21 PM
how are you talking to the server ... via telnet or another script ?

closer
08-04-2002, 03:08 PM
Well the script is basically a loop-back script only talking to itself. so if i called the script the server and the script the client, the script would be talking to itself via TCP direct on port 23. I actually developed a workaround for my problem by just looping through the keys and add them into an array. when a \r or \n is found it just takes that array, trims it, and sets it against the commands i want. um, Im still very interested if anyone knows the answer to my first question.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum