php socket重置,socket连接被重置是什么原因 ?

自己写http服务器, 服务端监听套接字, handle_request线程处理浏览器php动态请求.

...

while(1){

if(-1 == (client_fd = accept(sockfd, (struct sockaddr *) &client_sock, &sin_size))) err_exit("accept");

if(pthread_create(&ntid, NULL, (void *)handle_request, &client_fd) != 0) err_exit("pthread_create");

}

close(sockfd);

return 0;

在handle_request中与php-fpm通信之后, 获取了执行结果msg,  msg包含了两行http响应头信息, 空行 以及响应主体(php代码执行后的结果), 然后我只要添上一个响应行, 就构造了http响应数据包, 最后发给客户端.

...

/* 发送响应 */

sprintf(header, "%s 200 OK\r\n", hr->version);

//printf("%s%s\n", header, msg);

send(client_fd, header, strlen(header), 0);

send(client_fd, msg, contentLength, 0);

free(msg);

close(client_fd);

奇怪的是我在浏览器中访问, php执行结果一闪而过, 然后提示连接被重置

Firefox can’t establish a connection to the server at 127.0.0.1:8899.

在telnet测试, 能收到完整的http响应信息

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

GET /index.php HTTP/1.1

HTTP/1.1 200 OK

X-Powered-By: PHP/5.5.9-1ubuntu4.21

Content-type: text/html

hello worldConnection closed by foreign host.

php 程序

echo "hello world";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值