php socket html,HTTP协议与PHP Socket实现

可通过chrome开发者工具查看http的状态。

Remote Address:

210.14.147.14:80

Request URL:

http://www.ucai.cn/home/mission

Request Method:

GET

Status Code:

200 OK

Request Headers

Accept:

text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Accept-Encoding:

gzip,deflate,sdch

Accept-Language:

zh-CN,zh;q=0.8

Cache-Control:

max-age=0

Connection:

keep-alive 长连接,close是短连接

Cookie:

TS_LOGGED_USER=RFSwEEAXOrAcG1ypLeLpm; PHPSESSID=bbvnqr51h20b3ne425v1j4btd4; TS_think_language=zh-CN; TS_login_time_2613=1408417523; CNZZDATA81391345=cnzz_eid%3D958456644-1407842879-http%253A%252F%252Fwww.baidu.com%252F%26ntime%3D1408416283; Hm_lvt_37b59124316b902e2e8185984e81034e=1408156197,1408245166,1408356698,1408416074; Hm_lpvt_37b59124316b902e2e8185984e81034e=1408417310

Host:

www.ucai.cn

Referer:

http://www.ucai.cn/train 从哪连过来的,防止CSRF攻击。

User-Agent:

Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36

Response Headers

Access-Control-Allow-Origin:

http://quanzhan.ucai.cn

Cache-control:

private

Connection:

close

Content-Encoding:

gzip 服务器可配置,前端优化,压缩文件

Content-Type:

text/html; charset=utf-8

Date:

Tue, 19 Aug 2014 03:05:56 GMT

Expires:

Thu, 19 Nov 1981 08:52:00 GMT

P3P:

CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR" 支持跨域请求

Pragma:

no-cache

Server:

nginx/1.5.7

Transfer-Encoding:

chunked

Vary:

Accept-Encoding

X-Powered-By:

PHP/5.3.17

socket是一组API的统称,建立scoket请求至少需要一个IP和端口。

服务器端LISTEN是监听,相当于电话接线员,等着来连。ESTABLISHED是已经建立请求。

0818b9ca8b590ca3270a3433284dd417.png

0.0.0.0是允许所有IP来链接,127.0.0.1是只允许本机,10.131.170.167是允许局域网访问。

/etc/sysconfig/iptables 配置防火墙。

PHP Socket API

socket_create

socket_bind

socket_connect

socket_read

socket_write

socket_close

socket_listen

socket_accept

socket_strerror

/**

* @author

*/

class server{

protected $ip;

protected $port;

public function __construct($ip, $port){

$this->ip = $ip;

$this->port = $port;

$this->createSocket();

}

public function createSocket(){

//resource socket_create ( int $domain , int $type , int $protocol )

//创建并返回一个套接字,也称作一个通讯节点。一个典型的网络连接由个套接字构成,一个运行在客户端,另一个运行在服务器端。

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//AF = Address Family (地址族) INET = Internet ipv4

if($sock<0){

echo "Error:".socket_strerror($sock)."\n";

}

$ret = socket_bind($sock, $this->ip, $this->port);

if(!$ret){

echo "BIND FAILED:".socket_strerror(socket_last_error())."\n";

exit;

}

echo "OK\n";

$rs = socket_listen($sock);

if($rs < 0){

echo "LISTEN_FAILED:" . socket_strerror(socket_last_error()) . "\n";

}

do{

$asock = socket_accept($sock);

if($asock < 0){

echo "ACCEPT_FAILED:" . socket_strerror(socket_last_error()) . "\n";

break;

}

$buf = scoket_read($sock, 1204);

if(!$buf){

echo "READ_FAILED:" . socket_strerror(socket_last_error()) . "\n";

break;

}

$msg = "Received {$buf}\n";

echo $msg;

socket_write($asock, $msg, strlen($msg));

socket_close($asock);

break;

}while (True);

socket_close($sock);

}

}

$server = new server("127.0.0.1", 8110);

var_dump($server);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值