免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4227 | 回复: 6
打印 上一主题 下一主题

如何在自己的WEBServer中使用IO::Select [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-04-29 10:43 |只看该作者 |倒序浏览
我在做一个实验性的技术模型研究。

WebServer需要给新的请求创建不影响父进程的运行方式。
fork在win32下支持不好
ithreads说实话我不知道这个在高访问下是否会出现崩溃现象
所以我想采用多路复用技术用IO::Select实现。

可我不知道在一个简单的IO::Socket产生的WebServer代码中如何实现IO::Select

请指教,谢谢!

论坛徽章:
0
2 [报告]
发表于 2003-04-29 12:13 |只看该作者

如何在自己的WEBServer中使用IO::Select

你是高手阿,呵呵~谁敢指教呢,这个代码,是不是你需要的?:)~
#!usr/bin/perl
use IO::Socket;
use IO::Select;
    # Create a socket to listen on.
    my $listener = IO::Socket::INET->;new( LocalPort =>; 8008, Listen =>; 5, Reuse =>; 1 );
    die "Can''''t create socket for listening: $!" unless $listener;
    print "Listening for connections on port 8008\n";
    my $readable = IO::Select->;new;     # Create a new IO::Select object
    $readable->;add($listener);          # Add the listener to it
    while(1) {
        # Get a list of sockets that are ready to talk to us.
        my ($ready) = IO::Select->;select($readable, undef, undef, undef);
        foreach my $s (@$ready) {
            # Is it a new connection?
            if($s == $listener) {
                # Accept the connection and add it to our readable list.
                my $new_sock = $listener->;accept;
                $readable->;add($new_sock) if $new_sock;
                print $new_sock "Welcome the this server!\r\n";
                print $new_sock "escape whit word exit!\r\n";
            } else {  # It''''s an established connection
                my $buf = <$s>;;   # Try to read a line
                # Was there anyone on the other end?
                if( defined $buf ) {
                    # If they said goodbye, close the socket. If not,
                    # echo what they said to us.
                    #
                    if ($buf =~ /exit/i) {
                        print $s "See you next time!\n";
                        $readable->;remove($s);
                        $s->;close;
                    } else {
                        print $s "enter your command:\n";
                        open(A,"$buf|" || print $s "$0 error!\n";
                        while($buff=<A>{
                        print $s "$buff\n";}
                    }
                    
                } else { # The client disconnected.
               
                    $readable->;remove($s);
                    $s->;close;
                    print STDERR "Client Connection closed\n";
                    
                }
            }
        }
    }

论坛徽章:
0
3 [报告]
发表于 2003-04-29 19:38 |只看该作者

如何在自己的WEBServer中使用IO::Select

我从来没说过这种话啊?

不是嘲笑我吧。哥们。

谢谢你的example,我已经解决问题了。

明天,我尝试使用ithreads来做。

演示http://61.132.62.131:8080

源代码为:
#!/usr/bin/perl
use IO::Socket;
use IO::Select;

#构造SOCKET对象,打开端口
my $socket = IO::Socket::INET->;new( LocalPort =>; '8080',
                                                                        Listen =>; SOMAXCONN,
                                                                        Reuse =>; 1 );
my $CRLF = "\015\012";
my $count = 0;

warn "Server runing.....\n";

my $select = IO::Select->;new($socket);
#$socket->;blocking(1);                #老版本的perl不支持这个用法
my $all_count = 0;

#等待,直到有事情发生
while(@ready = $select->;can_read) {
        my $client;

        for $client (@ready) {
                if($client == $socket) {
                        my $new = $socket->;accept;
                        $select->;add($new);
                }
                else {
                        $all_count++;
                        $client->;recv($line,1024);

                        $body = qq~<html>;

<head>;
<meta http-equiv="Content-Language" content="zh-cn">;
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">;
<title>;Test</title>;
</head>;

<body>;

<p align="center">;<b>erl WebServer</b>;</p>;
<hr>;
<p align="center">;这是一个实验性的WebServer,皆在利用PerlServer自身来产生高速的网络应用程序。</p>;
<p align="center">;系统当前被请求次数:<font color="#FF0000">;$all_count</font>;</p>;
<hr>;

<p align="left">;本程序使用了IO::Socket技术来实现Socket</p>;
<p align="left">;IO::Select的多路复用技术实现多请求处理</p>;

<p align="left">;程序在W2K+ActivePerl5.6+256MB的兼容机环境下(测试机器与服务器就是一台电脑)一分钟内请求1790次占系统CPU资源平均9%最高14%最低5%。其中解析器对内存占用了3.3xxMB容量</p>;

</body>;

</html>;~;
                        $length = length($body);

                        #print the header
                        print $client "HTTP/1.0 200 OK$CRLF";
                        print $client "Content-length: $length$CRLF";
                        print $client "Content-type: text/html\n\n$CRLF";
                        print $client $CRLF;
                        print $client "$body";

                        $select->;remove($client);
                        $client->;close();
                }
        }
}

close $socket;

论坛徽章:
0
4 [报告]
发表于 2003-04-29 20:02 |只看该作者

如何在自己的WEBServer中使用IO::Select

不错啊,还带有演示 :)
welcome!

论坛徽章:
0
5 [报告]
发表于 2003-04-29 23:17 |只看该作者

如何在自己的WEBServer中使用IO::Select

:)没有嘲笑你的意思,只是我知道你是高手啦~perlchina上不是有你的介绍吗?以后希望多多指教阿~对了写好了threads的让俺看看哦~

论坛徽章:
0
6 [报告]
发表于 2003-04-29 23:45 |只看该作者

如何在自己的WEBServer中使用IO::Select

ithreads我暂时没搞明白。

还是继续使用多路复用好了。反正这种技术蛮好的。

最起码没有资料说会由于技术缺陷造成崩溃。

这里恐怕是国内唯一人气不错的perl论坛了。

论坛徽章:
0
7 [报告]
发表于 2003-05-06 00:07 |只看该作者

如何在自己的WEBServer中使用IO::Select

高手啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP