Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Simple Tcp Client Laravel Package

spatie/simple-tcp-client

Simple TCP client for PHP/Laravel: connect to a host/port, send data, and receive responses with a clean API. Useful for interacting with TCP services (SMTP, HTTP, custom servers), testing network protocols, and building lightweight clients.

View on GitHub
Deep Wiki
Context7

🔌 Connect and send data through a TCP connection

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('smtp.gmail.com', 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('example.com', 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);

Handling timeouts

The client supports connection timeouts to prevent hanging:

use Spatie\SimpleTcpClient\TcpClient;
use Spatie\SimpleTcpClient\Exceptions\ConnectionTimeout;

$client = new TcpClient('slow-server.com', 80, 5_000); // 5000ms (5 second) timeout

try {
    $client->connect();
} catch (ConnectionTimeout $exception) {
    echo "Server took too long to respond: " . $exception->getMessage();
}

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.

use Spatie\SimpleTcpClient\TcpClient;

$client = new TcpClient('smtp.gmail.com', 587);

$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('8.8.8.8', 53);
    
    $client->connect();
    
    echo "Port 53 is open!";
    
    $client->close();
} catch (CouldNotConnect $exception) {
    echo "Port 53 is closed or filtered";
}

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport