Hi,
I have a multithreaded socket client that i use to send and receive messages to/from a server.I would like my client to connect to the server if it can,if it can't it must try to connect every 500 ms(and loop until it connects). Once it is connected,it must make two threads of which each one calls a subroutine.All this is fine,
My problem is that,if for some reason,the server disconnects,my client loops giving error messages.I want my client to detect that the server has disconnected,and retry to connect every 500ms.
How can i detect that the socket went down,to pass my script back to step 1 where it tries to connect.
Thanks for any help/suggestions.
Here's my code
#!/usr/local/bin/perl
use threads;
use IO::Socket::INET;
$error=1;
my $MySocket;
while (1) {
if ($error eq 1) {
if ($MySocket=new IO::Socket::INET->new(PeerPort=>1234
+,Proto=>'tcp',PeerAddr=>'192.168.100.9')) {
$error=0;
print "Connected $error \n";
}
else {
print "Could not connect try again 5ms \n";
select(undef, undef, undef, 0.5);
$error=1;
}
}
else {
print "Hallo daar Error=$error\n";
$thread1 = threads->new(\&sendQMsg);
$thread2 = threads->new(\&sendQMsg2);
grep {$_->join;} ($thread1,$thread2);
while ( my(@list)=threads->list()) {
print "$#list\n";
grep { $_->join } @list;
}
}
}
sub sendQMsg {
$message = "Hi from thread 1";
if ($MySocket->send($message)) {
$error=0;
}
else {
$error=1;
}
}
sub sendQMsg2 {
$message = "Hi from thread 2\n";
if ($MySocket->send($message)) {
$error=0;
}
else {
$error=1;
}
}
Before my MySocket->send i would like to see that the socket server went down,and this must pass control back to the place where i try to connect.Any suggestions?
Thanks
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.