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. spatie/simple-tcp-client

ActiveLibrary

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

Connect and send data through a TCP connection

1.1.3(3mo ago)387.3k↑1550%2[1 PRs](https://github.com/spatie/simple-tcp-client/pulls)MITPHPPHP ^8.4CI passing

Since Jul 2Pushed 3mo 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 1mo ago

READMEChangelog (8)Dependencies (4)Versions (10)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://camo.githubusercontent.com/cfa004f5873a3fcc86468ab9986092da6edc738e33abc2e685e887a373c9cb92/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f73696d706c652d7463702d636c69656e742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](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

51

—

FairBetter than 96% of packages

Maintenance81

Actively maintained with recent releases

Popularity36

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 85.3% 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

98d 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 (58 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (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/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[spatie/laravel-activitylog

A very simple activity logger to monitor the users of your website or application

5.8k45.4M309](/packages/spatie-laravel-activitylog)[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k37.7M472](/packages/spatie-laravel-medialibrary)[spatie/laravel-backup

A Laravel package to backup your application

6.0k21.8M191](/packages/spatie-laravel-backup)[spatie/image-optimizer

Easily optimize images using PHP

2.9k71.3M109](/packages/spatie-image-optimizer)[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.4k26.9M220](/packages/spatie-laravel-query-builder)

PHPackages © 2026

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