Code Monkey home page Code Monkey logo

socket's Introduction

InitPHP Socket Manager

PHP Socket (TCP, TLS, UDP, SSL) Server/Client Library

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Requirements

  • PHP 7.4 or higher
  • PHP Sockets Extension

Installation

composer require initphp/socket

Usage

Supported Types :

  • TCP
  • UDP
  • TLS
  • SSL

Factory

\InitPHP\Socket\Socket::class It allows you to easily create socket server or client.

Socket::server()

public static function server(int $handler = Socket::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketServerInterface
  • $handler : Socket::SSL, Socket::TCP, Socket::TLS or Socket::UDP
  • $host : Identifies the socket host. If not defined or left blank, it will throw an error.
  • $port : Identifies the socket port. If not defined or left blank, it will throw an error.
  • $argument : This value is the value that will be sent as 3 parameters to the constructor method of the handler.
    • SSL or TLS = (float) Defines the timeout period.
    • UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix"

Socket::client()

public static function client(int $handler = self::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketClientInterface
  • $handler : Socket::SSL, Socket::TCP, Socket::TLS or Socket::UDP
  • $host : Identifies the socket host. If not defined or left blank, it will throw an error.
  • $port : Identifies the socket port. If not defined or left blank, it will throw an error.
  • $argument : This value is the value that will be sent as 3 parameters to the constructor method of the handler.
    • SSL or TLS = (float) Defines the timeout period.
    • UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix"

Methods

connection() : Initiates the socket connection.

public function connection(): self;

disconnect() : Terminates the connection.

public function disconnect(): bool;

read() : Reads data from socket.

public function read(int $length = 1024): ?string;

write() : Writes data to the socket

public function write(string $string): ?int;

Server Methods

live() :

public function live(callable $callback): void;

wait() :

public function wait(int $second): void;

Special methods for TLS and SSL.

TLS and SSL work similarly.

There are some additional methods you can use from TLS and SSL sockets.

timeout() : Defines the timeout period of the current.

public function timeout(int $second): self;

blocking() : Sets the blocking mode of the current.

public function blocking(bool $mode = true): self;

crypto() : Turns encryption on or off on a connected socket.

public function crypto(?string $method = null): self;

Possible values for $method are;

  • "sslv2"
  • "sslv3"
  • "sslv23"
  • "any"
  • "tls"
  • "tlsv1.0"
  • "tlsv1.1"
  • "tlsv1.2"
  • NULL

option() : Defines connection options for SSL and TLS. see; https://www.php.net/manual/en/context.ssl.php

public function option(string $key, mixed $value): self;

Socket Server

Example :

require_once "../vendor/autoload.php";
use \InitPHP\Socket\Socket;
use \InitPHP\Socket\Interfaces\SocketServerInterface;

$server = Socket::server(Socket::TLS, '127.0.0.1', 8080);
$server->connection();

$server->live(function (SocketServerInterface $socket) {
    switch ($socket->read()) {
        case 'exit' : 
            $socket->write('Goodbye!');
            return;
        case 'write' :
            $socket->write('Run write command.');
        break;
        case 'read' :
            $socket->write('Run read command.');
        break;
        default: return;
    }
});

Socket Client

Example :

require_once "../vendor/autoload.php";
use \InitPHP\Socket\Socket;

$client = Socket::client(Socket::SSL, 'smtp.gmail.com', 465);

$client->option('verify_peer', false)
    ->option('verify_peer_name', false);

$client->connection();

$client->write('EHLO [127.0.0.1]');

echo $client->read();

In the above example, a simple smtp connection to gmail is made.

Credits

License

Copyright © 2022 MIT License

socket's People

Contributors

muhammetsafak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

socket's Issues

How to set up a server-client socket

I'm trying to install a server
I use an OpenServer.
Done.

downloaded the archive;
placed files in a folder c:\openserver\domains\SocketInitPHPGithub;
from composer.json deleted "name": "initphp/socket",
because it gave an error the next step, then deleted it according to the recommendations on some forum.
Now my folders look like in the picture.

Question.
How to use it? What should I add?
How to start the server?
How to start the client?

Pictute-1
27 05 2022_17-55-32

Pictute-2
27 05 2022_17-55-52

I can't catch exception

Can you help?

( ! ) Fatal error: Uncaught InitPHP\Socket\Exception\SocketConnectionException: Socket Connection Error : 10060 vendor\initphp\socket\src\Client\TCP.php on line 55

        use \InitPHP\Socket\Socket;
        use \InitPHP\Socket\Exception\SocketConnectionException;
        use \InitPHP\Socket\Exception\SocketInvalidArgumentException;

	try
	{
		//error_reporting(0);
		@ini_set("default_socket_timeout",15);
		$this->client = Socket::client(Socket::TCP,$this->params['connectip'],$this->params['port']);
	}
	catch(Exception | Throwable | \SocketInvalidArgumentException | \SocketConnectionException $e)
	{
		$this->error=$e->getMessage();
		$this->setLog('connect error',$this->error);
		return false;
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.