Perl 板


LINE

前文吃光光 在經過友人的鼎力相助之後~終於產出了這個程式 我的貢獻度應該有~~~0.1% 都是朋友寫的XDDD 也PO上來讓大家參考一下 Server: =========================== #!/usr/bin/perl use strict; use warnings; use IO::Socket; use threads; use threads::shared; my $port = 8000; my $read_wait = 2000000; my $write_wait = 5000000; #======================================================================================== my %clients; my $writing :shared; my @data_arr :shared = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0); # 當行程結束時,kernel會向他的父行程發送一個 CHLD 信號 # 因為我們的行程不會有NOHANG的狀況,所以設成IGNORE才不會生出zombie $SIG{CHLD} = 'IGNORE'; my $server = IO::Socket::INET->new( LocalPort => $port, Listen => SOMAXCONN, #最大連接數 Proto => 'tcp', ReuseAddr => 1 ) or die "Couldn't create a tcp server on port $port : $@\n";; print "Server ready. Waiting for connections on port : $port ... \n"; while (my $client = $server->accept) { # spawn a thread to handle the connection # 因為沒有要拿回傳值,所以直接detach就好了 threads->create("request_process", $client)->detach; } sub request_process { # accept data from the socket and put it on the queue my $socket = shift; my ($mode, $count, $data); while (my $line = <$socket>) { chomp($line); last if $line eq 'finish'; ($mode, $count, $data) = split /:/, $line; print "client $count in, mode : $mode ", ($mode eq 'write' ? ", data : $data" : ''), "\n"; # the writer if ($mode eq 'write') { my @data_recv = split /,/, $data; lock $writing; $writing++; # 這個動作其實沒有用到,只是怕他太無聊 lock @data_arr; # 其實不鎖這個也可以 print "array is changing and locked by client $count.\n"; @data_arr[0, 1, 2, 3, 4] = @data_recv[0, 1, 2, 3, 4]; internal_loop($mode); @data_arr[5, 6, 7, 8, 9] = @data_recv[5, 6, 7, 8, 9]; $writing--; # 這個動作其實沒有用到,只是怕他太無聊 # the reader } else { lock $writing; internal_loop($mode); print $socket join ',', @data_arr, "\n"; print "client $count get the array\n"; } } print $socket "finish\n"; close $socket; print "client $count out\n"; } sub internal_loop { my $time_wait = shift; $time_wait = ($time_wait eq 'read' ? $read_wait : $write_wait); for (1 .. $time_wait) { #do nothing } } Client: ________________________________________ #!/usr/bin/perl use strict; use warnings; use IO::Socket; use threads; my $remote_host = '127.0.0.1'; my $remote_port = 8000; $SIG{CHLD} = 'IGNORE'; for my $count (1 .. 49) { my $server = IO::Socket::INET->new( PeerAddr => $remote_host, PeerPort => $remote_port, Proto => "tcp", Type => SOCK_STREAM ) or die "Couldn't connect to $remote_host:$remote_port : $@\n"; my $mode = 'read'; $mode = 'write' if $count % 5 == 0; my $data = '-'; if ($mode eq 'write') { $data = "$count," x 9 . $count; } threads->create('data_out', $server, $mode, $count, $data); } while (threads->list()) { foreach my $thread (threads->list(threads::joinable)) { $thread->join; } } sleep(1); sub data_out { my $socket = shift; my $mode = shift; my $count = shift; my $data = shift; print $socket "$mode:$count:$data\n"; print $socket "finish\n"; while (my $line = <$socket>) { chomp($line); last if $line eq 'finish'; print "client $count get : $line\n"; } close $socket; print "client $count finished\n"; } -- --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.72.195.107
1F:→ kornelius:請愛用 POE :) 04/28 09:51
2F:→ tonini:POE最大的好處是甚麼呢?? 04/28 11:16
3F:→ kornelius:http://poe.perl.org/?What_POE_Is 04/28 15:20
4F:→ kornelius:最顯著的好處,threads 間 share 資料不是這麼方便 04/28 15:20
5F:→ kornelius:然而 POE 可利用 HEAP 直接在不同 event 裡頭分享物件 04/28 15:21
6F:→ kornelius:也可在不同 process 間 share objects 04/28 15:21







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP