Sponsored Content
Top Forums Shell Programming and Scripting [PERL] Check if socket is connected before sending data Post 302981350 by Lord Spectre on Sunday 11th of September 2016 04:22:55 AM
Old 09-11-2016
[PERL] Check if socket is connected before sending data

Hello community,
I'm programming a simple code using socket connection in perl:
Code:
$sock = new IO::Socket::INET(
                  PeerAddr => '192.168.10.7',
                  PeerPort => 8000,
                  Proto    => 'tcp');
$sock or die "no socket :$!";

Then sending data using a loop:
Code:
while...
print $sock $str;
...loop

Is there a way to insert into the loop a command to check connection?
Something like:
Code:
while...
   socket is up?
   yes => send data
   no => connect again and the continue with loop
...loop

Thank you
Lucas
 

10 More Discussions You Might Find Interesting

1. Solaris

command for to check the connected cilents to NFS

Hi all, i want to know the command for to check the how many clients are connected to NFS server? regards Krishna (4 Replies)
Discussion started by: murthy76
4 Replies

2. IP Networking

check whether connected to network

hi... can anyone pls suggest a few methods to check whether a computer is connected to any network, using the terminal, not GUI. thanks eskay (1 Reply)
Discussion started by: eskay_karthik
1 Replies

3. IP Networking

how can i check whether i am connected to thenetwork

On a Unix system terminal without GUI (graphical user interface) how can i check whether i am connected to a network. (1 Reply)
Discussion started by: ud4u
1 Replies

4. Programming

help me about sending file through socket udp with c in linux

hi, i am newbie of socket. i want to ask some question. if i want to send file from client to server, how do i do? and if i want to send file from server to client, how do i do? any pro help me and if possible, you can post code for an example i need it very much thank you for helping me:)... (1 Reply)
Discussion started by: tung1984
1 Replies

5. UNIX for Advanced & Expert Users

check external hardware connected to linux/solaris machines

hello, I'm planning to do inventory of all our servers but this time it's more on physical. Meaning I would like to check all hardware connected to it (external only like storage, power support) including the count Would like to get perhaps its serial nr..(linux and solaris different servers... (2 Replies)
Discussion started by: lhareigh890
2 Replies

6. Programming

sending http url through http socket programming..

hi am senthil am developing a software to send and receive SMS using HTTP connection first of all am forming a URL and sending that URL to a remote server using my Client Program i send that url through Socket(using Send() Function) if i send more than one URL one by one using the same... (0 Replies)
Discussion started by: senkerth
0 Replies

7. Shell Programming and Scripting

sending http url through http socket programming..

hi am senthil am developing a software to send and receive SMS using HTTP connection first of all am forming a URL and sending that URL to a remote server using my Client Program i send that url through Socket(using Send() Function) if i send more than one URL one by one using the same... (4 Replies)
Discussion started by: senkerth
4 Replies

8. Programming

Socket Programming file sending

Hello everyone,, I am doing one socket programming.Is it possible to send one file from client to server without using buffer??.I am sending tar files. Thanks in Advance (9 Replies)
Discussion started by: andrew.paul
9 Replies

9. UNIX for Advanced & Expert Users

Ways to quickly check if you are connected to the internet while on an internal network

I need some help thinking of ways to quickly check if I am connected to the internet while on an internal network. I never lose connection to the internal network but for some annoying reason I lose the internet quite often. I don't get any errors or warnings. I usually find out that I have lost my... (2 Replies)
Discussion started by: cokedude
2 Replies

10. Shell Programming and Scripting

Unable to retrieve data from shell when connected to Oracle

I am trying to get the data from oracle table to a variable I have tried the below code #/usr/bin/sh echo " I am in correct loop" SPOOL_FILE=/app/scripts/alt.lst sqlplus /nolog <<END_SQL connect bindu/bindu@gis whenever sqlerror exit failure rollback;... (4 Replies)
Discussion started by: Kiransagar
4 Replies
Wx::Socket(3pm) 					User Contributed Perl Documentation					   Wx::Socket(3pm)

NAME
Wx::Socket - wxSocket* classes USAGE
use Wx qw(:socket) ; use Wx::Event qw(EVT_SOCKET_INPUT EVT_SOCKET_LOST) ; use Wx::Event qw(EVT_SOCKET_CONNECTION) ; ########## # CLIENT # ########## my $sock = Wx::SocketClient->new(wxSOCKET_WAITALL); EVT_SOCKET_INPUT($parent , $sock , &onInput ) ; EVT_SOCKET_LOST($parent , $sock , &onClose ) ; $sock->Connect('localhost',5050) ; if (! $sock->IsConnected ) { print "ERROR " ;} sub onInput { my ( $sock , $this , $evt ) = @_ ; my $length = 123; my $buffer ; $sock->Read($buffer , 1024 , $length ) ; } ########## # SERVER # ########## my $sock = Wx::SocketServer->new('localhost',5050,wxSOCKET_WAITALL); EVT_SOCKET_CONNECTION($parent , $sock , &onConnect ) ; if ( !$sock->Ok ) { print "ERROR " ;} sub onConnect { my ( $sock , $this , $evt ) = @_ ; my $client = $sock->Accept(0) ; my ($local_host,$local_port) = $client->GetLocal ; my ($peer_host,$peer_port) = $client->GetPeer ; $client->Write("This is a data test! ") ; ... or ... $client->Write( $data , length($data) ) ; $client->Close ; } METHODS
All the methods work as in wxWidgets (see the documentation). The functions for reading data (Read, ReadMsg, Peek) take 3 arguments, like the Perl read() function: ## To read the data into the variable $sock->Read($buffer , 1024) ; ... or ... ## To append data at the given offset: $sock->Read($buffer , 1024 , $offset ) ; The write functions (Write, WriteMsg, Unread) can be used with 1 or 2 arguments: $client->Write("This is a data test! ") ; $client->Write($data , $length) ; EVENTS
The events are: EVT_SOCKET EVT_SOCKET_ALL EVT_SOCKET_INPUT EVT_SOCKET_OUTPUT EVT_SOCKET_CONNECTION EVT_SOCKET_LOST The EVT_SOCKET works as in wxWidgets, the others are wxPerl extensions. Note that EVT_SOCKET events of wxSocketClient and wxSocketServer work differently than other event types. First you need to set the event handler: $sock->SetEventHandler($handler, $id) ; Then you set what types of event you want to receive: ## this select all. $sock->SetNotify(wxSOCKET_INPUT_FLAG|wxSOCKET_OUTPUT_FLAG| wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG) ; Enable the event notification: $sock->Notify(1) ; And only after this use: ## note that $handler must be the same that was used in ## SetEventHandler EVT_SOCKET($handler, $id , sub{...} ) To make the events easier to use, all the proccess is automatic, and you just use: EVT_SOCKET_INPUT($handler , $socket , sub{...} ) EVT_SOCKET_OUTPUT($handler , $socket , sub{...} ) EVT_SOCKET_CONNECTION($handler , $socket , sub{...} ) EVT_SOCKET_LOST($handler , $socket , sub{...} ) ## This is for the events not used yet by the above: EVT_SOCKET_ALL($parent , $socket , sub{...} ) ** The new way is better to handle more than one socket in the same time too. Take a look in the demos. SEE ALSO
Wx, The wxWxwindows documentation at <http://www.wxwindows.org/> AUTHOR
Graciliano M. P. <gm@virtuasites.com.br> COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2007-06-18 Wx::Socket(3pm)
All times are GMT -4. The time now is 07:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy