Home » Networking » Net_Socket » Manual
Net_Socket provides a generic API for communication over TCP/IP-sockets.
Net_Socket::connect()
Net_Socket::connect() – connects to a server
Synopsis
require_once 'Net/Socket.php';
boolean Net_Socket::connect (
string $addr
, integer $port
, boolean $persistent
= null
, integer $timeout
= null
, array $options
= null
)
Description
Connect to the specified port. If called when the socket is already connected, it disconnects and connects again.
Return value
boolean - Returns TRUE on success,
PEAR_Error on failure.
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| every | every |
The connection could not be established because
|
Check for server name, the connection to the net and possible firewalls on client or server side |
Note
This function can not be called statically.
See
Net_Socket::disconnect()
Net_Socket::disconnect() – disconnects from a server
Synopsis
require_once 'Net/Socket.php';
boolean Net_Socket::disconnect (
)
Description
Disconnects froms server, closes the socket.
Return value
boolean - Returns TRUE on success,
PEAR_Error on failure.
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | The connection to close was not open or is already closed. | Ensure a successfull call of connect() |
Note
This function can not be called statically.
See
Net_Socket::eof()
Net_Socket::eof() – test for end-of-file
Synopsis
require_once 'Net/Socket.php';
boolean Net_Socket::eof (
)
Description
Test for the end of data transmission.
Return value
boolean - TRUE, if no more data avaible
or connection is closed
Note
This function can not be called statically.
Net_Socket::gets()
Net_Socket::gets() – retrieve a string
Synopsis
require_once 'Net/Socket.php';
mixed Net_Socket::gets (
integer $size
)
Description
Returns a string containing data from the socket connection
Parameter
-
integer $size- maximum number of chars to recieve
Return value
string - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::getStatus()
Net_Socket::getStatus() – retrieve information about a connection
Synopsis
require_once 'Net/Socket.php';
array Net_Socket::getStatus (
)
Description
Returns information about an existing socket resource.
Return value
array - the data as array or a PEAR_Error
The content of the array is:
-
boolean 'timed_out'- the socket timed out waiting for data -
boolean 'blocked'- blocking mode -
boolean 'eof'- indicates EOF event -
integer 'unread_bytes'- number of bytes left in the socket buffer
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
Net_Socket::isBlocking()
Net_Socket::isBlocking() – check for blocking mode
Synopsis
require_once 'Net/Socket.php';
boolean Net_Socket::isBlocking (
)
Description
Find out if the socket is in blocking mode.
Return value
boolean - if TRUE, the connection is in
blocking mode
Note
This function can not be called statically.
See
Net_Socket::read()
Net_Socket::read() – retrieve data
Synopsis
require_once 'Net/Socket.php';
mixed Net_Socket::read (
integer $size
)
Description
Read a specified amount of data. This is guaranteed to return, and has the added benefit of getting everything in one fread() chunk; if you know the size of the data you're getting beforehand, this is definitely the way to go.
Parameter
-
integer $size- maximum number of chars to recieve
Return value
mixed - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::readAll()
Net_Socket::readAll() – retrieve data until connection closes
Synopsis
require_once 'Net/Socket.php';
mixed Net_Socket::readAll (
)
Description
Read until the socket closes.
This function will not exit, if the socket is in blocking mode until the socket closes.
Return value
mixed - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::readByte()
Net_Socket::readByte() – read a byte
Synopsis
require_once 'Net/Socket.php';
mixed Net_Socket::readByte (
)
Description
Read a byte from the socket
Return value
mixed - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::read() , Net_Socket::readWord() , Net_Socket::readInt() , Net_Socket::readString() , Net_Socket::readIPAddress()
Net_Socket::readInt()
Net_Socket::readInt() – read an integer
Synopsis
require_once 'Net/Socket.php';
mixed Net_Socket::readInt (
)
Description
Read an integer from the socket.
Return value
mixed - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::read() , Net_Socket::readByte() , Net_Socket::readWord() , Net_Socket::readString() , Net_Socket::readIPAddress()
Net_Socket::readIPAddress()
Net_Socket::readIPAddress() – read an IP-address
Synopsis
require_once 'Net/Socket.php';
string Net_Socket::readIPAddress (
)
Description
Reads a IP from the socket. The function expects an integer which will converted to a IP address. This function only works with IPv4 addresses.
Return value
string - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::read() , Net_Socket::readByte() , Net_Socket::readWord() , Net_Socket::readInt() , Net_Socket::readString()
Net_Socket::readLine()
Net_Socket::readLine() – retrieve data until line end
Synopsis
require_once 'Net/Socket.php';
mixed Net_Socket::readLine (
)
Description
Read until either the end of the socket or a newline, whichever comes first. Strips the trailing newline from the returned data.
Return value
mixed - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::readString()
Net_Socket::readString() – read a string
Synopsis
require_once 'Net/Socket.php';
mixed Net_Socket::readString (
)
Description
Read a zeroterminated string ("\x00") from the socket.
Return value
mixed - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::read() , Net_Socket::readByte() , Net_Socket::readWord() , Net_Socket::readInt() , Net_Socket::readIPAddress()
Net_Socket::readWord()
Net_Socket::readWord() – read a word
Synopsis
require_once 'Net/Socket.php';
mixed Net_Socket::readWord (
)
Description
Read a word from the socket.
Return value
mixed - the data or a PEAR_Error
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
"Word" means not a word in literary sense. A word is a statement of size for data.
See
Net_Socket::read() , Net_Socket::readByte() , Net_Socket::readInt() , Net_Socket::readString() , Net_Socket::readIPAddress()
Net_Socket::setBlocking()
Net_Socket::setBlocking() – sets blocking mode
Synopsis
require_once 'Net/Socket.php';
boolean Net_Socket::setBlocking (
boolean $mode
)
Description
Sets whether the socket connection should be blocking or not. A read call to a non-blocking socket will return immediately if there is no data available, whereas it will block until there is data for blocking sockets.
Parameter
-
boolean $mode- TRUE for blocking sockets, FALSE for non blocking
Return value
-
boolean- Returns TRUE on success, PEAR_Error on failure.
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::setTimeout()
Net_Socket::setTimeout() – sets timeout
Synopsis
require_once 'Net/Socket.php';
boolean Net_Socket::setTimeout (
integer $seconds
, integer $microseconds
)
Description
Sets the timeout value on socket descriptor, expressed in the sum of seconds and microseconds.
Parameter
-
integer $seconds- seconds part -
integer $microseconds- microseconds part
Return value
boolean - Returns TRUE on success,
PEAR_Error on failure.
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
Net_Socket::write()
Net_Socket::write() – write data
Synopsis
require_once 'Net/Socket.php';
boolean Net_Socket::write (
string $data
)
Description
Write data to a socket connection
Parameter
-
string $data- the data to write
Return value
boolean - Returns TRUE on success,
PEAR_Error on failure.
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Net_Socket::writeLine()
Net_Socket::writeLine() – write data including EOL
Synopsis
require_once 'Net/Socket.php';
boolean Net_Socket::writeLine (
string $data
)
Description
Write a line of data to a socket connection, followed by a trailing "\r\n".
Parameter
-
string $data- the data to write
Return value
boolean - Returns TRUE on success,
PEAR_Error on failure.
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | "not connected" | There is no open connection. | You must establish a connection before ( Net_Socket::connect()). |
Note
This function can not be called statically.
See
Example
Example – using Net_Socket
Example
Sending and recieving data
<?php
$socket = new Net_Socket() ;
// open connection
$socket->connect("http://www.example.com", 80, true, 30);
// Send data including linebreak
$socket->writeLine("String with data");
// receive data until linebreak
$result = $socket->readLine();
// receive a number of data
$result = $socket->read(512);
// close connection
$socket->disconnect();
?>