spatie/simple-tcp-client

Connect and send data through a TCP connection

0.0.2 2025-07-02 21:53 UTC

This package is auto-updated.

Last update: 2025-07-05 20:04:44 UTC


README

Latest Version on Packagist Tests Total Downloads

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();

Support us

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.

Installation

You can install the package via composer:

composer require spatie/simple-tcp-client

Usage

Here's how you can connect to TCP service:

use Spatie\SimpleTcpClient\TcpClient;

$client = new TcpClient(host: 'example.com', port: 80);

$client->connect();

Sending and Receiving Data

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);

HTTP Requests over TCP

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();

Testing SMTP Connections

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();

Port Scanning

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";
}

Handling timeouts

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();
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.