首页 > 后端开发 > php教程 > phpsocket 简单的php socket 实例

phpsocket 简单的php socket 实例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-07-28 08:25:29
原创
1128 人浏览过

server:

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

<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:

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

<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 实例

以上就介绍了phpsocket 简单的php socket 实例,包括了phpsocket方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板