How to Send byte with suffix in socket

Allready solved , get solution from DroopSnoot, $sulfix = sprintf(“%c%c%c%c”, 255, 254, 253, 252);

I have a task, to send a message with ending sulfix. If the message like $Message=“abcdef”
how to write code with php, thx before

Suffix in hexadecimal format is : {FF, FE, FD, FC}
The message structure can be viewed at the message format section.
Suffix is a byte array delivered after the instruction message was sent. The format of suffix delivered
should follow the below format.


Where are you stuck? How are you sending the message?

this is my code and i try to sending but not get reply. when i ask the vendor, he said, he did not get the message

    function hex2ByteArray($hexString) {
		$string = hex2bin($hexString);
		return unpack('C*', $string);

	$var_str ="";
	$sulfix = array(255, 254, 253, 252);
	$description="Test Cirt 20220812";
	$var_str = '<Message name="SecuritiesTransfer" type="OutgoingMessage">'."\n";
	$var_str = $var_str.'<Record name="data">'."\n";
	$var_str = $var_str.'<Field name="externalReference">'.($Namafile).'</Field>'."\n";
	$var_str = $var_str.'<Field name="participantCode">'.($participantCode).'</Field>'."\n";
	$var_str = $var_str.'<Field name="sourceAccount">'.($sourceAccount).'</Field>'."\n";
	$var_str = $var_str.'<Field name="targetAccount">'.($targetAccount).'</Field>'."\n";
	$var_str = $var_str.'<Field name="securityCodeType">'.($securityCodeType).'</Field>'."\n";
	$var_str = $var_str.'<Field name="securityCode">'.($securityCode).'</Field>'."\n";	
	$var_str = $var_str.'<Field name="numberOfSecurities">'.($numberOfSecurities).'</Field>'."\n";
	$var_str = $var_str.'<Field name="settlementDate">'.($settlementDate).'</Field>'."\n";
	$var_str = $var_str.'<Field name="description">'.($description).'</Field>'."\n";
	$var_str = $var_str.'</Record>'."\n";
	$var_str = $var_str.'</Message>'.' '.$F.$E.$D.$C;
	$message	= $var_str;
	echo "\n"."Message To server :"."\n".$message."\n";
	echo "\n"."Socket Sender to ip# ".$host. " using port# ".$port ."\n";

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");


What is in your hex2bytearray() function? I can’t find anything to suggest that is a standard part of PHP, and I wondered why you could convert each individual byte to a byte array.

Can’t you use sprintf() to add the characters to the end? Something like

$var_str .= sprintf("%c%c%c%c", 255, 254, 253, 252);

I’m not sure it works with non-printable characters.

Your vendor should have instructions on how to interact with their service, if they do not, you need to ask clarifying questions. From the look in the first post, this is for an ongoing transport system, i.e. message, message, message etc.

For example, the first post explains a “message delivered” sequence/code.
In the post above, you add this directly to the end of your message.

Have you tried sending the message delivered code, after you sent the actual message, in a new socket_write command? I would also confirm if the message should be encoded before sending as well.

I am also not sure if your format of the message delivered code is correct.

Your next step should be as mentioned above, getting the instructions from the provider, or asking questions to clarify how each part should be delivered.

got it , you fix my code … big thx

1 Like

Fundamentally, the interpretation of the material exchanged between the two sides of the connection must be agreed upon. It is a protocol to do this.

The first two bytes could be interpreted as an integer, as indicated. Ensure that both parties can agree on which byte is high and which is low (the endianness of the number). This method also stops you from sending continuous strings that are greater than 65k bytes. It probably won’t be an issue, but it’s something to be aware of.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.