• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

server/client pair using a udp socket

the server should receive 1 and only 1 client and print "." to the shell once a second.  the socket should listen on port 9999, close the connection, remove the socket, and exit on the TERM sig.

I have included some code as follows:
#!/usr/bin/perl
use Socket;
socket(SERVER,PF_INET,SOCK_DGRAM, 0) ;
my $sin = sockaddr_un(9999,INADDR_ANY);
bind (SERVER,$sin) || die $!;
this is where I am getting confused because I've read something similar to this
use IO::Socket;

$socket= IO::Socket::INET->new(PeerAddr =>$remote_host,
                                                 PeerPort  =>9999,
                                                 Proto       =>"udp",
                                                 Type       =>SOCK_DGRAM)  || die "Couldn't connect to $remote_host:$remote_port : $@\n";
                                               
                                           
                                               


0
targetddc
Asked:
targetddc
1 Solution
 
ozoCommented:
#!/usr/bin/perl -w

my $remote_host="127.0.0.1";
my $remote_port=9999;
use strict;
use warnings;
use Socket;
socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('udp'))   || die "socket: $!";
bind(SOCKET, sockaddr_in($remote_port, inet_aton($remote_host))) || die "bind: $!";
if( fork() == 0 ){
  $SIG{TERM}=$SIG{PIPE}=\&cleanup;

  select((select(SOCKET),$|=1)[0]);

  while( 1 ){
    print STDERR $! unless send(SOCKET,".",0,sockaddr_in($remote_port, inet_aton($remote_host)));
    sleep 1;
  }
  sub cleanup{
    close SOCKET;
    exit;
  }
  exit;
}
$|=1;
print $_||$! while recv(SOCKET,$_,1,0);

0
 
targetddcAuthor Commented:
ozo,
good show.  Curious as to if this can be written using a server/client pair?  Perhaps I'm stating the question wrong.  Is there a way to write a server program and a client program?
Regards,
tddc
Thanks for your input!!!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now