Hello i am working with socket programming in Perl. It is a server client script. My server is able to handle multiple connections but my client hangs when i try to receive data from server. Client and server exchange two messages before communication between server and client can start. My client hangs when i try to catch the second message from the server. I can't figure why my clients get stuck at $handle->recv($nick_check,255);. I am new to socket programming any help would be greatly appreciated.
#!/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"; # } #}
My server code
#!/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();

In reply to Perl client blocking communication from server by PerlNewbie94

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.