I have the below sample data stream, which has to be transmitted to a system, through socket programming. This stream is generated from db. Every 243 char I get a new stream.
> 0243P254NIP40524KCK0240000000104844200001
> 00000064005736540000000985714052400000000000010000000000000000BIR01082013KCK*IBKL0KCK01*130718i28535983*lazy
> frog
> KCK0240000000104844200001KCK0240000000104844200001KCK02400000001048442
i tried below code but how do i read the stream and map the response to the input.
every time i get the below message from server.php "Could not read input" from client.php "Could not send data to server"
pls tell me how can i make sever.php read the data and client.php send the data
server.php
<?php
$host = "217.16.1.12";
$port = 10001;
$message = "Hello Client";
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// put server into passive state and listen for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
$com = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($com, 1024000) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client says: ".$input;
socket_write($com, $message , strlen ($message)) or die("Could not write output\n");
// close sockets
socket_close($com);
socket_close($socket);
?>
client.php
<?php
include ("db/Config.php");
$host = "217.16.1.12";
$port = 10001;
$data = mysql_query("select isbn from klrans where isbn is not NULL and isbn <> '' limit 5");
while($info = mysql_fetch_array( $data ))
{
$ifsc= $info['isbn'];
$msg .= "<td align='center'>".$isbn. "</td> ";
}
$message = $msg;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die ("Could not connect to server\n");
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024000) or die("Could not read server response\n");
echo "Server says :".$result;
// close socket
socket_close($socket);
?>
You need a loop in order to read from a socket. The reason is that you don't know when the data is arriving so you have to actively check whether the remote system has sent something to you. There is a full example in the PHP docs: http://www.php.net/manual/en/sockets.examples.php
Here is the juice: