Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3341296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:42:23+00:00 2026-05-18T00:42:23+00:00

I’m trying to make a communication between a PHP page and running Java server.

  • 0

I’m trying to make a communication between a PHP page and running Java server.
Just a simple string exchange through sockets.

This is my Java Code for thread that handles the connection:

            InputStream in = clientSocket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

                String request;

                if((request=br.readLine())!=null){
                System.out.println("got the following request: " + request);
                out.write(request +"\n");
                out.flush();

                out.close();
                in.close();
                }

I tested it with simple Java client that sends the string then receives the result and prints it back and it worked. Both client and the server had the same output.

This is my PHP code:

$fp = @fsockopen ($host, $port, $errno, $errstr);

if($fp){
    fputs($fp, $str);
    //echo fgets($fp);
}

close($fp);

Which sends the string to the Server, and the Server receives it.
But once I uncomment the line with fgets($fp) I am blocked until some sort of timeout happens after 1-2 minutes. During that Block my server is not receiving anything.
After timeout my server prints, that it has received the line, and probably sends the response back, however, the PHP code doesn’t print anything.

What could be the problem?

Thank you in advance.

P.S. It is probably worth saying that I am accessing this web page through AJAX, so it “echoes” the result back to the other page.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:42:24+00:00Added an answer on May 18, 2026 at 12:42 am

    I prefer the socket_* functions, personally. But either way, you’re likely missing checking for a terminating character:

    $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    socket_connect($sock, $host, $port);
    
    socket_write($sock, $str);
    
    $response = '';
    while($resp = socket_read($sock, 1024)) {
        if(!$resp)
            break;
        $response .= $resp;
        if(strpos($resp, "\n") !== false)
            break;
    }
    
    echo "Server said: {$response}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.