Socket Communication for PHP

Source: Internet
Author: User
<span id="Label3"></p><p><p><span lang="EN-US"><span lang="EN-US">You're not a stranger to tcp/ip, UDP,<span lang="EN-US">socket programming. With the development of network technology, These words are filled with our Ears. Then I would like to Ask:<br><br><span lang="EN-US">1. What is <span lang="EN-US">tcp/ip,<span lang="EN-US">UDP?<br><span lang="EN-US">2. <span lang="EN-US">where is the socket?<br><span lang="EN-US">3. <span lang="EN-US">what is a socket?<br><span lang="EN-US">4. Will you use them?<br><br><strong>What is <span lang="EN-US">TCP/IP</span> </strong> <strong>,<span lang="EN-US">UDP</span> </strong> <strong>? </strong></span></span></span></span></span></span></span></span></span></span></span></p></p><p><p><span lang="EN-US"><span lang="EN-US">TCP/IP (<span lang="EN-US">transmission Control protocol/internet Protocol) is <span lang="EN-US">a protocol/inter-network protocol, an industry-standard set of protocols designed for wide area networks (<span lang="EN-US">WANs).<br><span lang="EN-US"><span lang="EN-US">UDP (<span lang="EN-US">user Data Protocol, Subscriber Datagram Protocol) is <span lang="EN-US">the protocol that corresponds to TCP. It is <span lang="EN-US">a part of the TCP/IP protocol Family.<br><span lang="EN-US">Here is a diagram showing the relationship of these protocols.<br></span></span></span></span></span></span></span></span></span></span></span></p></p><p><p></p></p><p><p>The TCP/IP protocol family includes transport layer, network layer, and link layer. Now you know the relationship between TCP/IP and Udp.<br><strong>Where's the socket?</strong><br>In Figure 1, we don't see the socket shadow, so where is it? Or use a diagram to speak, at a glance.</p></p><p><p></p></p><p><p>The original socket is Here.<br><strong>What is a socket?</strong><br>A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of Interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified Protocol.<br>Will you use them?<br>Predecessors have done a lot of things to us, the communication between the network is a lot simpler, but after all, there is still a lot of work to do. Previously heard socket programming, think it is more advanced programming knowledge, but as long as understand how the socket programming principle, The mysterious Veil also opened.<br>A scene in the Life. You have to call a friend, dial first, a friend hears a phone call, and then you and your friend establish a connection, you can talk. When the communication is over, hang up the phone and end the Conversation. The scene in life explains this work principle, perhaps the TCP/IP protocol family is born in the life, this is not necessarily.</p></p><p><p></p></p><p><p>Start with the server Side. The Server-side initializes the socket, then binds to the port (bind), listens to the port (listen), calls the accept block, waits for the client to Connect. At this point if a client initializes a socket and then connects to the server (connect), the Client-server connection is established if the connection is Successful. The client sends the data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, closes the connection, and ends the interaction at the End.</p></p><p><strong><strong>Socket Correlation function:</strong></strong><br>----------------------------------------------------------------------------------------------<br>Socket_accept () accepts a socket connection<br>Socket_bind () binds the socket to an IP address and port<br>Socket_clear_error () clears the socket error or the last error code<br>Socket_close () Close a socket resource<br>Socket_connect () start a socket connection<br>Socket_create_listen () Open a socket listener on the specified port<br>Socket_create_pair () produces a pair of indistinguishable sockets into an array<br>Socket_create () produces a socket equivalent to a data structure that produces a socket<br>Socket_get_option () Get socket options<br>Socket_getpeername () Gets the IP address of a remote similar host<br>Socket_getsockname () Gets the IP address of the local socket<br>Socket_iovec_add () Add a new vector to a scatter/aggregate array<br>Socket_iovec_alloc () This function creates a IOVEC data structure that can send the received read and write<br>Socket_iovec_delete () Delete an already assigned Iovec<br>Socket_iovec_fetch () Returns the data for the specified Iovec resource<br>Socket_iovec_free () Releasing a Iovec resource<br>Socket_iovec_set () Sets the new data value of the Iovec<br>Socket_last_error () Gets the last error code for the current socket<br>Socket_listen () listens for all connections by the specified socket<br>Socket_read () reads the specified length of data<br>Socket_readv () reads data from a scatter/aggregate array<br>SOCKET_RECV () end data from socket to cache<br>Socket_recvfrom () accepts data from the specified socket, if not specified, the default current socket<br>Socket_recvmsg () receive messages from Iovec<br>Socket_select () Multi-channel Selection<br>Socket_send () This function sends the data to the connected socket<br>Socket_sendmsg () Send message to socket<br>Socket_sendto () sends a message to the socket at the specified address<br>Socket_set_block () Set as block mode in socket<br>Socket_set_nonblock () set to Non-block mode in socket<br>Socket_set_option () Set socket options<br>Socket_shutdown () This function allows you to close a read, write, or specified socket<br>Socket_strerror () Returns a detailed error for the specified error number<br>Socket_write () Write data to the socket cache<br>Socket_writev () Write data to a scatter/aggregate array</p><p><p><strong>Case One: Socket Communication Demo</strong></p></p><p><p>Server-side:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 <?php 2//ensure that the client is connected without time-out 3 set_time_limit (0); 4 5 $ip = ' 127.0.0.1 '; 6 $port = 1935; 7 8/* 9 +-------------------------------* @socket Communication The whole process one by one +-------------------------------* @socket_crea TE13 * @socket_bind14 * @socket_listen15 * @socket_accept16 * @socket_read17 * @socket_write18 * @ SOCKET_CLOSE19 +--------------------------------*/21///----------------the following operations are on the MANUAL-------------------*/23 I F (($sock = Socket_create (af_inet,sock_stream,sol_tcp)) < 0) {echo "socket_create () failed due to:". Socket_strerror ($SOC K). " \ n ";}26 ($ret = socket_bind ($sock, $ip, $port)) < 0) {echo" Socket_bind () failed due to: ". socket_strerror ($ret). "\ n";}30 If (($ret = Socket_listen ($sock, 4)) < 0) {echo "socket_listen () failed because:". socket_strerror ($ret). " \ n ";}34 $count = 0;36 Notoginseng do {($msgsock = socket_accept ($sock)) < 0) {" echo "socket_accept () Failed:reason: ". Socket_strerror ($msgsock). "\ n";break;41} else {42 43//sent to client $msg = "test succeeded! \ n "; socket_write ($msgsock, $msg, strlen ($msg)); echo" test succeeded AH \ n "; $buf = Socket_r EAD ($msgsock, 8192); $talkback = "message received: $buf \ n"; echo $talkback; 53 54 If (+ + $count >= 5) {break;56};57}60//echo $buf; Socket_clo Se ($msgsock); socket_close (true);?> ($sock);</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>This is the Server-side code for the Socket. Then run cmd, Note that it is your own program to store the path ah.</p></p><p><p></p></p><p><p></p></p><p><p>No reflection, the current service side of the program has started to run, the port has started to Listen. Run Netstat-ano to see the Port condition, mine is Port 1935.</p></p><p><p></p></p><p><p></p></p><p><p>look, the port is already in the listening State. Next we just run the client program to Connect. On the Code</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 <?php 2 error_reporting (e_all); 3 Set_time_limit (0); 4 echo "&LT;H2&GT;TCP/IP connection

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.