Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,028

HOME > PHP Manual > socket_create_pair - Creates a pair of indistinguishable sockets and stores them in an array





ลองใช้ค้นหาข้อมูล


   
Bookmark.

socket_create_pair

(PHP 4 >= 4.0.7, PHP 5)

socket_create_pairCreates a pair of indistinguishable sockets and stores them in an array

Description

bool socket_create_pair ( int $domain , int $type , int $protocol , array &$fd )

socket_create_pair() creates two connected and indistinguishable sockets, and stores them in fd . This function is commonly used in IPC (InterProcess Communication).

Parameters

domain

The domain parameter specifies the protocol family to be used by the socket. See socket_create() for the full list.

type

The type parameter selects the type of communication to be used by the socket. See socket_create() for the full list.

protocol

The protocol parameter sets the specific protocol within the specified domain to be used when communicating on the returned socket. The proper value can be retrieved by name by using getprotobyname(). If the desired protocol is TCP, or UDP the corresponding constants SOL_TCP, and SOL_UDP can also be used.

See socket_create() for the full list of supported protocols.

fd

Reference to an array in which the two socket resources will be inserted.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 socket_create_pair() example

<?php
$sockets 
= array();
/* Setup socket pair */
if (socket_create_pair(AF_UNIXSOCK_STREAM0$sockets) === false) {
    echo 
"socket_create_pair failed. Reason: ".socket_strerror(socket_last_error());
}
/* Send and Recieve Data */
if (socket_write($sockets[0], "ABCdef123\n"strlen("ABCdef123\n")) === false) {
    echo 
"socket_write() failed. Reason: ".socket_strerror(socket_last_error($sockets[0]));
}
if ((
$data socket_read($sockets[1], strlen("ABCdef123\n"), PHP_BINARY_READ) === false) {
    echo 
"socket_read() failed. Reason: ".socket_strerror(socket_last_error($sockets[1]));
}
var_dump($data);

/* Close sockets */
socket_close($sockets[0]);
socket_close($sockets[1]);
?>

Example #2 socket_create_pair() IPC example

<?php
$ary 
= array();
$strone 'Message From Parent.';
$strtwo 'Message From Child.';
if (
socket_create_pair(AF_UNIXSOCK_STREAM0$ary) === false) {
    echo 
"socket_create_pair() failed. Reason: ".socket_strerror(socket_last_error());
}
$pid pcntl_fork();
if (
$pid == -1) {
    echo 
'Could not fork Process.';
} elseif (
$pid) {
    
/*parent*/
    
socket_close($ary[0]);
    if (
socket_write($ary[1], $stronestrlen($strone)) === false) {
        echo 
"socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[1]));
    }
    if (
socket_read($ary[1], strlen($strtwo), PHP_BINARY_READ) == $strtwo) {
        echo 
"Recieved $strtwo\n";
    }
    
socket_close($ary[1]);
} else {
    
/*child*/
    
socket_close($ary[1]);
    if (
socket_write($ary[0], $strtwostrlen($strtwo)) === false) {
        echo 
"socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[0]));
    }
    if (
socket_read($ary[0], strlen($strone), PHP_BINARY_READ) == $strone) {
        echo 
"Recieved $strone\n";
    }
    
socket_close($ary[0]);
}
?>



ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่