5

I am trying to set up a simple TCP connection on a single port between a Java application that will act as the TCP server and a PHP scrip that will act as the Client.

I will post the code for each below, but the problem is: I can connect and send data to the Java server just fine. I can get that data and print it out. My problem arises in trying to send a response back to the php server.

When I comment out that last line of php "echo socket_read($socket, 14, PHP_NORMAL_READ);" The data gets to the Java server just fine. When I add that line back in, the data does not even get to the Java server.

Because of this, I am assuming my problem has something to do with how I am either sending the data from Java, or trying to get the data in PHP from the server.

This has me really stumped, any help would be greatly appreciated!

Java Server:

protected ServerSocket socket;
protected final int port = 9005;
protected Socket connection;
protected String command = new String();
protected String responseString = new String();


socket = new ServerSocket(port);


while(true)
{
    // open socket
    connection = socket.accept();
    // get input reader
    InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
    BufferedReader input = new BufferedReader(inputStream);
    // get output handler
    DataOutputStream response = new DataOutputStream(connection.getOutputStream());

    // get input
    command = input.readLine();

    // process input
    Logger.log("Command: " + command);
    responseString = command + " MC2 It Works!";

    // send response
    response.writeBytes(responseString);
    response.flush();
    response.close();
}

PHP Client:

$address = 'example.com'; // obviously not the address I am using
$port = 9005;
$message = "Test";

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));

socket_connect($socket, $address, $port)

if(socket_send($socket, $message, strlen($message), MSG_EOF) != FALSE)
{
    echo socket_read($socket, 14, PHP_NORMAL_READ);
}
5
  • 1
    responseString = command + " MC2 It Works!"+"\r\n"; Commented Mar 15, 2012 at 6:22
  • I think you must add the EOF of the line that the socket know you finish sending your data Commented Mar 15, 2012 at 6:22
  • Yes that was definitely part of the problem. I figured it all out about 20 minutes after submitting the question. So is life. Commented Mar 15, 2012 at 6:41
  • 1
    Nice work KayoticSully. As a friendly reminder, can you please post an answer to the question yourself and then accept that answer so that we can close this question? Probably you don't have yet enough reputation but keep in mind that you can and should answer your own questions if need. Commented May 29, 2012 at 10:36
  • Thanks, I actually forgot about this. I became slightly engrossed in the rest of what I was working on once I solved this and it got away from me. Hope this can help someone. Commented Jun 11, 2012 at 3:18

1 Answer 1

2

SOLVED: Here is the solution I figured out what the problem was. When reading data back from the Java server, I was only reading one chunk and everything was hanging on the rest. The data needs to be read in a loop to get all of it.

Also The data I was sending to the Java Server was not ending in a new line or carriage return.

the following re-written PHP client now make the entire thing work nicely

$address = 'minecraft.kayoticgamer.com';
$port = 9005;

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $address, $port);

$message .= chr(10);

$status = socket_sendto($socket, $message, strlen($message), MSG_EOF, $address, $port);
if($status !== FALSE)
{
    $message = '';
    $next = '';
    while ($next = socket_read($socket, 4096))
    {
        $message .= $next;
    }

    echo $message;
}
else
{
    echo "Failed";
}

socket_close($socket);
Sign up to request clarification or add additional context in comments.

1 Comment

Hello KayoticSully : I try to run your example but it continuous loading is shown in browser please help me if you have done already.

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.