gpt4 book ai didi

php - PHP和C++之间的套接字连接

转载 作者:太空狗 更新时间:2023-10-29 21:41:37 26 4
gpt4 key购买 nike

我已经在 PHP 页面和 C++ 代码之间创建了一个 TCP 套接字连接。这是用于此的 C++ 代码。

服务器.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;

int main(){
int create_socket,new_socket,fd;
int n;
socklen_t addrlen;
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
cout<<"The socket was created\n";
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;//INADDR_ANY;inet_addr("localhost");
address.sin_port = htons(4000);
cout<<"\nworking";
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
cout<<"Binding Socket\n";
while(1){
listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);
cout<<"*************************\n";
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
cout<<"*************************\n";
char dobj[1024];
if (new_socket > 0){
n = read(new_socket, dobj, 1023);
string query(dobj);
string strl(query);
string s, d;
istringstream iss(strl);
iss >> s;
iss >> d;
const char* source = s.c_str();
const char* dest = d.c_str();
printf("Here is the message: %s %s\n",source,dest);
n = write(new_socket, "I got your message", 18);
}
}
close(new_socket);
return close(create_socket);
}

PHP代码是索引.php

<?php
if (isset($_GET['submit'])){
$query = $_GET['query'];
echo $query;
error_reporting(E_ALL);
$service_port = 4000;
$address = "localhost";

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false){
echo "Failed: ". socket_strerror(socket_last_error($socket))."\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...'\n";
$result = socket_connect($socket, $address, $service_port);

if ($result === false){
echo "Failed: ". socket_strerror(socket_last_error($socket))."\n";
}

$in = $query;
$out = '';

echo "Sending...\n";
socket_send($socket, $in, strlen($in), MSG_WAITALL);
echo "OK.\n";

echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}

socket_close($socket);
}
?>

<html>
<head>
</head>
<body>
<form action="index.php" method="get">
Query: <input type="text" name="query">
<button type="submit" name="submit">Submit!</button>
</form>
</body>
</html>

我在终端上运行 Server.cpp 并在浏览器上运行 index.php。当我在浏览器上运行 index.php 并输入查询“123456 7890”时,服务器在终端上打印出消息 123456 7890。

但是服务器无法将消息“我收到你的消息”发送回浏览器上的 index.php。只有当我按 Ctrl + C 退出服务器时,它才会将消息发送到 index.php,然后 index.php 才会回显浏览器上的所有内容。这里有什么问题?我想在 Server.cpp 中监听 socket 并向 index.php 无限发送消息。

最佳答案

我看起来你没有将套接字配置为非阻塞(fcntl 带有 O_NONBLOCK 标志),所以读取操作 read(new_socket, dobj, 1023); 将阻塞直到它接收到 1023 个字节.

您可以将套接字配置为非阻塞或一次读取较少的字节。但最好的解决方案是第一个。

有关此问题的完整解决方案的更多详细信息,请查看我的回答 here

关于php - PHP和C++之间的套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362376/

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