Current Location: Home> Latest Articles> How to use the socket_addrinfo_connect function in PHP with the socket_write function to send data? Examples included.

How to use the socket_addrinfo_connect function in PHP with the socket_write function to send data? Examples included.

gitbox 2025-06-12

In PHP, when performing network programming, the socket_addrinfo_connect function is used together with the socket_write function to send data. The socket_addrinfo_connect function connects to the target server through the address information structure (addrinfo), and socket_write writes data to the established socket. Below is an example demonstrating how to use these functions for sending HTTP GET requests over a socket connection.


Overview of Functions

  • socket_addrinfo_connect
    Used to connect to the target server via address information structure (addrinfo) after resolving the domain using socket_addrinfo_lookup. It supports both IPv4 and IPv6 protocols.

  • socket_write
    Used to write data to the connected socket after the connection is established, allowing the transmission of various types of data like HTTP requests.


Steps for Sending Data

  1. Resolve the domain using socket_addrinfo_lookup to get address information (addrinfo).

  2. Use socket_addrinfo_connect to establish a connection using the address information, resulting in an active socket connection.

  3. Use socket_write to send data over the socket.

  4. Read the response from the server if necessary.


Example Code

<?php
// 1. Resolve the domain using socket_addrinfo_lookup
$host = "m66.net"; // The server address to connect to
$port = 80; // The port to use for the connection
<p>$addrinfo = socket_addrinfo_lookup($host, $port, AF_UNSPEC, SOCK_STREAM, SOL_TCP);</p>
<p>if ($addrinfo === false) {<br>
die("Failed to resolve address: " . socket_strerror(socket_last_error()));<br>
}</p>
<p>// 2. Connect using socket_addrinfo_connect<br>
$socket = socket_addrinfo_connect($addrinfo[0]);</p>
<p>if ($socket === false) {<br>
die("Failed to connect: " . socket_strerror(socket_last_error()));<br>
}</p>
<p>// 3. Send an HTTP GET request<br>
$request = "GET / HTTP/1.1\r\n";<br>
$request .= "Host: $host\r\n";<br>
$request .= "Connection: close\r\n\r\n";</p>
<p>$sentBytes = socket_write($socket, $request, strlen($request));</p>
<p>if ($sentBytes === false) {<br>
die("Failed to send data: " . socket_strerror(socket_last_error($socket)));<br>
}</p>
<p>echo "Sent $sentBytes bytes of data.\n";</p>
<p>// 4. Read the response<br>
$response = "";<br>
while ($out = socket_read($socket, 2048)) {<br>
$response .= $out;<br>
}</p>
<p>echo "Received response:\n";<br>
echo $response;</p>
<p>// 5. Close the socket connection<br>
socket_close($socket);<br>
?>


Summary

  • The example demonstrates how to connect to m66.net, send an HTTP GET request, and read the server's response.

  • This guide shows how to perform network communication using TCP sockets and send an HTTP GET request over the established connection.

  • socket_addrinfo_lookup resolves the domain name, and socket_addrinfo_connect handles the connection setup for both IPv4 and IPv6 protocols.

  • Handling errors properly with socket_strerror and socket_last_error is essential for debugging socket communication.

  • Support for AF_UNSPEC allows compatibility with both IPv4 and IPv6 addresses.