承接 spatie/simple-tcp-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spatie/simple-tcp-client

最新稳定版本:1.1.2

Composer 安装命令:

composer require spatie/simple-tcp-client

包简介

Connect and send data through a TCP connection

README 文档

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('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.

统计信息

  • 总下载量: 4.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 36
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固