Skip to content
Advertisement

Java Socket Server with PHP client

the question is totally rewritten since I have understood that previously it was really unclear. I have created a Java Socket server with threads to accept multiple connection in order to handle php tcp requests. The java server just for the testing purposes it reverse the string supplied from php. Java server is hosted on a ubuntu server and the php is located on another machine. The java server shows that php client is connected, but the php is not loading and the string is not sent. From the codes given below what could be the mistake?

UPDATE the problem is the received string from the Java server. I have checked with debugger and the BufferedReader is full of ‘u0000’ and server stops responding. The rest code and communication is working perfect. How I can avoid those null characters or decode the string correct?

ReverseServer

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

public class ReverseServer {

public static void main(String[] args) {

    int port = 10007;

    try (ServerSocket serverSocket = new ServerSocket(port)) {

        System.out.println("Server is listening on port " + port);

        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("New client connected");

            new ServerThread(socket).start();
        }

    } catch (IOException ex) {
        System.out.println("Server exception: " + ex.getMessage());
        ex.printStackTrace();
    }
}
}

ServerThread

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

public class ServerThread extends Thread {
private Socket socket;

public ServerThread(Socket socket) {
    this.socket = socket;
}

public void run() {
    try {
        InputStream input = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        OutputStream output = socket.getOutputStream();
        PrintWriter writer = new PrintWriter(output, true);


        String text;

        do {
            text = reader.readLine();
            String reverseText = new StringBuilder(text).reverse().toString();
            writer.println("Server: " + reverseText);

        } while (!text.equals("bye"));

        socket.close();
    } catch (IOException ex) {
        System.out.println("Server exception: " + ex.getMessage());
        ex.printStackTrace();
    }
}
}

PHP client

<?php

// websocket connection test
$host    = "ip_of_server";
$port    = 10007;
$message = "Hello";
echo "Message To server :".$message;
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('TCP'));
$result = socket_connect($socket, $host, $port);

if ($result) {
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to servern");
// get server response
$result = socket_read($socket, 1024) or die("Could not read server responsen");
echo "Reply From Server  :" . $result;
}

socket_close($socket);

Advertisement

Answer

I am trying to read a line, but on the string given on the php client didn’t had the carriage return symbol “r”. Once I have put this on the string it works as expected.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement