Hey Guys, I am working with a script which needs to handle multiple tcp clients. I have learned that this can be solved by using IO::Select. I have tested my server script with nc command in Linux and it is working ok. My issue is that i need to take input from user. But my client doesn't receive any information from the server till i have pressed enter on the client. My question is shouldn't $read_handler->can_read() get a value everytime there is incoming data from server? If a value is sent by server then client should be receiving it immediately? P.S. i am new to Perl and socket programming. Any kind of help would be greatly appreciated.
use IO::Socket::INET; use IO::Select; # auto-flush on socket $| = 1; my $server_ip; my $server_port; #my $r,$w; @ARGV == 2 or die "IP and Portnumber needed"; ($server_ip,$server_port)=@ARGV; # create a connecting socket my $socket = new IO::Socket::INET ( PeerHost => $server_ip, PeerPort => $server_port, Proto => 'tcp', ); die "cannot connect to the server $!\n" unless $socket; my $read_handler = IO::Select->new(); $read_handler->add($socket); my $send_buffer; while(1) { @sockets_ready =$read_handler->can_read(); foreach my $read(@sockets_ready) { my $buffer; $buffer = <$socket>; print "$buffer\n"; } @write_ready=$read_handler->can_write(); foreach my $message(@write_ready) { my $send_buffer=<STDIN>; $message->send($send_buffer); } }
This is my server code in case.
#!/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 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; ($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 @sockets = $read_select->can_write(); #returns a array + of handl$ foreach my $sck(@sockets) { $sck->send("$buf\n"); } } else { $read_select->remove($read); } } } } $tcp_socket->close();

In reply to Select Tcp Client 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.