UDP Sockets

Hello there,

I am trying to make a program in perl that can basically send data to an IP address. I don't need to connect, so I want to use UDP.
If you know where I can find a tutorial for this it would be very much appreciated.
Could you give me some code that would send the string $aaaa to 206.45.35.12
Thanks...
SunilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SunilAuthor Commented:
Adjusted points to 240
0
icdCommented:
To send a string to 206.45.35.12 you need more than just the address. When you connect to
206.45.35.12, you have to specify that you want to talk to the FTP server (for example) this is done through assigned port numbers.

You can find a very good tutorial at:-

http://spectral.mscs.mu.edu/NetworksClass/Materials/tcptut3.html

0
icdCommented:
Since you gave not reason for the rejection I have to make another stab in the dark :-)

--- cut here ---
#!/usr/bin/perl

$aaaa = "this is your string";
($them,$port) =@ARGV;
$port=2345 unless $port;
$them='localhost' unless $them;

$AF_INET=2;
$SOCK_STREAM=1;

$SIG{'INT'}= 'dokill';
sub dokill {
      kill 9,$child if $child;
}

$sockaddr ='S n a4 x8';
chop($hostname=`hostname`);
($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp') unless $port =~ /^\d+$/;;
($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);

$this=pack($sockaddr, $AF_INET, 0, $thisaddr);
$that=pack($sockaddr, $AF_INET, $port, $thataddr);

if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
      print "socket ok\n";
} else {
      die $!;
}

if (bind(S, $this)) {
      print "bind OK\n";
} else {
      die $!;
}

if (connect(S,$that)) {
      print "connect OK\n";
} else {
      die $!;
}

select(S); $|=1; select(STDOUT);

print S "$aaaa";
close (S);
--- end ---

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

SunilAuthor Commented:
Sorry to be so picky but I really need it to work.
The example you have shown me does not include the IP address that I want to send it to.

Also, I need to use UDP because I will not be able to connect to the remote machine. It just has to send automatically.
E.g. suppose my next door neighbour is on the net. This program should just bomb his or hers IP address with data. I know you can do this with UDP.

The site you told me didn't have any perl examples.

Hope you can help.
0
icdCommented:
OK. Third and last try. This will connect to port 80 (http port)
or you could try other ports (e.g. mail port 25) or ftp (port 21)

--- cut here ---
#!/usr/bin/perl

$port=80;

($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp') unless $port =~ /^\d+$/;;

$sockaddr ='S n a4 x8';
$thisaddr = pack('C4', 127,0,0,1);
$thataddr = pack('C4', 206,45,35,12);

$this=pack($sockaddr, 2, 0, $thisaddr);
$that=pack($sockaddr, 2, $port, $thataddr);

if (socket(S, 2, 1, $proto)) {
      print "socket ok\n";
} else {
      die $!;
}

if (bind(S, $this)) {
      print "bind OK\n";
} else {
      die $!;
}

if (connect(S,$that)) {
      print "connect OK\n";
} else {
      die $!;
}

select(S); $|=1; select(STDOUT);

for (1...10000) {
      print S "I am going to try and crash your server!";
}
close (S);
--- end ---
0
SunilAuthor Commented:
Sorry.

It didn't work.
I need to use UDP anyway. It has to send datagrams and not connect.

0
icdCommented:
I just found an example UDP message passing program that will probably be closer to what you want.

Look at

http://www.perl.com/perl/all_about/ipc/udp.html


0
SunilAuthor Commented:
Sorry but it is no good giving me a a lump of code and not telling me how to use it. My knowledge of Perl is not extensive so i need a detailed explanation of each line.

Hope you can help me soon.
0
shawniCommented:

This requires a recent perl, preferably 5.004.  If you don't
have a recent perl, you might be able to get by with 5.003+
and go install the IO module from CPAN (I use ftp.cdrom.com/pub/
perl/CPAN).

Doing this with an older perl is possible, just a lot more
involved.

#!/usr/bin/perl

use IO::Socket;  # this is why it has to be recent

$aaaa = "TEST";  # data to send
$flags = 0;      # you might want MSG_OOB or such later
$udp_port = 7;   # destination UDP port (you didn't say, so
                 # we'll use the echo port

# create a UDP socket, with the endpoint set ("connected").
$socket=IO::Socket::INET->new(PeerAddr=>"206.45.35.12:$udp_port",
     Proto=>"udp");

# send one packet.
send $socket,$aaaa,$flags;

# send another.
send $socket,"Hi there!",$flags;

exit(0);

0
SunilAuthor Commented:
Hi.

Thank you so much for helping me out with this problem.
I was just wondering whether there is any way that I can test to see whether it works or not?

Hope to hear from you soon.

Thanks.

0
shawniCommented:

I usually use tcpdump, but this program will receive a single
UDP packet so you can test the other program.  Run this in
one window, and run the sending program in another window.
This program will receive the packet and print the contents.

You'll also have to change the port number/host IP address
in the UDP packet sending program to point at your testing
host and testing port number.

For more documentation on how these functions work, use

perldoc IO::Socket

..on any machine that this program runs on.



#!/usr/local/bin/perl
use IO::Socket;

$aaaa = "";  # these have the same meaning as the other
$len = 100;  # program, $len is maximum length of packet you
             # wish to receive
$port = 10000;  # local service port.  Remember to change the
                # port number in the other program too!
$flags = 0;

$sock = IO::Socket::INET->new(LocalPort=>$port,
        Proto=>"udp");

recv $sock,$aaaa,$len,$flags;

print $aaaa,"\n";

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SunilAuthor Commented:
Everything works fine.
Thank you so much for giving me the code.
I am so grateful.
If you know any good perl resource sites for beginers could you please tell me.
Thanks.



0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.