[英]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.请在这里保持温和,我今天早上才开始玩 CakePHP(或任何框架,就此而言)。 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.
在浏览了 Cake 关于建立博客的简短教程后,我爱上了它,并想将其应用到我目前正在从事的另一个项目中,我只需要一点帮助。 I need to establish and write to a socket over a TCP connection.
我需要通过 TCP 连接建立并写入套接字。 I have been playing with CakeSocket for the last hour and have made approximately 0 progress.
我在过去一个小时里一直在玩 CakeSocket,并且取得了大约 0 的进展。 I have been reading up on CakeSocket , but honestly can't even figure out how to include the thing.
我一直在阅读CakeSocket ,但老实说,我什至不知道如何包含它。 (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!
(关于 cake apis 如何解释这一点的任何指导也会有所帮助,因为我想在未来利用我可以利用的所有资源——并且我确信我已经做了一个巨大的疏忽。无论如何——这是一点点到目前为止(这是控制器)。任何建议、示例或链接都会非常有帮助。谢谢!
<?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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.