4

I've been stuck for two days with the following problem: I wrote a java socket server which can receive and send data to a socket hosted at 'localhost:64005'. I can connect to it via php and send or receive messages. However I cannot send and then receive an answer. I've traced the problem back to the php script i've written.

<?php
    class socketCommunication{
        protected $PK;
        protected $ip = '127.0.0.1';
        protected $port = 64005;
        protected $socket;
        public $result;

        function __construct($key){
         $this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
         $this->result = socket_connect($this->socket, $this->ip, $this->port) or die("Could not connect to server\n");
         $this->PK = $key;
         $this->sendSocket();
        }

        function getResponse(){
            //$input =  socket_read($this->socket, 1024) or die("Could not read");
            $bytes = socket_recv($this->socket, $buf, 2048, MSG_WAITALL);
            return $buf;
        }

        function sendSocket(){
            $len = strlen($this->PK);
            socket_send ($this->socket, $this->PK, $len, 0);
        }
    }
?>

<?php
    //include("/mysql/RandomQuery.php");
    include("/java/socketCommunication.php");

    $object2 = new socketCommunication(100001);
    echo $object2->getResponse();
 ?>

the java socket server:

package Server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.Semaphore;

import Database.Reader;

public class Receive implements Runnable {
    Semaphore semaphore;
    private Socket connection;
    String result = "default";

    public Receive(Socket conn, Semaphore sem){
        this.connection = conn;
        this.semaphore = sem;
    }

    @Override
    public void run() {
            try {
                semaphore.acquire();
                System.out.println(connection.toString());
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
                String userInput;
                while ((userInput = in.readLine()) != null) {
                    System.out.println(userInput);
                    Reader reader = new Reader(Integer.parseInt(userInput));
                    this.result = reader.getResult();
                    System.out.println(result);
                    out.println(result);
                    break;
                }
                connection.close();
                in.close();
                System.out.println("input is closed");
                semaphore.release();
                } catch (IOException | InterruptedException e) {e.printStackTrace();}
    }

}

As soon as I call both the sendSocket and getResponse method in php the page just keeps loading infinitely. However if I just call the sendSocket or getResponse(after changing the java socket so it won't wait for input) method seperatly they work fine.

what am I doing wrong?

Kind Regards.

1 Answer 1

1

See: http://php.net/manual/en/function.socket-recv.php

The MSG_WAITALL flag will block until it has received the full length of the buffer. Which you have specified as 2048 bytes of data.

Sign up to request clarification or add additional context in comments.

3 Comments

That doesn't appear to be the case. I'm currently using a workaround in which I'm using 2 sockets. 1 for receiving and 1 for sending. I receive messages with variable length.
What happens when you try to receive without sending in the php script? When you compare how you are reading the buffer in the two scripts, you are incrementally grabbing chunks of the buffer in Java, but are just assigning the buffer to the php variable. You'd need to take the same approach in php. Browsers don't load the page until those connections have resolved and it seems like your read connection is staying open because it is waiting to receive more data. Browsers started doing this about 6 years ago instead of piping out live data to the page while reading the buffer they block.
When I receive without sending I get the message send by the java server without any problem. The workaround I made is working great so far. I was thinking it might actually be better if I handle the send and receive in different threads. I'll close this question. Thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.