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.

image

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

<?php
    function hex2ByteArray($hexString) {
		$string = hex2bin($hexString);
		return unpack('C*', $string);
	}
	$F=hex2ByteArray("FF");
	$E=hex2ByteArray("FE");
	$D=hex2ByteArray("FD");
	$C=hex2ByteArray("FC");

	$host="10.112.6.71";
	$port=11000;
	//$host="192.168.10.223";
    //$port=12000;
    $timeout=30;
	$var_str ="";
	$sulfix = array(255, 254, 253, 252);
	
	
	
	$Namafile="Test20220823a1";
	$participantCode="EL001";
	$sourceAccount="EL001000000160";
	$targetAccount="EL001000000261";
	$securityCodeType="LOCAL";
	$securityCode="abcd";
	$numberOfSecurities=100;
	$settlementDate="20220823";
	$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");
socket_close($socket);

?>

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.