How to use php to simulate multiple data sending through a socket _ PHP Tutorial

Source: Internet
Author: User
Tags socket error
Php simulates how a socket sends data multiple times. Table 4: Socket function name description socket_accept () accepts a Socket connection socket_bind () to bind the socket to an IP address and port socket_clear_error () to clear the socket Error or
Table 4: Socket functions
Function name description
Socket_accept () accepts a Socket connection
Socket_bind () binds the socket to an IP address and port.
Socket_clear_error () clears socket errors or the final error code
Socket_close () closes a socket resource
Socket_connect () starts a socket connection
Socket_create_listen () opens a socket listener on the specified port.
Socket_create_pair () generates a pair of identical sockets into an array.
Socket_create () generates a socket, which is equivalent to the data structure of a socket.
Socket_get_option () obtain the socket option
Socket_getpeername () obtains the IP address of a remote host similar to that of a remote host.
Socket_getsockname () gets the IP address of the local socket
Socket_iovec_add () add a new vector to a scattered/aggregated array
Socket_iovec_alloc () this function creates an iovec data structure that can send and receive read/write data.
Socket_iovec_delete () deletes an allocated iovec
Socket_iovec_fetch () returns the data of the specified iovec resource.
Socket_iovec_free () releases an iovec resource.
Socket_iovec_set () sets the new value of iovec data
Socket_last_error () obtains the final error code of the current socket.
Socket_listen () listens to all connections from the specified socket
Socket_read () reads data of the specified length
Socket_readv () reads data from scattered/aggregate arrays
Socket_recv () ends data from the socket to the cache
Socket_recvfrom () accepts data from the specified socket. If no value is specified, the current socket is used by default.
Socket_recvmsg () receives messages from iovec
Socket_select () multi-path selection
Socket_send () this function sends data to the connected socket
Socket_sendmsg () sends a message to the socket
Socket_sendto () sends a message to the socket of the specified address
Socket_set_block () is set to block mode in socket
Set the socket in socket_set_nonblock () to non-block mode.
Socket_set_option () sets socket options
The socket_shutdown () function allows you to close the read, write, or specified socket
Socket_strerror () returns a detailed error of the specified error number
Socket_write () writes data to the socket cache
Socket_writev () writes data to a distributed/aggregate array

For more details, see The php Tutorial er/30/7 cadb3c9195ac7d8ac9425da61a25c6e.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 data 5 times 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 );

// Obtain the result
// $ Result = '';
// While (! Feof ($ conn ))
//{
// $ Result. = fgets ($ conn, 128 );
//}
// Return $ result;
}

Post ('1970. 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);
?>
Simulate post to achieve user login
Socket. php
/**
* @ Author macopad@qq.com
* Simulate the socket to send post data
* The sending file is socket. php.
* The received data is get_socket.php.
* @ Var unknown_type
*/
$ Flag = 0;
// Data to be post
$ Argv = array (
'Username' => 'macopad @ qq.com ',
'Password' => 'macopad'
);
// Construct the 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); // The length of post
// Create a socket connection
$ Post = fsockopen ($ HTTP_SERVER_VARS ["SERVER_ADDR"], 80, $ errno, $ errstr, 10) or exit ($ errstr. "->". $ errno );
// Construct the post request header
$ Header = "POST/guojinyong/test/get_socket.php HTTP/1.1rn"; // the data submitted by the POST method and the page and protocol type to be submitted
$ Header. = "Host:". $ HTTP_SERVER_VARS ["SERVER_ADDR"]. "rn"; // defines the Host
$ 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 POST
$ Header. = "Content-Length:". $ length. "rn"; // The Length of the submitted data
$ Header. = "Connection: Closernrn"; // close the Connection
$ Header. = $ params. "rn"; // add the post string
// Send post data
Fputs ($ post, $ header );
// Receive and print the data returned by get_socket.php
While (! Feof ($ post ))
{
Echo fgets ($ post, 1024); // get it after 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 simulation program!
";
Echo "the current server is:". $ HTTP_SERVER_VARS ["SERVER_ADDR"]."
";
Echo "the user name received is:". $ userName ."
The received password is: ". $ password;
Display result
HTTP/1.1 200 OK Date: Wed, 14 Apr 2010 06:49:07 GMT Server: Apache X-Powered-By: PHP/5.2.5 Cache-Control: max-age = 0 Expires: Wed, 14 Apr 2010 06:49:07 GMT Vary: Accept-Encoding Content-Length: 189 Connection: close Content-Type: text/html Set-Cookie: name = Macopad; expires = Fri 12-Nov-99 3:59:59 GMT
Send data through the socket simulation program!
Current server: http://www.zhutiai.com
The user name received is: macopad@qq.com
The password received is macopad.

Et_accept () accepts a Socket connection socket_bind () to bind the socket to an IP address and port. socket_clear_error () clears the socket Error or...

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.