Socket troubles

H

Harshavardhan

Hi,

I am trying to create two sender and listener sockets using perl.

***************
Listener.pl
***************

#! /usr/bin/perl

use IO::Socket;
$sock = new IO::Socket::INET ( LocalHost => 'localhost',
Local Port => 1200,
Proto = 'tcp',
Listen = 5,
Reuse = 1);

die "Socket could not be created. Reason: $!" unless $sock;

while ( $new_sock = $sock->accept() ) {
while (defined ($buf = <$sock>)) {
print $buf;
}
}

close ($sock)

***********************
Sender.pl
***********************

#! /usr/bin/perl

use IO::Socket;
$sock = new IO::Socket::INET ( PeerAddr => 'localhost',
PeerPort => 1200,
Proto = 'tcp',
);

die "Socket could not be created. Reason: $!" unless $sock;

foreach (1 .. 10) {
print $sock "Msg $_: How are you? \n";
}

close ($sock)

***********************

When run the Listener, it waits for new connections, but when I run the
sender.pl, it just finishes without any output at the listener. I get no
error messages.

Any ideas on whats happening?

Thanks
harsha

when I run hostname on my computer, it returns localhost.localdomain. I am
running both the sender and the listener on my computer. I am using Perl
5.8.0
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,

I am trying to create two sender and listener sockets using perl.

***************
Listener.pl
***************

#! /usr/bin/perl

use IO::Socket;
$sock = new IO::Socket::INET ( LocalHost => 'localhost',
Local Port => 1200,
^^^^^^^^^^
This is not syntatically-correct code
Proto = 'tcp',
^^^
This is an illegal modification of a constant, again not syntatically-correct.
Listen = 5,
^^^
So is this one.
Reuse = 1);
^^^
And this one.

Do NOT re-type Perl code when posting to usenet. Use copy+paste or your newsreader's "import" function.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/Z8HIeS99pGMif6wRAkyeAJ9y5BH/bSkOAmCuVBwdvH5qG/jkygCglxck
wYUhYEqCrcMkrmL/InDLI6o=
=E+CH
-----END PGP SIGNATURE-----
 
H

Harshavardhan

Thanks guys.

After I used

use strict;
use warnings;

and after specifying each variable with its package name I found out the
error and now my program is working. Next time I post I will remember to
cut and paste the code.

Thanks for all your help
harsha
 
U

Uri Guttman

H> After I used

H> use strict;
H> use warnings;

H> and after specifying each variable with its package name I found out the
H> error and now my program is working. Next time I post I will remember to
H> cut and paste the code.

first off, don't top post.

secondly, that is the wrong way to work with strict. the correct way is
to use my. what you did is create package globals and that is not good
practice.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top