Proper use of IO::Socket::INET for TCP client in perl -


I have a question about how to use the IO :: Socket; I have a script that should run continuously, monitoring the asterisk server for some events; When these events occur, the script sends data from an event to another server via a TCP socket I have found that sometimes, The socket will be closed. My question is, should I use a socket, and open it forever (and understand why + stop it from being closed), or do I open a new socket to open and open data of each bit Will i do
I have very little experience with such things, and I have read all the documents that I have searched without receiving that reply. Below is a sample below what I have achieved:

  #! / Usr / bin / perl asterisk :: ami; Use the Io :: socket; Strict use; Use warnings; My $ sock = new IO :: Socket :: INET (Pierreader => '127.0.0.1', Perport => '1234', Proto => 'TCP',); Sub-nechlan {my ($ ami, $ event) = @_; If ($ event- & gt; {'context'} eq "trunk") {my $ unique_id = $ event-> {'UniqueID'}; My $ this_call = $ call {$ unique_id}; $ The_call- & gt; {Caller_name} = $ event- & gt; {'Calleridem'}; $ The_call- & gt; {Caller_number} = $ event- & gt; {'Calleridem'}; $ This_call- & gt; {Dnis} = $ event- & gt; {'Extension'}; $ Call {$ unique_id} = $ this_call; }; } Sub Ringchak {my ($ Ami, $ Event) = @_; ($ Event- & gt; {SubEvent} eq 'start') (my $ unique_id = $ event- & gt; {UniqueID}; if ($ call $ $ unique_id} is present) {my $ this_call = $ call { $ Unique_id}; $ The_call- & gt; {system_extension} = $ event- & gt; {Dialstring}; $ This_call- & gt; {dest_uniqueid} = $ event- & gt; {DestUniqueID}; Printf $ sock "R | % S |% s |% s ||% s \ n ", $ this_call-> gt; caller_name}, $ this_call- & gt; {caller_number}, $ this_call- & gt; {system_extension}, $ this_call- & Gt; {dnis}; $ This_call- & gt; {status} = "Ringing";}}   

is slightly more than that, but it shows that I think I should start / stop a new socket (in the ringcheck sub).

Tell me if your I should clarify anything or add

each It is better to establish a new connection for the message or to keep the connection open:

  • Is the upper part associated with connection establishment important? It depends on factors such as the frequency with which the message needs to be sent, and the quality of the network connection.

    If there is a remote and 'localhost', as if your sample is above the script, then it is unlikely to be a problem, and in fact in that case, instead of using any Unix domain socket, Will also recommend

  • Is the remote and sending back anything? If both sides need to send asynchronous messages, then it is difficult to manage hard-to-run connections, it seems that this is not the case for you.

  • Are there any important resources that will keep you holding the connection open? Note that I do not remember that there is a good reason to leave a random connection to make a new connection every time. If possible, it would be better to diagnose that problem in any case. Otherwise, you can get incredible performance, no matter what attitude you take.

    In my experience, long-held TCP connections seem to be a very common reason for random dropouts, the Intermediate Tracking Firewall. Such a firewall will leave a connection, if they do not see any activity for the protection of their resources over a period of time. There is a way to deal with this, which I use in some of my devices, the socket option SO_KEEPALIVE is set to the socket, such as:

      use a socket; ... Setcon ($ SOC, SOOLCOCK, SOKPLEVE, 1); There are some benefits in this - the result is that the colonel regularly sends a reader message on your connection at intervals, even if all is silent, which is enough to keep some firewalls happy. In addition, if your connection drops, your program wants to write it instead of next time (although you can not notice it, unless you are regularly checking errors on your sockets ) Instead of directly.  

    Perhaps your best way might be to set SO_KEEPALIVE, and open your socket, but check the errors even when you try to write it, and if you have any errors , Then close the connection and reopen.

    You may also be using.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -