4

I have created a TCP Socket connection between a PHP Page and C++ code. Here is the c++ code for this.

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);
}

and the PHP code is 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 run Server.cpp on terminal and run index.php on browser.When I run index.php on browser and enter a query say "123456 7890", the Server prints outs the message 123456 7890 on terminal.

But the server is not able to send back the message "I got your message" to index.php on browser. Only when I exit the server by pressing Ctrl + C, it sends the message to index.php and only then index.php echoes everything on the browser. What is the problem here? I want to listen to socket in Server.cpp and send messages to index.php infinitely.

skjindal93
  • 706
  • 1
  • 16
  • 34
  • Unrelated to your problem, but you check for success and not for errors. You need to check for errors as well, and report them and handle them somehow instead of just disregarding them. – Some programmer dude Feb 06 '15 at 09:26
  • Actually, if I just write the socket part of code in PHP and run it via terminal. it works well. I don't know what happened on browser. – skjindal93 Feb 06 '15 at 09:29
  • It worked. I just have to add "break" in the while loop of the PHP code after the echo $out line. Thanks..:) – skjindal93 Feb 06 '15 at 09:36

1 Answers1

2

I looks like you have not configured your socket as non blocking (fcntl with O_NONBLOCK flag), so the read operation, read(new_socket, dobj, 1023); will block until it receives 1023 bytes.

You can either configure the socket as non-blocking or read fewer bytes at a time. But the best solution is the first one.

For more details on the full solution to this problem check my answer here

Community
  • 1
  • 1
Pandrei
  • 4,843
  • 3
  • 27
  • 44