php socket 的问题 本帖最后由 qq85168163 于 2013-11-06 18:02:41 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你把代码if(!socket_write($socket, $msg))改为 if(!socket_write($socket, $msg,strlen($msg)))试试。如果还是卡。建议你使用strace -p 服务器端进程号。 会有输出内容,贴出来。帮你分析下 <?php/* * * 客户端 */class SocketClient { protected $client; protected $message; public function __construct($domain,$port) { $this->init(); $this->client = stream_socket_client("tcp://$domain:$port", $errno, $errstr, 300000); if (!$this->client) { $this->log("$errstr ($errno)"); return FALSE; } $this->log('client ok'); } protected static function init() { error_reporting(E_ALL ^ E_NOTICE); //error_reporting(0); set_time_limit(0); ob_implicit_flush(); date_default_timezone_set('Asia/Shanghai'); ignore_user_abort(TRUE); mb_internal_encoding('gbk'); } public function sendMessage($msg) { if ($msg === '') { return -1; } try { stream_socket_sendto($this->client, $msg); } catch (Exception $exc) { //$this->log($exc->getTraceAsString()); } } public function getMessage() { $this->message = stream_socket_recvfrom($this->client, 10270000); //$this->log("收到消息:"); //$this->log($this->message); fwrite(STDOUT, $this->message . "\r\n"); } public function shutdown() { stream_socket_shutdown($this->client, STREAM_SHUT_RDWR); fclose($this->client); } public static function log($message) { echo "\r\n" . $message . "\r\n"; }}//$client = new SocketClient('127.0.0.1',12345); $client->sendMessage($msg); echo $client->getMessage();} <?php/* * * 客户端 */class SocketClient { protected $client; protected $message; public function __construct($domain,$port) { $this->init(); $this->client = stream_socket_client("tcp://$domain:$port", $errno, $errstr, 300000); if (!$this->client) { $this->log("$errstr ($errno)"); return FALSE; } $this->log('client ok'); } protected static function init() { error_reporting(E_ALL ^ E_NOTICE); //error_reporting(0); set_time_limit(0); ob_implicit_flush(); date_default_timezone_set('Asia/Shanghai'); ignore_user_abort(TRUE); mb_internal_encoding('gbk'); } public function sendMessage($msg) { if ($msg === '') { return -1; } try { stream_socket_sendto($this->client, $msg); } catch (Exception $exc) { //$this->log($exc->getTraceAsString()); } } public function getMessage() { $this->message = stream_socket_recvfrom($this->client, 10270000); //$this->log("收到消息:"); //$this->log($this->message); echo $this->message . "\r\n"; } public function shutdown() { stream_socket_shutdown($this->client, STREAM_SHUT_RDWR); fclose($this->client); } public static function log($message) { echo "\r\n" . $message . "\r\n"; }}//使用方法$client = new SocketClient('127.0.0.1',12345);$client->sendMessage($msg);echo $client->getMessage(); 为何在循环内open write close?那不是不断在IO么?send后sleep几秒试试 js传值给html问题 一些PHP程序做出来的安装程序数据库的连接用户名和密码如何定义的? 自学PHP file_get_contents函数有时候可用,有是有提示连接超时 fopen除了r方式,都打不开文件? php + mysql 存图片的问题 $_SERVER['HTTP_REFERRER']在什么情况下无效? 在PHPMyadmin-2.5.0上进行在线导出数据时,为何会出错呢? 关于session的问题,兄弟们来帮帮忙。 有事,回家两天。凡来者,给分,版主可不要删贴噢! PHP 数组 thinkphp通过模块]怎么接收URL参数
if(!socket_write($socket, $msg))
改为
if(!socket_write($socket, $msg,strlen($msg)))
试试。
如果还是卡。建议你使用strace -p 服务器端进程号。 会有输出内容,贴出来。帮你分析下
<?php/*
*
* 客户端
*/class SocketClient { protected $client;
protected $message; public function __construct($domain,$port) {
$this->init();
$this->client = stream_socket_client("tcp://$domain:$port", $errno, $errstr, 300000);
if (!$this->client) {
$this->log("$errstr ($errno)");
return FALSE;
}
$this->log('client ok');
} protected static function init() {
error_reporting(E_ALL ^ E_NOTICE);
//error_reporting(0);
set_time_limit(0);
ob_implicit_flush();
date_default_timezone_set('Asia/Shanghai');
ignore_user_abort(TRUE);
mb_internal_encoding('gbk');
} public function sendMessage($msg) {
if ($msg === '') {
return -1;
}
try {
stream_socket_sendto($this->client, $msg);
} catch (Exception $exc) {
//$this->log($exc->getTraceAsString());
}
} public function getMessage() {
$this->message = stream_socket_recvfrom($this->client, 10270000);
//$this->log("收到消息:");
//$this->log($this->message);
fwrite(STDOUT, $this->message . "\r\n");
} public function shutdown() {
stream_socket_shutdown($this->client, STREAM_SHUT_RDWR);
fclose($this->client);
} public static function log($message) {
echo "\r\n" . $message . "\r\n";
}}//$client = new SocketClient('127.0.0.1',12345);
$client->sendMessage($msg);
echo $client->getMessage();
}
<?php/*
*
* 客户端
*/class SocketClient { protected $client;
protected $message; public function __construct($domain,$port) {
$this->init();
$this->client = stream_socket_client("tcp://$domain:$port", $errno, $errstr, 300000);
if (!$this->client) {
$this->log("$errstr ($errno)");
return FALSE;
}
$this->log('client ok');
} protected static function init() {
error_reporting(E_ALL ^ E_NOTICE);
//error_reporting(0);
set_time_limit(0);
ob_implicit_flush();
date_default_timezone_set('Asia/Shanghai');
ignore_user_abort(TRUE);
mb_internal_encoding('gbk');
} public function sendMessage($msg) {
if ($msg === '') {
return -1;
}
try {
stream_socket_sendto($this->client, $msg);
} catch (Exception $exc) {
//$this->log($exc->getTraceAsString());
}
} public function getMessage() {
$this->message = stream_socket_recvfrom($this->client, 10270000);
//$this->log("收到消息:");
//$this->log($this->message);
echo $this->message . "\r\n";
} public function shutdown() {
stream_socket_shutdown($this->client, STREAM_SHUT_RDWR);
fclose($this->client);
} public static function log($message) {
echo "\r\n" . $message . "\r\n";
}}//使用方法$client = new SocketClient('127.0.0.1',12345);
$client->sendMessage($msg);
echo $client->getMessage();