1

Java code:

package servermonitor;

import java.io.*;
import java.net.*;

public class CommandListener extends Thread
{
    public int count = 0;
    public void run()
    {
            try
            {
                ServerSocket server = new ServerSocket(4444);
                while(true)
                {
                    System.out.println("listening");
                    Socket client = server.accept();
                    System.out.println("accepted");
                    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    System.out.println("got reader");
                    String data = "";
                    String line;
                    while((line = in.readLine()) != null)
                    {
                        System.out.println("inloop");
                        data = data + line;
                    }
                    System.out.println("RECIEVED DATA: " + data);
                    in.close();
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                    count++;
                    out.write("gotcha: " + count + "\\n");
                    out.flush();
                }

            }
            catch(IOException ex)
            {
                System.out.println(ex.getMessage());
            }

    }

 }

Java console (when I access the following PHP script):

listening
accepted
got reader

PHP code:

<?php
$PORT = 4444; //the port on which we are connecting to the "remote" machine
$HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)

$sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
or die("error: could not create socket\n");

$succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
or die("error: could not connect to host\n");

$text = "Hello, Java!\n"; //the text we want to send to the server

socket_write($sock, $text, strlen($text) + 1) //Writing the text to the socket
or die("error: failed to write to socket\n");

$reply = socket_read($sock, 10000) //Reading the reply from socket
or die("error: failed to read from socket\n");

echo $reply;
?>

When I navigate to the PHP page, it loads forever.

Any ideas?

1
  • are u sure it does not print inloop Commented Jul 12, 2011 at 5:20

2 Answers 2

4

The Java side expects a newline in its input. You're not sending one, so readLine never finishes.

Also, readLine won't return null until the socket is closed or an exception occurs (I/O error for instance). You need to return some data as soon as you've read a line if your protocol works like that.

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

2 Comments

Hmm, that was only a partial fix - I'm now stuck at the Java console saying "inloop" and the page still loads forever!
socket_send($sock,$text,strlen($text),0) //for just send information, without receiv it
0

As it was told, you need to close socket to readLine returns null.

Comments

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.