gpt4 book ai didi

php - 从本地主机到实时服务器的 PHP 套接字编程

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:13 24 4
gpt4 key购买 nike

我想在 php 中使用套接字编程,这里我想将响应从本地机器发送到实时服务器(例如 www.example.com)。

在 example.com 中我想捕捉响应。

我尝试了很多网上的方法,但没有一个对我有用。

假设...我想将此技术用于聊天应用程序。

我能够从本地到本地以及从 xyz 实时服务器到 xyz 实时服务器发送和接收响应....

现在我想从本地到 xyz 或 abc 到 xyz 访问这个

//client.php
<form method="POST">

<input type="text" name="message">
<input type="submit">
</form>


<?php
if (isset($_POST['message']) && $_POST['message'] != ''){
/*
* PHP Sockets - How to create a sockets server/client
*/

//include the server.php script to start the server
//include_once('server.php');

$host = "192.186.246.136";
$port = 4096;

$message = $_POST['message'];

echo "Message To server :".$message;
// 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, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
}
?>




//Server.php
<?php
// set ip and port
$host = "192.186.246.136";
$port = 4096;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
// bind socket to port
$result = socket_bind($socket, $host, $port);
// start listening for connections
$result = socket_listen($socket, 3);

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input."<br />";
// reverse client input and send back
$output = strrev($input) ."<br />";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>

最佳答案

也许 worker 可以帮助你。它是 php 的套接字框架。

http://github.com/walkor/workerman

例如一个简单的聊天服务器

文件:Applications/SimpleChat/start.php

<?php
use Workerman\Worker;

$global_uid = 0;
$connections_array = array();

function handler_connection($connection)
{
global $connections_array, $global_uid;
$connection->uid = ++$global_uid;
$connections_array[$connection->uid] = $connection;
}

// send to all
function handle_message($connection, $data)
{
global $connections_array;
foreach($connections_array as $conn)
{
$conn->send("user[{$connection->uid}] said: $data");
}
}

// clear
function handle_close($connection)
{
global $connections_array;
unset($connections_array[$connection->uid]);
}


// Websockt
$text_worker = new Worker("Text://0.0.0.0:2347");

// one process
$text_worker->count = 1;

$text_worker->onConnect = 'handler_connection';
$text_worker->onMessage = 'handle_message';
$text_worker->onClose = 'handle_close';

然后就可以使用telnet命令来测试了。

telnet 127.0.0.1 2347
...

关于php - 从本地主机到实时服务器的 PHP 套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com