Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello guys,

I'm trying to do a tcp socket server which will send a message to all clients connected every 10s, like a heartbeat.

I managed to do it using IO::Socket::INET only, but the code only handle a single connection, so when the client dies, the server dies too.

Searching, I found a way to do it with IO::Select, but I can't make it work too.

It doesn't send the 'HeartBeat' and still when I stop the a client the server stops too. Can you guys help me to find what I'm doing wrong?

Here the codes:

server.pl

#!/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; ($server_ip,$server_port)=('127.0.0.1', 5000); # 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"; my $read_select = IO::Select->new(); $read_select->add($tcp_socket); my $hello ="Hello <VERSION>\n"; 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($hello); $read_select->add($new_connection); }else{ my $buf; my $msg; $buf='HeartBeat'; if($buf){ my @sockets = $read_select->can_write(); foreach my $sck(@sockets){ $sck->send("$buf\n"); } }else{ $read_select->remove($read); } } } } $tcp_socket->close();

client.pl

#!/usr/bin/perl #tcpclient.pl use IO::Socket::INET; use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); $| = 1; my ($socket,$client_socket); $socket = new IO::Socket::INET ( PeerAddr => '127.0.0.1', PeerPort => '5000', Proto => 'tcp' ) or die "ERROR in Socket Creation : $! $@\n"; print "TCP Connection Success.\n"; while(1){ $socket->recv($data,1024); print "Received from Server : $data\n"; } $socket->close();

In reply to How do I make a tcp socket heartbeat? by pudda

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found