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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:53:57+00:00 2026-05-31T10:53:57+00:00

I want to send JSON messages from a PHP script to a C# app

  • 0

I want to send JSON messages from a PHP script to a C# app over a network connection using PHP Sockets.

Usually, for binary protocols, the first 4 bytes of every message must be an integer which represents the length (how many bytes) of the message.

In C# I prefix every message with an integer that tells the length of the message as follow:

byte[] msgBytes = UTF8Encoding.UTF8.GetBytes("A JSON msg");            
byte[] prefixBytes = BitConverter.GetBytes(msgBytes.Length);
byte[] msgToSend = new byte[prefixBytes.Length + msgBytes.Length];
Buffer.BlockCopy(prefixBytes, 0, msgToSend, 0, prefixBytes.Length);
Buffer.BlockCopy(msgBytes, 0, msgToSend, prefixBytes.Length, msgBytes.Length);

As I understand, in PHP the function socket_send only accept strings. So, how can I do the same prefixing in PHP 5.x?

Update: I posted a follow-up question on how to process such prefixed data when received from a network 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-31T10:53:59+00:00Added an answer on May 31, 2026 at 10:53 am

    In PHP strings are binary.

    So you need to encode the integer length value as the binary representation of an unsigned integer as a 4-char (4 Octets; 32 bits) string. See pack:

    # choose the right format according to your byte-order needs:
    
    l   signed long (always 32 bit, machine byte order)
    L   unsigned long (always 32 bit, machine byte order)
    N   unsigned long (always 32 bit, big endian byte order)
    V   unsigned long (always 32 bit, little endian byte order)
    
    $string = pack('l', $length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send a JSON object from the client to an action on
i want to send json to server using Spring 3.x, i use annotation @RequestBody,
i want to send a javascript array to php using jquery ajax. $.post(controllers/ajaxcalls/users.php, {
I want to check the app version from apple so I send request like
If I want to send an array from PHP to JavaScript, I do something
Hello Sir i want send list of data to php server i use following
Want to send the text from my current vb application to the Active Window
I want to send email from a Ruby application. Is there a call in
I want to send json service when the user search text on searchbar. Here
i want to send JSON with POST/PUT i don't know if it's the same

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.