All Projects → hoaproject → Socket

hoaproject / Socket

Licence: other
The Hoa\Socket library.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to Socket

gnb udp over tcp
gnb_udp_over_tcp 是一个为GNB开发的通过tcp链路中转UDP分组转发的服务
Stars: ✭ 32 (-47.54%)
Mutual labels:  socket, tcp, udp
Socket
Non-blocking socket and TLS functionality for PHP based on Amp.
Stars: ✭ 122 (+100%)
Mutual labels:  socket, tcp, udp
T Io
解决其它网络框架没有解决的用户痛点,让天下没有难开发的网络程序
Stars: ✭ 1,331 (+2081.97%)
Mutual labels:  socket, tcp, udp
Elixir Socket
Socket wrapping for Elixir.
Stars: ✭ 642 (+952.46%)
Mutual labels:  socket, tcp, udp
Pypacker
📦 The fastest and simplest packet manipulation lib for Python
Stars: ✭ 216 (+254.1%)
Mutual labels:  socket, tcp, udp
Godsharp.socket
An easy-to-use .NET socket server and client.
Stars: ✭ 35 (-42.62%)
Mutual labels:  socket, tcp, udp
Go Netstat
A netstat implementation written in Go
Stars: ✭ 121 (+98.36%)
Mutual labels:  socket, tcp, udp
ddos
Simple dos attack utility
Stars: ✭ 36 (-40.98%)
Mutual labels:  socket, tcp, udp
Ohsce
PHP HI-REL SOCKET TCP/UDP/ICMP/Serial .高可靠性PHP通信&控制框架SOCKET-TCP/UDP/ICMP/硬件Serial-RS232/RS422/RS485 AND MORE!
Stars: ✭ 206 (+237.7%)
Mutual labels:  socket, tcp, udp
Kalm.js
The socket manager
Stars: ✭ 155 (+154.1%)
Mutual labels:  socket, tcp, udp
Yasio
A multi-platform support c++11 library with focus on asio (asynchronous socket I/O) for any client application.
Stars: ✭ 483 (+691.8%)
Mutual labels:  socket, tcp, udp
KingNetwork
KingNetwork is an open source library to facilitate the creation and communication of clients and servers via TCP, UDP, WebSocket and RUDP sockets.
Stars: ✭ 78 (+27.87%)
Mutual labels:  socket, tcp, udp
Hp Socket
High Performance TCP/UDP/HTTP Communication Component
Stars: ✭ 4,420 (+7145.9%)
Mutual labels:  socket, tcp, udp
AndroidNetMonitor
This project aims to collect and analyze traffic information of Android.(采集手机发送和接收的报文简要信息,并且根据socket记录每个报文对应哪个手机app)
Stars: ✭ 25 (-59.02%)
Mutual labels:  socket, tcp, udp
Netcat
💻 Netcat client and server modules written in pure Javascript for Node.js.
Stars: ✭ 315 (+416.39%)
Mutual labels:  socket, tcp, udp
Goproxy
🔥 Proxy is a high performance HTTP(S) proxies, SOCKS5 proxies,WEBSOCKET, TCP, UDP proxy server implemented by golang. Now, it supports chain-style proxies,nat forwarding in different lan,TCP/UDP port forwarding, SSH forwarding.Proxy是golang实现的高性能http,https,websocket,tcp,socks5代理服务器,支持内网穿透,链式代理,通讯加密,智能HTTP,SOCKS5代理,黑白名单,限速,限流量,限连接数,跨平台,KCP支持,认证API。
Stars: ✭ 11,334 (+18480.33%)
Mutual labels:  socket, tcp, udp
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (+9.84%)
Mutual labels:  socket, tcp, udp
DatagramTunneler
Simple C++ cross-platform client/server app forwarding UDP datagrams through a TCP connection.
Stars: ✭ 116 (+90.16%)
Mutual labels:  socket, tcp, udp
Async Sockets Cpp
Simple thread-based asynchronous TCP & UDP Socket classes in C++.
Stars: ✭ 127 (+108.2%)
Mutual labels:  socket, tcp, udp
Ssokit Qmake
A Simple & Strong Tool for TCP&UDP Debug
Stars: ✭ 231 (+278.69%)
Mutual labels:  socket, tcp, udp

Hoa


Build status Code coverage Packagist License

Hoa is a modular, extensible and structured set of PHP libraries.
Moreover, Hoa aims at being a bridge between industrial and research worlds.

Hoa\Socket

Help on IRC Help on Gitter Documentation Board

This library provides an abstract layer to build safe, fast and modular clients and servers.

It represents a connection as a stream (please, see the Hoa\Stream library) that is used to build clients and servers. A connection supports timeout, options, context, encryption, remote informations etc. Such a connection, along with an abstract connection handler, allows to embed and “merge” many connections inside the same processus side-by-side.

Learn more.

Installation

With Composer, to include this library into your dependencies, you need to require hoa/socket:

$ composer require hoa/socket '~1.0'

For more installation procedures, please read the Source page.

Testing

Before running the test suites, the development dependencies must be installed:

$ composer install

Then, to run all the test suites:

$ vendor/bin/hoa test:run

For more information, please read the contributor guide.

Quick usage

As a quick overview, we will look at creating a server and a client, and introduce the respective API.

A connection behind

Both server and client extend a connection, namely the Hoa\Socket\Connection\Connection class, which is a stream represented by the Hoa\Stream library. This latter provides the common stream API whose the read and write methods (from Hoa\Stream\IStream\In, Hoa\Stream\IStream\Out and also Hoa\Stream\IStream\Pathable). Since it is also responsible of the connection, we are able to manipulate the underlying socket resource, the timeout, the different flags, the stream context, the encryption, the remote informations etc.

