hxp
2025-06-09 6c3f6335c70859ded94a1ad8d218acb0ac34239c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Socket;
#´´½¨ÈÕÆÚ£º2012-5-16 ÏÂÎç1:26:01
#×÷Õߣºzhouliang
 
const nSingleRecvMaxLen=1000; #×î´ó½ÓÊÕ³¤¶È 
 
class Socket
{
    private $m_socket=null;
    private $m_bConnect=false;
    function __construct()
    {
    }
    
    function __destruct()
    {
        socket_close( $this->m_socket );
    }
    
    #½øÐгõʼ»¯,³É¹¦·µ»Øtrue£¬·ñÔò·µ»Øfalse
    public function Init()
    {
        $this->m_socket = socket_create( AF_INET, SOCK_STREAM, getprotobyname("tcp") );
        
        if( !$this->m_socket )
        {
            \Logging\LogInfo( '´´½¨socketʧ°Ü£¡' );
            return false;
        }
        else
        {
            socket_set_option( $this->m_socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>30, "usec"=>0 ) );
            \Logging\LogInfo( '´´½¨socket³É¹¦£¡' );
            return true;
        }
    }
    
    #socketÁ¬½Ó²Ù×÷£¬³É¹¦Ôò·µ»Øtrue,ʧ°Ü·µ»Øfalse
    public function Connect( $strIP, $strPort )
    {
        if( !isset( $this->m_socket ) )
        {
            \Logging\LogError( 'socket Î´´´½¨£¡' );
            return false;
        }
        
        if( $this->m_bConnect )
        {
            \Logging\LogInfo( 'socketÒѾ­Á¬½Ó' );
            return true;
        }
        
        if( !socket_connect( $this->m_socket, $strIP, $strPort ) )
        {
            \Logging\LogError( "Á¬½Ó·þÎñÆ÷ʧ°Ü IP:$strIP Port:$strPort, ´íÎóÔ­Òò:"
                    .socket_last_error( $this->m_socket ) );
            $this->m_bConnect=false;
            return false;
        }
        else
        {
            \Logging\LogInfo( "socket Á¬½Ó³É¹¦, IP:$strIP Port:$strPort" );
            $this->m_bConnect=true;
            return true;
        }
        
        $this->m_bConnect=true;
    }
    
    #socket·¢ËͲÙ×÷£¬³É¹¦·µ»Øtrue,ʧ°Ü·µ»Øfalse
    public function Write( &$buffer )
    {
        if( !$this->IsConnected() )
        {
            \Logging\LogError( "socket»¹Î´Á¬½Ó²»ÄܽøÐÐд²Ù×÷£¡" );
            return false;
        }
        if( false===socket_write( $this->m_socket, $buffer ) )
        {
        
            \Logging\LogError( "·¢ËÍÊý¾Ýʧ°Ü!´íÎóÂë:".socket_last_error( $this->m_socket ) );
            return false;
        }
        else
        {
            \Logging\LogInfo( '·¢ËÍÊý¾Ý³É¹¦!' );
            return true;
        }
    }
    
    #socket½ÓÊÕ²Ù×÷£¬³É¹¦·µ»Øtrue,ʧ°Ü·µ»Øfalse
    public function Read( &$buffer )
    {
        if( !$this->IsConnected() )
        {
            \Logging\LogError( 'socket»¹Î´´´½¨£¬²»ÄܽøÐжÁ²Ù×÷£¡' );
            return false;
        }
        
        $buffer=socket_read( $this->m_socket, nSingleRecvMaxLen );
        if( !$buffer )
        {
            $strLastError=socket_last_error( $this->m_socket );
            \Logging\LogError( '½ÓÊÕÊý¾Ýʧ°Ü£¡´íÎóÂë:'.$strLastError );
            return false;
        }
        
        return true;
    }
    
    
    #ÊÇ·ñÁ¬½Óµ½Ô¶¶Ë
    public function IsConnected()
    {
        return $this->m_bConnect;
    }
}
?>