简体   繁体   中英

Establishing a TCP connection with CakeSocket

Please be gentle here, I have only started playing with CakePHP (or any framework, for that matter) this morning. After just walking through Cake's brief tutorial on setting up a blog I have fallen in love, and would like to apply it to another project I am currently working on, I just need a bit of help. I need to establish and write to a socket over a TCP connection. I have been playing with CakeSocket for the last hour and have made approximately 0 progress. I have been reading up on CakeSocket , but honestly can't even figure out how to include the thing. (any guidance on how the cake apis explains this would be helpful too, because I would like to make use of all resources that I can in the future - and am certain I have made a giant oversight. In any case - here is what little I have so far (this is the controller). ANY advice, examples or links would be extremely helpful. Thanks!

<?php
class ConnectionController extends AppController {
public function index() {
    $socket = new CakeSocket();
    // For the purpose of this post I have changed the host and port.
    // I am using a static IP as the host.
    $socket->_construct(array(
        'persistent'    => false,
        'host'          => 'localhost',
        'protocol'      => 'tcp',
        'port'          => 80,
        'timeout'       => 30
    ));
    if ($socket->connect()) {
        setFlash('Connected');
    } else {
        setFlash('Not Connected.');
    }
}
}
?>
App::uses('CakeSocket','Network');
$socket = new CakeSocket( array('host'=>$ip,'port'=>$port));
if($socket->connect()) {
  $socket->write($data);
  $socket->disconnect();
  return true;
}
return false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM