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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:05:46+00:00 2026-06-02T09:05:46+00:00

I have recently started practicing with sockets on PHP and got an issue for

  • 0

I have recently started practicing with sockets on PHP and got an issue for which I find no documentation. Similar cases I’ve seen in C++, but not a clear answer to this. The code:

do {
    $input = socket_read($client, 12,PHP_BINARY_READ);
    echo $input;
} while(TRUE);

Is supposed to block on the socket (code for creation, bind, etc not included) and get either 12 bytes or whatever information is available from the other side.

Oddly I just get a ‘1’ in the variable $input if 12 bytes are read. If I send from the client side more than 12 bytes then I receive ‘1[REST_OF_DATA]’ in the value of $input.

Any idea why this is happening?

If I change this to more data and to PHP_NORMAL_READ then I correctly receive the data.

PHP manual online does not say anything abou socket_read returning ‘1’..

**EDIT: Ok thanks for yout early answers :). I am saving to a file and reading (not echoing to browser) expecting any character. I think I may have just discovered something that could be good if someone with knowledge of C++ sockets can verify. Anyways, my read code actually was this (not what I posted above):

do {
    $input = ($seq_id == 0) ? socket_read($client, 12,PHP_BINARY_READ) : socket_read($client,1024,PHP_BINARY_READ);
    echo $input;
} while(TRUE);

I was expecting 12 bytes at the first read, then chunks of 1024, reason for that condition check. The weird ‘1’ comes from this. If I replace that with the line I posted above the data is read normally. In fact, even reading like this:

$input = ($seq_ID == 0) ? socket_read($client,12,PHP_BINARY_READ) : socket_read($client, 12,PHP_BINARY_READ);

Results in : 1st read = ‘1’ 2nd read = correct data, 3rd read = correct data..

  • 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-02T09:05:49+00:00Added an answer on June 2, 2026 at 9:05 am

    For the comments above, yes $seq_id should increment. I just wanted to shorten the code. So now, the answer is not important for me anymore but remains an enigma, after upgrading my Ubuntu version this month I have been unable to replicate the error with the same script:

    <?php
    set_time_limit(0);
    $address = "192.168.1.1";
    $port = 3320;
    $server_users = 3; 
    $mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
    $bind_result = socket_bind($mysock,$address, $port) or die("Could not bind to address\n"); 
    $listen_result = socket_listen($mysock, $server_users)  or die("Could not set up socket   listener\n");
    $client = socket_accept($mysock) or die("Could not accept the connection to socket\n");
    
    $seq_id =0;
    do {
    $input = ($seq_id == 0) ? socket_read($client, 12,PHP_BINARY_READ) : socket_read($client,1024,PHP_BINARY_READ);
    echo $input;
    } while(TRUE);
    ?>
    

    I execute it in terminal:

    $php -q myscript.php
    

    And test it using netcat:

    $netcat 192.168.1.1 3320
    

    Note that the question is about socket_read returning 1 as it results, which is not documented anywhere

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started using Zend Studio which has reported as warning the following
I have recently started learning C++, but I require a compiler. I have tried
I have recently started using the Rackspace Cloudfiles CDN (Limelight), about which I have
I have recently started to code heavily AJAX supported scripts in PHP, thing is,
I have recently started exploring Maven, but I feel a bit overwhelmed of all
I have recently started playing around with iOS development and have got most of
I have recently started using CAML.NET IntelliSense for SharePoint with Visual Studio 2008; which
I have recently started working on some open source project which I found relevant
I have recently started programming in Perl (I'll skip the long story) but I've
I have recently started using CI and with it CI sessions, but I have

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.