PHP analog socket Multiple Send data implementation method _php tutorial

Source: Internet
Author: User
Tags set socket socket error
Table IV: Socket function
Function Name Description
Socket_accept () accepts a socket connection
Socket_bind () binds 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 () produces a socket equivalent to a data structure that produces a socket
Socket_get_option () Get socket options
Socket_getpeername () Gets the IP address of a remote similar host
Socket_getsockname () Gets 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 () Sets the new data value of the 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 the specified length of data
SOCKET_READV () reads 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 () 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 () sends 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

For more details please see: PHP tutorial Er/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm ">http://www.bkjia.com/phper/30/ 7cadb3c9195ac7d8ac9104da61a25c6e.htm
 post.php
function Post ($host, $port)
{
$host = "127.0.0.1";
Establish a connection
$conn = Fsockopen ($host, $port);
if (! $conn)
{
Die ("Con error");
}
Send 5 data in a loop
//
for ($i = 0; $i <5; $i + +)
{
$data = "User_name=admin". $i;
WriteData ($conn, $host, $data);
echo $i. "
";
}

Fclose ($conn);
}

function WriteData ($conn, $host, $data)
{
$header = "post/test.php http/1.1rn";
$header. = "Host: {$host}rn";
$header. = "CONTENT-TYPE:APPLICATION/X-WWW-FORM-URLENCODEDRN";
$header. = "Content-length:". strlen ($data). " RN ";
Keep-alive is the key
$header. = "Connection:keep-alivernrn";
$header. = "{$data}rnrn";

Fwrite ($conn, $header);

Fetch results
$result = ";
while (!feof ($conn))
//{
$result. = Fgets ($conn, 128);
//}
return $result;
}

Post (' 127.0.0.1 ', 80);

?>
 
      test.php
$fp = fopen (' Result.txt ', ' a ');
$data = $_post[' user_name '). "--". Date (' y-m-d h:i:s '). " RN ";
Fwrite ($fp, $data);
Fclose ($FP);
?>
Then imitate post to realize user login
socket.php
/**
* @author macopad@qq.com
* Analog socket send post mode send data
* Send file as socket.php
* Receive data for get_socket.php
* @var Unknown_type
*/
$flag = 0;
The data to post
$ARGV = Array (
' Username ' = ' macopad@qq.com ',
' Password ' = ' Macopad '
);
Constructing a string to post
$params = ";
foreach ($argv as $key = $value)
{
if ($flag!=0)
{
$params. = "&";
$flag = 1;
}
$params. = $key. " = ";
$params. = UrlEncode ($value);
$flag = 1;
}
$length = strlen ($params); length of//post
Create a socket connection
$post = Fsockopen ($HTTP _server_vars["Server_addr"],80, $errno, $errstr, or exit ($errstr. ") ". $errno);
Constructs the header of a POST request
$header = "post/guojinyong/test/get_socket.php http/1.1rn"; Develop the method of submitting data for post and the type of page and protocol to be submitted to
$header. = "Host:". $HTTP _server_vars["Server_addr"]. " RN "; Defining hosts
$header. = "referer:http://". $HTTP _server_vars["Server_addr"]. " /guojinyong/test/socket.phprn "; Referer information,
$header. = "CONTENT-TYPE:APPLICATION/X-WWW-FORM-URLENCODEDRN"; Indicates that the request is a post
$header. = "Content-length:". $length. " RN "; Length of data submitted
$header. = "CONNECTION:CLOSERNRN";//Close connection
$header. = $params. " RN ";//Add a post string
Send data for Post
Fputs ($post, $header);
Receive the data returned by get_socket.php and print it out
while (!feof ($post))
{
Echo fgets ($post, 1024);//start getting from 1024 bytes
}
Fclose ($post); Close the socket connection
?>

get_socket.php
  
    echo "SET-COOKIE:NAME=MACOPAD; Expires=fri 12-nov-99 3:59:59 GMT ";
$userName = "";
$password = "";
$userName = $_post[' userName ');
$password = $_post[' password ');
echo "
Send data through the socket simulator!
”;
echo "Current server is:". $HTTP _server_vars["Server_addr"]. "
”;
echo "The username received is:". $userName. "
The password received is: ". $password;
Show results
http/1.1 OK date:wed, Apr 06:49:07 GMT server:apache x-powered-by:php/5.2.5 cache-control:max-age=0 Expire s:wed, APR 06:49:07 GMT vary:accept-encoding content-length:189 connection:close content-type:text/html Set-C Ookie:name=macopad; Expires=fri 12-nov-99 3:59:59 GMT
Send data through the socket simulator!
The current server is: http://www.zhutiai.com
The user name received is: macopad@qq.com
The password received is: Macopad

http://www.bkjia.com/PHPjc/444759.html www.bkjia.com true http://www.bkjia.com/PHPjc/444759.html techarticle Table IV: Socket function Function name Description socket_accept () accepts a socket connection socket_bind () binds the socket to an IP address and port Socket_clear_error () Clears the socket error or ...

  • Related Article

    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.