PHP how to send and receive data to the socket server, Socket _php Tutorial

Source: Internet
Author: User
Tags get ip set socket socket error

Php method to send and receive data to the socket server, socket


The example in this paper describes how PHP is sending and receiving data to the socket server. Share to everyone for your reference. Specific as follows:

In PHP to send data to other programs, you need to use the socket function to PHP to the instance, below we briefly look at an example, the code is as follows:

The code is as follows:

/*socket     send and receive Data @host (string) socket server IP     @post (int) port     @str (String) data to be sent     @back 1|0 socket end for data return     returns true|false| Server data */function sendsocketmsg ($host, $port, $str, $back =0) {         $socket = socket_create (Af_inet, sock_stream,0);         if ($socket < 0) return false;         $result = @socket_connect ($socket, $host, $port);         if ($result = = false) return false;         Socket_write ($socket, $str, strlen ($STR));        if ($back!=0) {             $input = Socket_read ($socket, 1024x768);             Socket_close ($socket);                 return $input;         } else{             Socket_close ($socket);                 return true;             }     }


The second parameter of the Socker_read specifies the number of bytes to read, which you can use to limit the size of the data fetched from the client.

Sock function Introduction

Name of function Describe
Socket_accept () Accept a socket connection
Socket_bind () Bind the socket to an IP address and port
Socket_clear_error () Clears the socket error or the last error code
Socket_close () Close a socket resource
Socket_connect () Start a socket connection
Socket_create_listen () Open a socket listener on the specified port
Socket_create_pair () Produces a pair of indistinguishable sockets into an array
Socket_create () Generate a socket that is equivalent to generating a data structure for a socket
Socket_get_option () Get socket options
Socket_getpeername () Get IP address of a remote similar host
Socket_getsockname () Get the IP address of the local socket
Socket_iovec_add () Add a new vector to a scatter/aggregate array
Socket_iovec_alloc () This function creates a IOVEC data structure that can send the received read and write
Socket_iovec_delete () Delete an already assigned Iovec
Socket_iovec_fetch () Returns the data for the specified Iovec resource
Socket_iovec_free () Releasing a Iovec resource
Socket_iovec_set () Set the new data value for Iovec
Socket_last_error () Gets the last error code for the current socket
Socket_listen () Listens for all connections by the specified socket
Socket_read () Reads data of a specified length
SOCKET_READV () Read data from a scatter/aggregate array
SOCKET_RECV () End data from socket to cache
Socket_recvfrom () Accepts data from the specified socket, if not specified, the default current socket
Socket_recvmsg () To receive messages from Iovec.
Socket_select () Multi-channel selection
Socket_send () This function sends the data to the connected socket
Socket_sendmsg () Send Message to Socket
Socket_sendto () Send a message to the socket at the specified address
Socket_set_block () Set as block mode in socket
Socket_set_nonblock () Set to non-block mode in socket
Socket_set_option () Set socket options
Socket_shutdown () This function allows you to close a read, write, or specified socket
Socket_strerror () Returns a detailed error for the specified error number
Socket_write () Write data to the socket cache
Socket_writev () Write data to a scatter/aggregate array

Note:The Socket_read function will always read the shell data until the n,t or characters are met, and the PHP script considers these characters as input terminator.

I hope this article is helpful to everyone's PHP programming.


http://www.bkjia.com/PHPjc/947218.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/947218. Htmltecharticlephp the way to send and receive data to the socket server, the socket transceiver This article describes how PHP send and receive data to the socket server. Share to everyone for your reference. Specific as follows: in PHP to ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.