21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket,'192.168.0.34', 6003);
// Write some test data to our socket
$msg = file_get_contents('client4', 'r');//获取一个二进制文件内容
if(!socket_write($socket, $msg))
{
echo("<p>Write failed</p>");
}
// Read any response from the socket
while($buffer = socket_read($socket, 1))
{
//把接收的内容写入名为get4的文件----这个文件没生成
$fp = fopen('get4','w');
fwrite($fp,$buffer);
fclose($fp);
echo("<p>Data sent was: SOME DATA<br> Response was:" . $buffer . "</p>");
break; //执行一次跳出
}
echo("<p>Done Reading from Socket</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);
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();
<?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();
}