PerlNewbie94 has asked for the wisdom of the Perl Monks concerning the following question:
My server code#!/usr/bin/perl -w use strict; use IO::Socket; @ARGV == 2 or die "IP:Portnumber Nickname needed "; my ( $host, $port, $kidpid, $handle, $line,$nickname, $connect_check, +$nick_che$ ($host, $port)=@ARGV; my $input = $ARGV[0]; $nickname = $ARGV[1]; my @arguments= split(':',$input); my $server_ip= $arguments[0]; my $server_port=$arguments[1]; $handle = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $server_ip, PeerPort => $server_port ) or die "can't connect to port $server_port on $server_ip: $!"; $handle->autoflush(1); print STDERR "[Connected to $host:$port]\n"; $handle->recv($connect_check,255); $nickname= "NICK" . " $nickname"; $handle->send($nickname); $handle->recv($nick_check,255); #die "can't fork: $!" unless defined( $kidpid = fork() ); # the if{} block runs only in the parent process #if ($kidpid) { # copy the socket to standard output # while ( defined( $line = <$handle> ) ) { # print STDOUT $line; # } #kill( "TERM", $kidpid ); # send SIGTERM to child #} # the else{} block runs only in the child process #else { # # copy standard input to the socket # while ( defined( $line = <STDIN> ) ) { # print $handle "$line"; # } #}
#!/usr/bin/perl -w use strict; use warnings; use IO::Socket; use IO::Select; # auto-flush on socket $| = 1; my $server_ip; my $server_port; @ARGV == 2 or die "IP and Portnumber needed"; ($server_ip,$server_port)=@ARGV; # creating a listening socket my $tcp_socket = new IO::Socket::INET( LocalHost => $server_ip, LocalPort => $server_port, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "cannot create socket $!\n" unless $tcp_socket; print "server waiting for client connection on port $server_port\n"; #Create write and readhandlers with select my $read_select = IO::Select->new(); $read_select->add($tcp_socket); #version send to server my $Version ="Hello <VERSION>\n"; #hash for client my %clients; while(1) { my $new_readable; my $nick; my $message_check=0; ($new_readable)=IO::Select->select($read_select,undef,undef,0); foreach my $read(@$new_readable) { if($read==$tcp_socket) { my $new_connection = $read->accept(); $new_connection->send($Version); $read_select->add($new_connection); } else { my $buf; my $msg; $buf=<$read>; if($buf) { my $check_message = index($buf,"MSG"); #Hanndle nick my $nick_check; my $content = "NICK"; my $found_content = index($buf,$content); if ($found_content == 0) { $nick_check="OK\n"; $read->send($nick_check); print "Ok sent\n"; } else { $nick_check="Error <txt>\n"; $read->send($nick_check); } if ($found_content == 0) { $nick = substr $buf, 5; $clients{"$read"}="$nick"; } my $nick_name=""; my $message = "MSG"; my $header=""; my $buffer; if($buf =~m/^MSG/) { while (my ($key,$value)= each %clients){ if ($key eq $read) { $nick_name = $value; chomp $nick_name; } } } if ($nick_name) { $header = $message." $nick_name"; substr($buf,0,3) = $header; } my @sockets = $read_select->can_write(); #returns a array of handl$ foreach my $sck(@sockets) { if ($sck!=$read && $buf !~m/^NICK/ && $message_check ! +=1) { $sck->send("$buf\n"); } } } else { $read_select->remove($read); } } } } $tcp_socket->close();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl client blocking communication from server
by tybalt89 (Monsignor) on Oct 17, 2019 at 08:25 UTC | |
|
Re: Perl client blocking communication from server
by Anonymous Monk on Oct 17, 2019 at 06:55 UTC | |
by PerlNewbie94 (Novice) on Oct 17, 2019 at 09:27 UTC |