To start a connection, we will use the connect method (the constructor does not start the connection by itself). For a server, we will often prefer to use the connectAndWait method (see bellow). To stop a connection, most of the time, we will use the disconnect method.

A remote connection (a client for the server, a server for the client) is represented by a node: an object that holds several informations about the remote connection. The default node is Hoa\Socket\Node and can be easily extended. To use a new node, we have to call the Hoa\Socket\Connection\Connection::setNodeName method.

A connection needs a socket URI, represented by the Hoa\Socket\Socket class, to know where to connect. This latter represents an IPv4 or IPv6 address, a domain or a path (for Unix socket), along with the transport scheme (tcp://, udp:// etc.) and the port.

Manipulating a server or a client

We will instanciate the Hoa\Socket\Server class and start a connection to tcp://127.0.0.1:4242. Then, to select active nodes, we will use the Hoa\Socket\Connection\Connection::select method that returns an iterator. Finally, we will read a line and write an uppercassed echo. Thus:

$server = new Hoa\Socket\Server('tcp://127.0.0.1:4242');
$server->connectAndWait();

while (true) {
    foreach ($server->select() as $node) {
        $line = $server->readLine();

        if (empty($line)) {
            $server->disconnect();
            continue;
        }

        echo '< ', $line, "\n";
        $server->writeLine(strtoupper($line));
    }
}

And then, with telnet:

$ telnet 127.0.0.1 4242
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
foobar
FOOBAR
hello world
HELLO WORLD

From the server, we will see:

< foobar
< hello world

To reproduce the same behavior with our own client, we will write (thanks to Hoa\Console\Readline\Readline, please see the Hoa\Console library):

$client = new Hoa\Socket\Client('tcp://127.0.0.1:4242');
$client->connect();

$readline = new Hoa\Console\Readline\Readline();

while (true) {
    $line = $readline->readLine('> ');

    if ('quit' === $line) {
        break;
    }

    $client->writeLine($line);

    echo '< ', $client->readLine(), "\n";
}

Finally:

$ php Client.php
> foobar
< FOOBAR
> hello world
< HELLO WORLD
> quit

Handle servers and clients

A connection has advanced operations but they are low-levels and not obvious. Moreover, there is repetitive and not so trivial tasks that we need often, such as broadcasting messages. The Hoa\Socket\Connection\Handler provides an easy way to create and embed a very flexible server or client. (A good and complete example is the Hoa\Websocket library).

We will focus on a server. A server has the magic run method that starts an infinite loop and make some computation on active nodes. This is basically the while (true) in our previous examples. In addition, we would like to easily send a message to a specific node, or send a message to all nodes except one. The Hoa\Socket\Connection\Handler class asks the user to implement only two methods: _run and _send, and provides the run method, along with send and broadcast. Then, we no longer need to start the connection or to take care about the implementation of different network topologies. All is managed by the handler. Thus:

class MyServer extends Hoa\Socket\Connection\Handler
{
    protected function _run (Hoa\Socket\Node $node)
    {
        $connection = $node->getConnection();
        $line       = $connection->readLine();

        if (empty($line)) {
            $connection->disconnect();
            return;
        }

        echo '< ', $line, "\n";
        $this->send(strtoupper($line));

        return;
    }

    protected function _send ($message, Hoa\Socket\Node $node)
    {
        return $node->getConnection()->writeLine($message);
    }
}

And then, all we need to do is:

$server = new MyServer(new Hoa\Socket\Server('tcp://127.0.0.1:4242'));
$server->run();

We see that the connection is embeded inside our server, and that all the logic has been moved inside the _run method. If we change the call to send by broadcast, we will see all connected clients receiving the message, something like:

        echo '< ', $line, "\n";
        $this->broadcast(strtoupper($line));

The _send method gives an implementation of “sending one message”, which is the basis. Because the _run method does not start an infinite loop, we have more flexibility (see the next section).

We can add listeners (please see the Hoa\Event library) to interact with the server, something like $server->on('message', function ( … ) { … }); etc.

Merging connections

Another huge advantage of using handlers is that they can be used inside a Hoa\Socket\Connection\Group object. The run method is an infinite loop, so we are not able to run two servers side-by-side in the same process. Fortunately, the Hoa\Socket\Connection\Group allows to “merge” connections (this is an underlying feature of Hoa\Socket\Connection\Connection but a group abstracts and manages all the complexity). Consequently, we are able to run several servers and clients together, inside the same processus, at the same time.

For example, we will run an instance of Hoa\Irc\Client (please, see the Hoa\Irc library) with a Hoa\Websocket\Server (please, see the Hoa\Websocket library: all messages received by the WebSocket server will be redirected on the IRC client. Thus:

$websocket = new Hoa\Websocket\Server(new Hoa\Socket\Server('tcp://…'));
$irc       = new Hoa\Irc\Client(new Hoa\Socket\Client('tcp://…'));
$group     = new Hoa\Socket\Connection\Group();
$group[]   = $websocket;
$group[]   = $irc;

$websocket->on(
    'message',
    function (Hoa\Event\Bucket $bucket) use ($irc) {
        $data = $bucket->getData();
        $irc->say($data['message']);

        return;
    }
);

// $irc->…

$group->run();

This is an illustration of the power provided by the Hoa\Socket\Connection classes.

Documentation

The hack book of Hoa\Socket contains detailed information about how to use this library and how it works.

To generate the documentation locally, execute the following commands:

$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open

More documentation can be found on the project's website: hoa-project.net.

Getting help

There are mainly two ways to get help:

Contribution

Do you want to contribute? Thanks! A detailed contributor guide explains everything you need to know.

License

Hoa is under the New BSD License (BSD-3-Clause). Please, see LICENSE for details.

Related projects

The following projects are using this library:

  • PHP School, A revolutionary new way to learn PHP.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].