PHP Socket Server

Source: Internet
Author: User


 
01
<? Php
02
If (php_sapi_name ()! = 'Cli '){
03
Exit ("run cli ");
04
}
05
 
06
# Error_reporting settings in php. ini are either active or dead
07
# While (true) is so fierce that writing logs will occupy high I/O resources.
08
// Ini_set ('error _ report', E_ERROR );
09
// Ini_set ('display _ errors ', 0 );
10
Set_time_limit (0 );
11
 
12
# Record Files
13
$ Recvfile = './recv.txt ';
14
 
15
# Heartbeat flag
16
$ Heartag = "\ r \ n ";
17
 
18
# Array Length
19
$ Datalen = 1024*1024;
20
 
21
$ Ip = '1970. 168.125.233 ';
22
$ Port = 12345;
23
 
24
# IPv4, stream, TCP
25
$ Sockect = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
26
# Required for binding to the server
27
If (! Socket_bind ($ sockect, $ ip, $ port )){
28
Exit ("socket bind fail \ n ");
29
}
30
 
31
# Listener
32
Socket_listen ($ sockect );
33
# No Blocking
34
Socket_set_nonblock ($ sockect );
35
 
36
$ Clients = array ();
37
 
38
While (true ){
39
# Receive client connection
40
$ Client = socket_accept ($ sockect );
41
If (is_resource ($ client )){
42
# Triggering heartbeat
43
Socket_write ($ client, $ heartag, strlen ($ heartag ));
44
$ Clients [] = $ client;
45
Printf ("client index: % d \ n", count ($ clients ));
46
}
47
Unset ($ client );
48
 
49
If (! Empty ($ clients )){
50
Foreach ($ clients AS $ idx ==>&$ client ){
51
If (is_resource ($ client )){
52
$ Recvstr = '';
53
# Receive client data note: the fourth parameter must be zero, which is different from the manual.
54
If (socket_recv ($ client, $ recvstr, $ datalen, 0) === 0 ){
55
Socket_close ($ client );
56
Socket_shutdown ($ client );
57
Unset ($ clients [$ idx]);
58
Continue;
59
}
60
 
61
If ($ recvstr = $ heartag ){
62
# Triggering heartbeat
63
Socket_write ($ client, $ heartag, strlen ($ heartag ));
64
} Elseif (trim ($ recvstr )! = ""){
65
# Output received messages
66
$ Stdmsg = sprintf ("% d: % s \ n", $ idx, $ recvstr );
67
File_put_contents ($ recvfile, $ stdmsg, FILE_APPEND );
68
Echo $ stdmsg;
69
}
70
}
71
Unset ($ recvstr, $ idx );
72
}
73
}
74
# How long does it take to sleep? This is a problem. If you don't have a sleep, the CPU is very tired and the memory is very good.
75
Usleep (50000 );
76
}
77
 
78
Socket_close ($ sockect );
79
Socket_shutdown ($ sockect );
80
?>
Author: oodbqpoo

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.