Solved

UDP Sockets

Posted on 1997-06-18
12
603 Views
Last Modified: 2008-03-10
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...
0
Comment
Question by:Sunil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 

Author Comment

by:Sunil
ID: 1204301
Adjusted points to 240
0
 
LVL 5

Expert Comment

by:icd
ID: 1204302
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
 
LVL 5

Expert Comment

by:icd
ID: 1204303
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Sunil
ID: 1204304
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
 
LVL 5

Expert Comment

by:icd
ID: 1204305
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
 

Author Comment

by:Sunil
ID: 1204306
Sorry.

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

0
 
LVL 5

Expert Comment

by:icd
ID: 1204307
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
 

Author Comment

by:Sunil
ID: 1204308
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
 

Expert Comment

by:shawni
ID: 1204309

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
 

Author Comment

by:Sunil
ID: 1204310
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
 

Accepted Solution

by:
shawni earned 240 total points
ID: 1204311

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
 

Author Comment

by:Sunil
ID: 1204312
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question