Last active
December 22, 2015 02:19
-
-
Save inix/6402876 to your computer and use it in GitHub Desktop.
simple http chunk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use IO::Socket::INET; | |
$| = 1; | |
#发给客户端的响应 | |
my $response_hdr = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nServer: perl tcp server\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n"; | |
my $response_body = "4\r\nWiki\r\n5\r\npedia\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"; | |
my $response = $response_hdr.$response_body; | |
#创建socket,并且监听8888端口 | |
$socket = new IO::Socket::INET ( | |
LocalHost => '0.0.0.0', | |
LocalPort => '8888', | |
Proto => 'tcp', | |
Listen => 5, | |
Reuse => 1 | |
) or die "ERROR in Socket Creation : $!\n"; | |
#循环等待client发完请求 | |
$client_socket = $socket->accept(); | |
while(defined($bf = <$client_socket>)) { | |
$request.=$bf; | |
if ($request =~ /\r\n\r\n/ and $request =~ /GET/) { | |
last; | |
} | |
} | |
#发送响应给客户端 | |
print $client_socket $response; | |
$socket->close(); #关闭连接 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment