simple socket programming in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple socket programming in perl
# 1  
Old 08-23-2008
simple socket programming in perl

hi

i want to write simple socket program which will listen on socket . here is the code
Code:
## read msg on socket 
#! /usr/bin/perl

use IO::Socket::INET;

my $MySocket= IO::Socket::INET->new(LocalPort=>1234,
                                                       Proto=>'udp') ;
while () {
   $MySocket->recv($text,128);
   if ( $text =~ /Done/i )
   {
      print " ftpdone\n"  ;
   } else
   {
      print " FTP Failed :Msg from Server [$text]\n" ;
      exit 1 ;
   }
}

## send message to socket
use IO::Socket::INET;
$MySocket=new IO::Socket::INET->new(PeerPort=>1234,
                                    Proto=>'udp',
                                    PeerAddr=>'zzz.zzz.zz.zz');

$msg="ftp failed" ;
$MySocket->send($msg);

now i want is that receiver program should try to read on port for 10 sec if no msg is received then wait for minute and again check for message for 10 sec do this for 10 minutes ( inshort check if any message is received in 10 minutes )

but code waits at
Code:
$MySocket->recv($text,128);

line for mesg till it receives msg. Timeout => 10 is not working
what else i can do ? any other module ?? or any work around ??
# 2  
Old 08-23-2008
Under Linux there is a way to make any file handler / file descriptor non-block through the fcntl() function.

Code:
use POSIX;
...
fcntl($MySocket, F_SETFL(), O_NONBLOCK()); # call this before $MySocket->recv($text,128)
...

You can also use sysread() which won't block.
# 3  
Old 08-23-2008
IO::select is also a standard unix sockets approach.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Socket Programming in Perl

Hi All I am getting an error when using the below code Receiver use IO::Socket; $sock = new IO::Socket::INET (LocalHost => 'goldengate', LocalPort => 1200, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Socket could not be created. Reason: $!" unless $sock; while ($new_sock =... (5 Replies)
Discussion started by: parthmittal2007
5 Replies

2. Programming

socket programming

how to include socket.h in visual studio 2005.. (2 Replies)
Discussion started by: asd123
2 Replies

3. Programming

help with socket programming in c

i'm doing a simple program in socket programming on c i have server that can handle 2clients in a single machine i'm running ubuntu linux so i got it work but the probelm when clients send a message the server will echo it but i cant distinguish which client send the message client 1 or client... (7 Replies)
Discussion started by: kedah160
7 Replies

4. UNIX for Dummies Questions & Answers

hi i need help with socket programming

in socket programming how can i : Create for example 3 blank files, namely: server, client, network •Server: act as servers/provider, will receive all requests from different client •Client: requesters •Network: middle-layer of communication between server & client any tips or... (6 Replies)
Discussion started by: kedah160
6 Replies

5. Programming

Help with socket programming in C

hi guys i got this code trying to make connection between the server and multi clients but when i do ./server i got message server waiting then when i run ./client it says client 1 nosuch file i dont know whats that should i use any argument plz help how to compile and run and whats the expected... (1 Reply)
Discussion started by: kedah160
1 Replies

6. UNIX for Advanced & Expert Users

socket programming

Hi, I am trying to connect to more than 60 servers to perform some actions remotely. for this I am using ssh2. But its taking lot of time. Though i am running the command in background, still its taking long time to execute.. Any one let me know can we use sockets instead of ssh2 for... (3 Replies)
Discussion started by: pvamsikr
3 Replies

7. Programming

help regarding socket programming

i m using sockets for setting up a connection between a server and a client. When the clients gets connected to the server, its ip is conveyed to the server through one of the predefined structures in c library... i save this ip address in an array....1st client's ip address goes to the zeroth... (1 Reply)
Discussion started by: abmxla007
1 Replies

8. Programming

Need Help Regarding Socket Programming

Can anyone plz me. I need a sample code for the following description. Its urgent. It is C/Socket program with the following descriptions: NAME coreadServer - Concurrent Readers Server. coreadClient - Concurrent Readers Client. SYNOPSIS coreadServer <OutputFile> coreadClient <n>... (1 Reply)
Discussion started by: priya.vmr
1 Replies

9. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

10. Programming

Socket programming

Suppose i am writing a C program which is going to use Socket calls. I want to use a Unix port for my Socket. How can i determine a port which is not already in use? (1 Reply)
Discussion started by: Nadeem Mistry
1 Replies
Login or Register to Ask a Question