Reputation: 43
I'm trying to transfer simple message between PHP socket and JAVA socket. The php socket successfully sends the data and is waiting for Java servers response. But on the other hand Java server's socket is still waiting for the message from PHP.
Here is the Java Code:
ServerSocket s = new ServerSocket(4280);
Socket sock = s.accept();
System.out.println("Connected");
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
System.out.println("Reading");
String str = br.readLine();
System.out.println("Writing");
bw.write(str);
Output:
Connected
Reading
Here's the PHP code:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, "localhost", 4280);
socket_write($socket, "Hello");
echo socket_read($socket, 10);
socket_write($socket, "Lelo");
echo socket_read($socket, 10);
Output:
Browser: waiting for localhost
Upvotes: 0
Views: 646
Reputation: 25727
Two things that can usually cause a problem:
readLine()
method but your not sending a linefeed
and return in your PHP code.Code:
Adding linefeed:
socket_write($socket, "Hello\r\n");
Upvotes: 3
Reputation: 14853
String str = br.readLine();
expect a \n
which is not sent by the PHP program.
Add this :
socket_write($socket, "Hello\n" ); // <<<=== '\n' added
Upvotes: 1