Dear monks,
I am trying to create short text user input in my chat client code, and I got stack. I am using a simple do until loop and the loop is breaking after the first run, unless it meets the if condition and re transmitted though the goto function.
Sample of working code provided under:
Update:#!/usr/bin/perl use strict; use warnings; use constant MAXBYTES => scalar 5; my $result; $| = 1; MSG: print "Please be aware maximum length 255 characters!:\n"; do { print "Send this text to clients:\n\n"; chomp ($result = <STDIN>); if (length($result) >= MAXBYTES) { print "\nYou have reached the limit of characters ".length($result +)."!\n\n"; goto MSG; } } until ($result eq "END" or $result "end"); $| = 1;
Correcting the until condition. Thanks LanX for pointing out my mistake and also about <readmore>.
If the user enters text under the MAXBYTES condition then the loop breaks and exits, even without the user typing end or END
The complete code implementation of TCP chat client is provided under:
#!/usr/bin/perl #TCP Client code use utf8; use strict; use warnings; use Data::Dumper; use IO::Socket::INET; use constant ARGUMENTS => scalar 2; use constant NICKNAME => scalar 12; use constant MAXBYTES => scalar 25; $| = 1; my ($client_socket,$server_data,$client_packet,$result); my $info = $ARGV[0]; # User message argv[0] my $Nickname = $ARGV[1]; # User nickname argv[1] if ($Nickname =~ s/\W//g) { print "Special characters detected, please remove them!\n"; exit(); } if (@ARGV > ARGUMENTS) { print "\nPlease no more than ".ARGUMENTS." arguments (ARGV[])!\n"; print "\nCorrect Syntax: perl Exercise_5_Client.pl 'IP:PORT NICKN +AME' (e.g. 127.0.0.1:5000 Thanos)\n\n"; exit(); } elsif (@ARGV < ARGUMENTS) { print "\nPlease no less than ".ARGUMENTS." arguments (ARGV[])\n"; print "\nCorrect Syntax: perl Exercise_5_Client.pl IP number\n (e.g. 127.0.0.1) port number (e.g. 5000)\n Nickname (e.g. Thanos)\n\n"; exit(); } if (length($Nickname) > NICKNAME) { print "\nPlease no more than ".NICKNAME." characters as nickname!\ +n"; } else { my $string = index($info, ':'); if ($string == '-1') { die "Please add ':' in your input - ".$info."\n"; } my @input = split( ':' , $info ); # creating object interface of IO::Socket::INET modules which inte +rnally creates # socket, binds and connects to the TCP server running on the spec +ific port. $client_socket = new IO::Socket::INET ( PeerHost => $input[0], PeerPort => $input[1], Proto => 'tcp', ) or die "ERROR in Socket Creation : $@!\n"; $client_socket->autoflush(1); # Send/receive immediately $client_packet = "\0"; my $first = &send($client_packet); my $receive = &receive(); $client_packet = $Nickname; my $second = &send($client_packet); my $receive_2 = &receive(); if ($receive_2 ne "OK") { $client_packet = "ERROR"; &send($client_packet); print "Client received unexpected message terminating!\n"; exit(); } # End of if receive eq OK MSG: print "Please be aware maximum length 255 characters!:\n"; do { print "Send this text to clients:\n\n"; chomp ($result = <STDIN>); if (length($result) >= MAXBYTES) { print "\nYou have exceeded the limit of characters ".length($r +esult)."!\n\n"; goto MSG; } else { my $new_line = "\n"; $client_packet = "".$result."".$new_line.""; utf8::encode($client_packet); my $third = &send($client_packet); my $receive_3 = &receive(); print "This is received_3: ".$receive_3."\n"; } } until ($result eq "END" || "end"); print "Goodbye\n"; $client_socket->close(); } # End of else condition (ARGV[]) sub send { $client_socket->send($client_packet); return $client_packet; } sub receive { # we can read from socket through recv() in IO::Socket::INET $client_socket->recv($server_data,MAXBYTES); print "The following data received form Server: (\ $server_data \) + "; print "\n"; return $server_data; } $| = 1;
I tried to enter another goto condition after end of do loop but not successfully. If I do so I lock my self in an infinity loop that I do not send messages or receiving messages, not even able to exit loop by typing end.
Any suggestions or ideas would be much appreciated.
Thanks everyone for their time and effort to assist me with my problem.
In reply to do until loop breaks before meeting the condition by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |