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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:00:51+00:00 2026-06-03T07:00:51+00:00

I have some a java server that I’m trying to get to play with

  • 0

I have some a java server that I’m trying to get to play with a php script.

The format provides the number of bytes the message will take as an unsigned byte, and then the bytes that comprise the string.

here’s my function with commentary

function read($socket, $readnum) {
    $endl = "<br>"; // It's running on apache

    // attempt to get the byte for the size
    $len = unpack("C",socket_read($socket, 1))[0];
    // this prints "after ", but $len does not print. $endl does
    echo "after " . $len . $endl;
    // obviously this fails because $len isn't in a valid state
    $msg = socket_read($socket, $len);
    // print the message for debugging?
    echo $len . " " . $msg;
    return $msg;
}

I’m not really a php guy, more of a Java guy, so I’m not sure how I should go about getting this length.

On @c0le2’s request, I made the following edit

$lenarr = unpack("C",socket_read($socket, 1));
$len = $lenarr[0]; // line 81

Which gave the following error

PHP Notice:  Undefined offset: 0 in simple_connect.php on line 81
  • 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-03T07:00:53+00:00Added an answer on June 3, 2026 at 7:00 am

    The unpack format string is actually like code [repeater] [name], separated by forward slashes. For example, Clength. The output array will be associative and keyed by name, with repeated fields having a numeric suffix appended, starting at 1. E.g. the output keys for C2length will be length1 and length2.

    The documentation is not super-clear about this.

    So when you don’t specify any name, it just appends the numeric suffix, starting at 1. So the length you are looking for is $lenarr[1].

    But you should try this instead:

    $v = unpack('Clength', "\x04");
    var_export($v);  // array('length' => 4)
    

    Here are some other examples:

    unpack('C2length', "\x04\x03");
    // array ( 'length1' => 4, 'length2' => 3, );
    
    unpack('Clength/Cwidth', "\x04\x03");
    // array ( 'length' => 4, 'width' => 3, );
    

    Also, in php you can’t generally use array-access notation on an expression–you need to use it on the variable directly. So functioncall()[0] won’t work, you need $v = functioncall(); $v[0]. This is a php wart.

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

Sidebar

Related Questions

I have a java server that sends some data through a socket to a
I have some Java projects that use Apache Commons Configuration and they get deployed
I have some Java projects that use Apache Commons Configuration and they get deployed
I've have some java code that starts a web server and listens for requests.
I have some Java code that is throwing out of memory exceptions after running
I have some Java code that connects to an Oracle database using DriverManager.getConnection(). It
I currently have a Java server that talks to a Flash client by passing
Suppose I have some PHP code running inside a web server, for example, running
I have several hand-written Java classes in a REST Server that uses JAXB to
I have a java socket server that sends an Animal object to the Flash

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.