Php socket usage details, phpsocket details _ PHP Tutorial

Source: Internet
Author: User
Php socket usage details, phpsocket details. Php socket usage details, phpsocket detailed examples in this article describes the use of socket in php, share for your reference. The specific usage is as follows: 1. how to enable socket in socketphpinfo () php and phpsocket

The examples in this article detail the socket usage in php and share them with you for your reference. The usage is as follows:

1. enable socket

Phpinfo (); check whether socket extension is enabled; otherwise, it is enabled in php. ini.

II. server code writing

The code is as follows:

<? Php
Error_reporting (E_ALL );
Set_time_limit (0 );
// Ob_implicit_flush ();

$ Address = '1970. 0.0.1 ';
$ Port = 10005;
// Create a port
If ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) = false ){
Echo "socket_create () failed: reason:". socket_strerror (socket_last_error (). "\ n ";
}

// Bind
If (socket_bind ($ sock, $ address, $ port) === false ){
Echo "socket_bind () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
}

// Listen
If (socket_listen ($ sock, 5) === false ){
Echo "socket_bind () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
}

Do {
// Get a link
If ($ msgsock = socket_accept ($ sock) === false ){
Echo "socket_accepty () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
Break;
}
// Send welcome to the client
$ Msg = "server send: welcome
";
Socket_write ($ msgsock, $ msg, strlen ($ msg ));
Echo 'read client message \ n ';
$ Buf = socket_read ($ msgsock, 8192 );
$ Talkback = "received message: $ buf \ n ";
Echo $ talkback;
If (false === socket_write ($ msgsock, $ talkback, strlen ($ talkback ))){
Echo "socket_write () failed reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
} Else {
Echo 'send success ';
}
Socket_close ($ msgsock );
} While (true );
// Close the socket
Socket_close ($ sock );
?>

The server must be executed in cli mode. it is possible that the php. ini file is loaded differently in cli mode.

The output is as follows:

At this time, there is a tem. text file in the zhoxh directory. View Configuration File (php. ini) Path => C: \ WINDOWS. It is not my php. ini file, which indicates that the php. ini file called is incorrect. In this case, specify the php. ini file command as follows:

Note that environment variables are configured when php can be executed directly.

III. client

The code is as follows:

<? Php
// Error_reporting (E_ALL );
Echo "TCP/IP connection \ n ";
$ Service_port = 10005;
$ Address = '1970. 0.0.1 ';

$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
If ($ socket = false ){
Echo "socket_create () failed: reason:". socket_strerror (socket_last_error (). "\ n ";
} Else {
Echo "OK. \ n ";
}

Echo "Attempting to connect to '$ address 'on port' $ service_port '...";
$ Result = socket_connect ($ socket, $ address, $ service_port );
If ($ result = false ){
Echo "socket_connect () failed. \ nReason: ($ result)". socket_strerror (socket_last_error ($ socket). "\ n ";
} Else {
Echo "OK \ n ";
}
$ In = "HEAD/http/1.1 \ r \ n ";
$ In. = "HOST: localhost \ r \ n ";
$ In. = "Connection: close \ r \ n ";
$ Out = "";
Echo "sending http head request ...";
Socket_write ($ socket, $ in, strlen ($ in ));
Echo "OK \ n ";

Echo "Reading response: \ n ";
While ($ out = socket_read ($ socket, 8192 )){
Echo $ out;
}
Echo "closeing socket ..";
Socket_close ($ socket );
Echo "OK. \ n ";



The execution result is as follows:
Server:

Client:

I hope this article will help you with PHP programming.


What is the main role of socket in php? In what ways?

It is generally used for interaction with the server, and data transmission is something like that!

Php socket programming is widely used in projects.

Sockets is not fully utilized in PHP. Today, you will see a server that can connect to the client and connect to the client using socket. the server sends detailed processing information to the client.
When you see the complete socket process, you will use it in future program development. This server is an HTTP server that allows you to connect to, and the client is a Web browser, which is a single client/server relationship.

Examples in this article describe the socket usage in php in detail and share it with you for your reference. The specific usage is as follows: 1. enable socket phpinfo ()...

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.