0

hey so I've been looking around a lot but i couldn't get a accurate answer and I want to communicate php and c++ with sockets and i have spend hours trying to find any example but I cannot find.

server:

WSADATA wsa_data;
    SOCKADDR_IN server_addr, client_addr;

    int wsa = WSAStartup(MAKEWORD(2, 2), &wsa_data);
    if (wsa != 0)
        return;

    const auto server = socket(AF_INET, SOCK_STREAM, 0);

    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(1111);

    ::bind(server, reinterpret_cast<SOCKADDR*>(&server_addr), sizeof(server_addr));
    listen(server, 0);
    printf("listening for connections\n");

    int client_addr_size = sizeof(client_addr);
    for (;;)
    {
        SOCKET client;
        if ((client = accept(server, reinterpret_cast<SOCKADDR*>(&client_addr), &client_addr_size)) != INVALID_SOCKET)
        {
            SOCKET soc = client;
            string str = inet_ntoa(client_addr.sin_addr);

            std::cout << "connec" << std::endl;
            char dobj[1024];
           auto n = recv(soc, dobj, 1024, 0);

           std::cout << n << std::endl;
           std::cout << dobj << std::endl;
        }

        const auto last_error = WSAGetLastError();

        if (last_error > 0)
        {
            printf("error: %i\n", last_error);
        }
    }
    closesocket(server);
    WSACleanup();

client:

<?php
    if (isset($_GET['submit'])){
        $query = $_GET['query'];
        echo $query;
        error_reporting(E_ALL);
        $service_port = 1111;
        $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, 10)) {
            echo $out;
        }

        socket_close($socket);
    }
?>  

here are my few attemts my code says success for connect and the php side is just stuck on sending and i cant figure it out

life less
  • 63
  • 4

1 Answers1

1

I made some tiny changes in your code and got it working. Depending on what exactly you want to do you should be able to adapt it to your needs.

In short: I changed PHP socket_send to socket_write (because socket_send seems to wait "forever") and changed while ($out = socket_read($socket, 10)) to while ($out = socket_read($socket, 10) > 0) because the loop won't stop after the first receive.
In C++ I added send(soc, "got msg!", 10, 0); after recv() to send a message back to PHP (which PHP waits for).

In the code below the C++ program should be started first. It waits for an established connection, prints some text it receives and "answeres" with "got msg!". The PHP script then connects to the socket, sends some string, waits for a response and prints it. Be aware that PHP cannot run forever, only just for a few seconds, depending on your server. So you should consider a design, where PHP just gets called to send and receive one message!

C++

#include <iostream>
#include <winsock.h>

int main()
{
    WSADATA wsa_data;
    SOCKADDR_IN server_addr, client_addr;

    int wsa = WSAStartup(MAKEWORD(2, 2), &wsa_data);
    if (wsa != 0)
        return 1;

    const auto server = socket(AF_INET, SOCK_STREAM, 0);

    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(1111);

    bind(server, reinterpret_cast<SOCKADDR*>(&server_addr), sizeof(server_addr));
    listen(server, 0);
    printf("listening for connections\n");

    int client_addr_size = sizeof(client_addr);
    for (;;)
    {
        SOCKET client;
        if ((client = accept(server, reinterpret_cast<SOCKADDR*>(&client_addr), &client_addr_size)) != INVALID_SOCKET)
        {
            SOCKET soc = client;
            std::string str = inet_ntoa(client_addr.sin_addr);

            std::cout << "connected" << std::endl;
            char dobj[1024];
            auto n = recv(soc, dobj, 1024, 0);

            std::cout << n << std::endl;
            std::cout << dobj << std::endl;

            // I added the following two lines:
            send(soc, "got msg!", 10, 0);
            std::cout << "sent response" << std::endl;
        }

        const auto last_error = WSAGetLastError();

        if (last_error > 0)
        {
            printf("error: %i\n", last_error);
        }
    }
    closesocket(server);
    WSACleanup();

    return 0;
}

PHP

<?php
    if (isset($_GET['submit'])){
        $query = $_GET['query'];
        echo $query;
        error_reporting(E_ALL);
        $service_port = 1111;
        $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_write($socket, $in, strlen($in));
        echo "OK.\n";

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

        socket_close($socket);
    }
?>
SaschaP
  • 883
  • 1
  • 7
  • 25