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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:30:38+00:00 2026-05-18T20:30:38+00:00

When I run the two files below through the command line, (first start socket_server,

  • 0

When I run the two files below through the command line, (first start socket_server, then socket_client) there is a long delay (~60s) before any output is sent to socket_client by the server. Is there a way to reduce this gap, or any hints as to what is causing the problem? Here are my two code snippets:

socket_client.php:

<?php
$fp = stream_socket_client("tcp://127.0.0.1:8000", $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
}
else {
       fwrite($fp, "2");
       echo fgets($fp, 1024);
    }
fclose($fp);
?>

socket_server.php:

<?php
$socket = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)<br />\n";
} else {
  while ($conn = stream_socket_accept($socket)) {
    while (!feof($conn)) {
          $result = fgets($conn, 1024);
        if($result = "2"){
                   fwrite($conn, "Hullo there");
                   }
        else{
            fwrite($conn, "Hullo here\n");
        }
    }
    fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
    fclose($conn);
  }
  fclose($socket);
}
?>
  • 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-18T20:30:39+00:00Added an answer on May 18, 2026 at 8:30 pm
    • You were forgetting to send \n at the end of some of the fwrite calls. The reason this was causing a problem is because fgets is looking for the newline before it returns.
    • I removed the feof loop from the server because the client is only sending one line.
    • I added the feof loop in the client to handle the multiple lines sent from the server.
    • I changed if($result = into if ($result == because == is a comparison operator (which is what you actually wanted). Inside an if statement you almost always want to use == instead of =.

    socket_client.php:

    <?php
    
    $fp = stream_socket_client("tcp://127.0.0.1:8000", $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        fwrite($fp, "2\n");
        while (!feof($fp)) {
            echo fgets($fp, 1024);
        }
    }
    fclose($fp);
    
    ?>
    

    socket_server.php:

    <?php
    
    $socket = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errstr);
    if (!$socket) {
        echo "$errstr ($errno)<br />\n";
    } else {
        while ($conn = stream_socket_accept($socket)) {
            $result = fgets($conn, 1024);
            if ($result == "2\n") {
                fwrite($conn, "Hullo there\n");
            } else {
                fwrite($conn, "Hullo here\n");
            }
            fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
            fclose($conn);
        }
        fclose($socket);
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the two files below through the command line, (first start socket_server,
There are a number of run-time differences in compatible code between these two versions
I'm using Python 2.6.1 on Mac OS X. I have two simple Python files (below), but
I've run into a problem where I'm getting two printouts of my /etc/motd file
We have a process that needs to run every two hours. It's a process
I have two insert statements, almost exactly the same, which run in two different
I have two scripts that often need to be run with the same parameter:
How do I have two effects in jQuery run in sequence , not simultaneously?
It may sound stupid, but sometimes I run into version conflicts between two versions
For some reason, when using two sums on a group by, i run into

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.