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 8079807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:13:38+00:00 2026-06-05T16:13:38+00:00

I am trying to set up a simple TCP connection on a single port

  • 0

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);
}
  • 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-06-05T16:13:40+00:00Added an answer on June 5, 2026 at 4:13 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set up a very simple UDP socket server with python that
I'm trying to set up a simple bash alias that will open a solution
I am trying to set up a simple single-view iOS5 iPhone application (with AppDelegates,
I'm trying to set up a simple server side RSA encryption of a small
I'm trying to set up a simple Websockets server with the Play! framework (1.2.4).
Trying to set up a simple login script in flex 4 with php. In
OK, so I am trying to set up a simple JSF application. I'm using
I am trying to create a simple client/server application using Thrift which is going
I'm trying to set up a simple animation that takes an image on the
I'm trying to set up a simple application for running multiple blogs and my

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.