This package provides a simple and elegant way to create TCP connections, send data, and receive responses. Perfect for interacting with TCP servers, testing network services, or building simple network clients.
use Spatie\SimpleTcpClient\TcpClient;
$client = new TcpClient(host: 'smtp.gmail.com', port: 587);
$client->connect();
$greeting = $client->receive();
echo $greeting; // 220 smtp.gmail.com ESMTP...
$client->send("EHLO test.local\r\n");
$response = $client->receive();
echo $response; // 250-smtp.gmail.com capabilities...
$client->close();
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/simple-tcp-client
Here's how you can connect to TCP service:
use Spatie\SimpleTcpClient\TcpClient;
$client = new TcpClient(host: 'example.com', port: 80);
$client->connect();
You can use send
and receive
methods to send and receive data.
$client->send("Hello, server!");
$response = $client->receive(); // Read response from server
echo $response;
By default, we'll read 8192 bytes of data. You can optionally specify the maximum number of bytes to read:
$response = $client->receive(1024);
Here's a full example where we use all methods.
$client = new TcpClient('httpbin.org', 80);
$client->connect();
$request = "GET /get HTTP/1.1\r\n";
$request .= "Host: httpbin.org\r\n";
$request .= "Connection: close\r\n\r\n";
$client->send($request);
$response = $client->receive();
echo $response;
$client->close();
Here's how you could connect and communicate with an SMTP server.
$client = new TcpClient('smtp.gmail.com', 587, 15);
$client->connect();
// Read the greeting
$greeting = $client->receive();
echo $greeting; // 220 smtp.gmail.com ESMTP...
// Send EHLO command
$client->send("EHLO test.local\r\n");
$response = $client->receive();
echo $response; // 250-smtp.gmail.com capabilities...
$client->close();
In this example, we are going to check if port 53 is open or closed.
use Spatie\SimpleTcpClient\TcpClient;
use Spatie\SimpleTcpClient\Exceptions\CouldNotConnect;
try {
$client = new TcpClient(host: '8.8.8.8', port: 53, timeout: 5);
$client->connect();
echo "Port 53 is open!";
$client->close();
} catch (CouldNotConnect $exception) {
echo "Port 53 is closed or filtered";
}
The client supports connection timeouts to prevent hanging:
use Spatie\SimpleTcpClient\TcpClient;
use Spatie\SimpleTcpClient\Exceptions\ConnectionTimeout;
$client = new TcpClient(host: 'slow-server.com', port: 80, timeout: 5); // 5 second timeout
try {
$client->connect();
} catch (ConnectionTimeout $exception) {
echo "Server took too long to respond: " . $exception->getMessage();
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.