PHPackages                             spatie/simple-tcp-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. spatie/simple-tcp-client

ActiveLibrary[HTTP &amp; Networking](/categories/http)

spatie/simple-tcp-client
========================

Connect and send data through a TCP connection

1.1.3(4mo ago)3910.3k↑2000%2MITPHPPHP ^8.4CI failing

Since Jul 2Pushed 1w agoCompare

[ Source](https://github.com/spatie/simple-tcp-client)[ Packagist](https://packagist.org/packages/spatie/simple-tcp-client)[ Docs](https://github.com/spatie/simple-tcp-client)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-simple-tcp-client/feed)WikiDiscussions main Synced yesterday

READMEChangelog (8)Dependencies (4)Versions (11)Used By (0)

🔌 Connect and send data through a TCP connection
================================================

[](#-connect-and-send-data-through-a-tcp-connection)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4bf1b2dd7f7eebc70c6082c7dd14ab42e2e4ea5215b7c4877ac87d3fb6ca3e2d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f73696d706c652d7463702d636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/simple-tcp-client)[![Tests](https://github.com/spatie/simple-tcp-client/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/simple-tcp-client/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/b4fdb8e904645ccbb827fcf5214b4a02af1940b9635fffce93945c626abb1fe1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f73696d706c652d7463702d636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/simple-tcp-client)

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

[](#support-us)

[![](https://camo.githubusercontent.com/26116b9cb402c5fb43bac8a3e53468b858dd59c6f66c248e7a95590c0e74d1a0/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f73696d706c652d7463702d636c69656e742e6a70673f743d31)](https://spatie.be/github-ad-click/simple-tcp-client)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/simple-tcp-client
```

Usage
-----

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance88

Actively maintained with recent releases

Popularity37

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 81.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~37 days

Recently: every ~53 days

Total

7

Last Release

144d ago

Major Versions

0.0.3 → 1.0.02025-07-13

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (60 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (9 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")

---

Tags

spatiesimple-tcp-client

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/spatie-simple-tcp-client/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-simple-tcp-client/health.svg)](https://phpackages.com/packages/spatie-simple-tcp-client)
```

###  Alternatives

[spatie/crawler

Crawl all internal links found on a website

2.8k18.5M66](/packages/spatie-crawler)[spatie/laravel-webhook-client

Receive webhooks in Laravel apps

1.2k13.2M128](/packages/spatie-laravel-webhook-client)[spatie/guzzle-rate-limiter-middleware

A rate limiter for Guzzle

1715.7M35](/packages/spatie-guzzle-rate-limiter-middleware)[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25126.1M82](/packages/php-http-cache-plugin)[illuminate/http

The Illuminate Http package.

11937.9M6.9k](/packages/illuminate-http)[rdkafka/rdkafka

A PHP extension for Kafka

2.2k24.3k1](/packages/rdkafka-rdkafka)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
