Home  >  Article  >  Backend Development  >  phpsocket simple php socket example

phpsocket simple php socket example

WBOY
WBOYOriginal
2016-07-28 08:25:291084browse

server:

<span> 1</span> <?<span>php
</span><span> 2</span><span>set_time_limit</span>(0<span>);
</span><span> 3</span><span> 4</span><span>$ip</span> = '127.0.0.1'<span>;
</span><span> 5</span><span>$port</span> = 8888<span>;
</span><span> 6</span><span> 7</span><span>//</span><span> 1. 创建</span><span> 8</span><span>if</span>( (<span>$sock</span> = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) == <span>FALSE</span><span> ){
</span><span> 9</span><span>echo</span> 'create fail:' .<span> socket_strerror(socket_last_error());
</span><span>10</span><span>}
</span><span>11</span><span>12</span><span>//</span><span> 2. 绑定</span><span>13</span><span>if</span> ( socket_bind(<span>$sock</span>, <span>$ip</span>, <span>$port</span>) == <span>FALSE</span><span> ) {
</span><span>14</span><span>echo</span> 'bind fail:' .<span> socket_strerror(socket_last_error());
</span><span>15</span><span>}
</span><span>16</span><span>17</span><span>//</span><span> 3. 监听</span><span>18</span><span>if</span>( socket_listen(<span>$sock</span>, 4) == <span>FALSE</span><span> ){
</span><span>19</span><span>echo</span> 'listen fail:' .<span> socket_strerror(socket_last_error());
</span><span>20</span><span>}
</span><span>21</span><span>22</span><span>$count</span> = 0<span>;
</span><span>23</span><span>24</span><span>do</span><span>{
</span><span>25</span><span>//</span><span> 4. 阻塞,等待客户端请求</span><span>26</span><span>if</span> ( (<span>$msgsock</span> = socket_accept(<span>$sock</span>)) == <span>FALSE</span><span> ) {
</span><span>27</span><span>28</span><span>echo</span> 'accept fail:' .<span> socket_strerror(socket_last_error());
</span><span>29</span><span>30</span><span>break</span><span>;
</span><span>31</span>     } <span>else</span><span> {
</span><span>32</span><span>33</span><span>//</span><span> 5. 向客户端写入信息</span><span>34</span><span>$msg</span> = 'server send successfully!'<span>;
</span><span>35</span>         socket_write(<span>$msgsock</span>, <span>$msg</span>, <span>strlen</span>(<span>$msg</span><span>));
</span><span>36</span><span>37</span><span>38</span><span>//</span><span> 5. 读取客户端信息</span><span>39</span><span>echo</span> '-----test successfully!------'<span>;
</span><span>40</span><span>$buf</span> = socket_read(<span>$msgsock</span>, 8192<span>);
</span><span>41</span><span>42</span><span>43</span><span>$talkback</span> = 'receive client: ' . <span>$buf</span><span>;
</span><span>44</span><span>echo</span><span>$talkback</span><span>;
</span><span>45</span><span>46</span><span>47</span><span>if</span> (<span>$count</span> >= 5<span>) {
</span><span>48</span><span>break</span><span>;
</span><span>49</span><span>        }
</span><span>50</span><span>    }
</span><span>51</span><span>52</span><span>//</span><span> 6. 关闭socket</span><span>53</span>     socket_close(<span>$msgsock</span><span>);
</span><span>54</span><span>55</span> }<span>while</span>(<span>true</span><span>);
</span><span>56</span><span>57</span><span>//</span><span> 6. 关闭socket</span><span>58</span> socket_close(<span>$sock</span>);

 简单的php socket 实例

 简单的php socket 实例

client:

<?<span>php
</span><span>error_reporting</span>(<span>E_ALL</span><span>);
</span><span>set_time_limit</span>(0<span>);


</span><span>$ip</span> = '127.0.0.1'<span>;
</span><span>$port</span> = 8888<span>;

</span><span>//</span><span> 1. 创建</span><span>$socket</span> = socket_create(AF_INET, SOCK_STREAM,<span> SOL_TCP);

</span><span>if</span>( <span>$socket</span> == <span>FALSE</span><span> ) {
    </span><span>echo</span> 'create fail: ' .<span> socket_strerror(socket_last_error());
} </span><span>else</span><span> {
    </span><span>echo</span> 'OK'<span>;
}

</span><span>//</span><span> 2. 链接</span><span>echo</span> 'we will try to connect ' . <span>$ip</span> .':' . <span>$port</span> . '\r\n----'<span>;
</span><span>$result</span> = socket_connect(<span>$socket</span>, <span>$ip</span>, <span>$port</span><span>);
</span><span>if</span> ( <span>$result</span> == <span>FALSE</span><span>) {
    
}

</span><span>$in</span> = 'HO '<span>;
</span><span>$in</span> .= 'first blood--------'<span>;
</span><span>$out</span> = ''<span>;

</span><span>//</span><span> 3. 向服务端写入</span><span>if</span>( !socket_write(<span>$socket</span>, <span>$in</span>, <span>strlen</span>(<span>$in</span><span>)) ) {
    </span><span>echo</span> 'write fail: ' .<span> socket_strerror(socket_last_error());
} </span><span>else</span><span> {
    </span><span>echo</span> '-----send to server succefully! \r\n----'<span>;
    </span><span>echo</span> 'the content is ' . <span>$in</span><span>;
}

</span><span>//</span><span> 3. 从服务端读取</span><span>while</span> ( <span>$out</span> = socket_read(<span>$socket</span>, 8129<span>) ) {
    </span><span>echo</span> '-----receive from server succefully!\r\n------'<span>;
    </span><span>echo</span> 'the contents is ' . <span>$out</span><span>;
}

</span><span>//</span><span> 4. 关闭</span><span>echo</span> '----close socket ...'<span>;
socket_close(</span><span>$socket</span><span>);
</span><span>echo</span> 'closed ok.';

 简单的php socket 实例

 简单的php socket 实例

The above has introduced a simple php socket example of phpsocket, including the content of phpsocket. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn