Fellow monks,

after having searched the web for hours, trying to find an existing solution to my problem, I am now requesting your help.

Problem:
I am currently (trying) to write a client-server pair, that will be used to edit a file on a remote computer.

Because I can't be sure that either machine is constantly available, I want to monitor the server-client constantly. If the connection drops, a reconnect is initiated. I.e. I have the client constantly running, and only start the server app, when I need to change something.

After connecting, the communication starts from the server-side by requesting the current content of the file. That way I could easily (sort of) implement the monitoring of the client-side connection:

sub ReadData{ my $recv_data; my $send_data; my $command; my $working_data; while ($endEx eq "false"){ if ($connected eq "true") { $socket->recv($recv_data,1024); if ($recv_data ne "") { # make sure, we can read from the + socket, if ($debug_log eq "on") { &WriteLog("Message received: $recv_data"); } $command = substr($recv_data , 0 , 3); if ($command eq "GET"){ $send_data = "OLD:" . &GetCurrent; } elsif ($command eq "PUT"){ $working_data = substr($recv_data , 4); &SetNew($working_data); $send_data = "NEW:" . &GetCurrent; } #elsif ($command eq "SET"){ #will be used in t +he future for setting startstop handling #} if ($command eq "GET" || $command eq "PUT"){ $socket->send($send_data); if ($debug_log eq "on") { &WriteLog("Message sent: $send_data"); } } } else{ $connected = "false"; # if not, reset socket an +d reconnect Win32::GUI::NotifyIcon::Change( $ni, -icon => $icon_no, -tip => "connecting...", ); } usleep($reconnecttime); } else { &WriteLog("Connection lost"); close $socket; $socket=""; &ConnectServer(); } } }
If the client can't read from the socket any longer, it is asumed, that the connection is broken. I close the socket and reopen a new one. Works fine.
The server is the one that really gives me a hard time.
The establishment of the connection is very easy. I just can't get the monitoring to work. I tried all kinds of aproaches:
  1. Using the same way as for the client, won't work, as I am not reading from the port, but rather waiting for something to send to the client
  2. I tried multithteading, but couldn't get two threads to a)read, i.e monitor and b) prepare to write to the same socket
Does anybody have experience on how to implement something like that?
I really appreciate your help,
Thanks,
Sven

In reply to TCP server, that realizes connection loss by sven

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.