[PERL] Check if socket is connected before sending data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [PERL] Check if socket is connected before sending data
# 1  
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
# 2  
Old 09-13-2016
No one? Smilie

Adding the code I tried, just in case. With that, after server disconnection I got:
Code:
Bad arg length for Socket::pack_sockaddr_in, length is 14, should be 4  at /usr/lib/perl5/5.8.8/IO/Socket/INET.pm line 224, <$fh> line  35302.

Code:
my $sock = new IO::Socket::INET(
                    PeerAddr => '192.168.10.152',
                    PeerPort => 8000,
                    Proto    => 'tcp');
  $sock or die "no socket :$!";

  open(my $fh, '<:encoding(UTF-8)', 'list1.txt')
      or die "Could not open file $!";

  while (my $msdn = <$fh>) {
        my $port="8000";
        my $ip="192.168.10.152";
        unless ($sock->connected) {
          $sock->connect($port, $ip) or die $!;
    }
    my $str="DATA TO SEND: " . $msdn;
    print $sock $str;
  }
  close($sock);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question