6 use strict;
7 use IO::Socket;
9 use threads;
10 use threads::shared;
12 use constant BUFSIZE => 1024;
.........
# create port, also "new" try bind, listen
31 my $server = IO::Socket::INET -> new(
.............
41 threads -> create( \&to_client ) || die "$! \n"; # STDIN, send
+ writed to client you point, like this "192.168.0.1:3128 my message",
+ also have "list" command
# start threads to treat querys
44 while( my $client = $server -> accept ) {
45 my $client_ip = $client -> peerhost();
46 my $client_port = $client -> peerport();
47 my $tmp = "$client_ip:$client_port";
48 ## ????? IN THIS PLACE I WANT TO PUSH SOCKET $client a
+s value in hash %clients with key "$client_ip:$client_port" ????
49 %clients = ( %clients, "$client_ip:$client_port" => fi
+leno($client) ) ; # add new socket to %clients
50
54 print "got a connection from: $client_ip:$client_port
+$client\n";
55 $client -> autoflush; # don't buffer
56
57 threads -> create( \&from_client, $client, $client_ip,
+ $client_port ) || die "$! \n"; #STDOUT, get message from client
58
59 }
60 exit 0;
# ******************************************************
79 # send message to client, or get list of clients
80 sub to_client {
81 my ( $clnt_ip_port, $msg_t, $tmp );
82
83 while( sysread(STDIN, $tmp, BUFSIZE) ) {
84 #print "debug1 $tmp";
85 chomp $tmp; # very-very need, difficult to exp
+lain =)
86 if( $tmp eq "list" ) { # show list of clients
87 print "connect to this server:\n";
88 foreach my $z ( keys %clients ) { prin
+t "client: $z $clients{$z}\n"; }
89 }
90 elsif ( $tmp =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d
+{1,3}:\d{4,5}\s.*/ ) { # find in STDIN blank
91 ( $clnt_ip_port, $msg_t ) = ( $tmp =~
+/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{4,5})\s(.*)/ ); # split messa
+ge to addres like "192.168.0.1:3128" (it is key fo r %clients)
+ and message
92 #print "debug1 blank {$clnt_ip_port} {$msg_t}\n";
93 if ( exists $clients{$clnt_ip_port} )
+{ # if key exist in %clients then send
94 #print "syswrite $clients{$clnt_ip_port} {$msg_t} \n";
????? IN THIS PLACE I WANT TO GET IT BACK ( SOCKET $client as
+value from hash %clients with key "$client_ip:$client_port" ) ????
95 my $client;
96 my $fileno = $clients{$clnt_ip
+_port};
97 open $client, "+<$fileno" || d
+ie "open: $!\n";
98 syswrite( $client, $msg_t ) ||
+ die ": $! __ $@\n";
99 close $client;
100 } else { print "don't exist client or
+empty message!\n"; }
101 } else { print "wrong value, check and see \"l
+ist\" of clients!\n"; }
102 }
103 }
106 # ******************************************************
107 # STDOUT, get message from client
108 sub from_client {
109 my ( $sock, $clnt_ip, $clnt_port ) = @_;;
110 my $msg_f;
111
112 syswrite( STDOUT, "from $clnt_ip:$clnt_port: $msg_f" )
+ while sysread( $sock, $msg_f, BUFSIZE );
113 print "client at $clnt_ip:$clnt_port closed connection
+ \n";
114 delete $clients{"$clnt_ip:$clnt_port"}; # delete socke
+t from %client
115
116 $sock -> close;
117 }
|