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

Socket Connection between PHP and C++

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

Server.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 代码是 index.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>

I 运行 Server.cpp 在终端上 运行 index.php 在 browser.When I 运行 index.php 在浏览器上输入查询说“123456 7890”,服务器在终端上打印出消息 123456 7890。

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

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

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

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