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

时间:2015-02-06 09:22:36

标签: php c++ sockets

我在PHP页面和C ++代码之间创建了一个TCP Socket连接。这是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>

我在终端上运行Server.cpp并在浏览器上运行index.php。当我在浏览器上运行index.php并输入一个查询&#34; 123456 7890&#34;时,服务器打印出消息123456 7890 on终端

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

1 个答案:

答案 0 :(得分:2)

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

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

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