Current Location: Home> Latest Articles> How to Use pack() Function and socket_sendto() Together for Data Packing and Sending?

How to Use pack() Function and socket_sendto() Together for Data Packing and Sending?

M66 2025-06-23

In network programming, data transmission must strictly follow the protocol format to ensure that both the sender and the receiver can correctly parse the data. The pack() function in PHP helps us pack data into a binary string according to a specified format, while socket_sendto() is used to send data to a specific address via the UDP protocol. This article will explain how to use these two functions together to pack and send data.

1. Introduction to pack() Function

The pack() function converts PHP variables into a binary string according to a specified format code. For example:

$data = pack("Nn", 12345, 80);

Here, "Nn" represents:

  • N: Unsigned long integer (32-bit, network byte order, i.e., big-endian)

  • n: Unsigned short integer (16-bit, network byte order)

This packs the integers 12345 and 80 into a binary string, making it easier to transmit over the network.

2. Introduction to socket_sendto() Function

The socket_sendto() function is primarily used for sending data over UDP. The function signature is as follows:

int socket_sendto(resource $socket, string $buf, int $len, int $flags, string $addr, int $port)
  • $socket: The socket resource created using socket_create()

  • $buf: The data to be sent

  • $len: The length of the data to be sent

  • $flags: Flags, usually set to 0

  • $addr: The target IP address

  • $port: The target port

3. Example Code: Combining pack() and socket_sendto() to Send Data

Suppose we need to send a data packet containing a "message ID" and a "status code," where the message ID is a 4-byte integer and the status code is a 2-byte integer.

<?php
// Create UDP socket
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($socket === false) {
    die("socket_create() failed: " . socket_strerror(socket_last_error()));
}
<p>// Prepare data: message ID and status code<br>
$messageId = 1001;  // 4-byte integer<br>
$statusCode = 200;  // 2-byte integer</p>
<p>// Use pack() function to pack data, network byte order (big-endian)<br>
$data = pack("Nn", $messageId, $statusCode);</p>
<p>// Target address and port<br>
$ip = "m66.net";<br>
$port = 12345;</p>
<p>// Send data<br>
$sent = socket_sendto($socket, $data, strlen($data), 0, $ip, $port);<br>
if ($sent === false) {<br>
echo "socket_sendto() failed: " . socket_strerror(socket_last_error($socket));<br>
} else {<br>
echo "Successfully sent {$sent} bytes of data to {$ip}:{$port}\n";<br>
}</p>
<p>// Close socket<br>
socket_close($socket);<br>
?><br>

4. Conclusion

Through the pack() function, we can flexibly encode various types of data into a binary format that conforms to network protocol requirements. By combining it with socket_sendto(), we can send the packed data over UDP to a specific server, achieving efficient network communication.