Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Not able to connect socket ....

Reply
Thread Tools

Not able to connect socket ....

 
 
Swanand.Kakade@gmail.com
Guest
Posts: n/a
 
      08-18-2006
This is the script i am useing as SERVER

# server0.pl
#--------------------

use strict;
use Socket;

# use port 7890 as default
my $host = shift || '10.255.37.164';
my $port = shift || 7890;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";

# grab a port on this machine
my $paddr = sockaddr_in($port, gethostbyname('proddev'));
#my $paddr = sockaddr_in($port,inet_aton('proddev'));

# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port ";

# accepting a connection
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER))
{
# find out who connected
my ($client_port, $client_ip) = sockaddr_in($client_addr);
my $client_ipnum = inet_ntoa($client_ip);
my $client_host = gethostbyaddr($client_ip, AF_INET);
# print who has connected
print "got a connection from: $client_host","[$client_ipnum] ";
# send them a message, close connection
print CLIENT "Smile from the server";
close CLIENT;
}

When i tries to run this script ...
it shows following error ...

bind: Unknown error at E:\QTP\Perl\server.pl line 21.


Client script that i am using is...

# client1.pl - a simple client
#----------------

use strict;
use Socket;

# initialize host and port
my $host = shift || '10.255.37.164';
my $port = shift || 7890;

my $proto = getprotobyname('tcp');

# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";

my $line;
while ($line = "")
{
print $line;
}
close SOCKET or die "close: $!";


What I want to achieve is here server will sent some message and
client will read and pass this message... to another server for
processing.

Pls can any one tell me what is that error " bind: Unknown error at
E:\QTP\Perl\server.pl line 21. "
What need to do for this?

pls reply as soon as possible u can contact me http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      08-18-2006

<(E-Mail Removed)> wrote in message
..
..
>
> Pls can any one tell me what is that error " bind: Unknown error


I'm not sure - I usually use IO::Socket instead of Socket for this sort of
thing. I think it might be because the second argument to sockaddr_in() is
inappropriate.

Here's a server script and a client script, based on what you provided and
on code from the Perl Cookbook, that do work for me (on Windows 2000).
Perhaps you can build on them to do what you want. (They run from different
consoles on localhost.):

server.pl
------------
use strict;
use warnings;
use Socket;

# use port 7890 as default
my $host = shift || 'localhost';
my $port = shift || 7890;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";

# grab a port on this machine
my $paddr = sockaddr_in($port, INADDR_ANY);
#my $paddr = sockaddr_in($port,inet_aton('proddev'));

# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port\n";

while (accept(CLIENT, SERVER)) {
print "Got a connection which said:\n";
my $got = <CLIENT>;
print $got, "\n";
}
-----------

client.pl
-----------
use strict;
use warnings;
use Socket;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 7890;

my $proto = getprotobyname('tcp');

# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";

my $line = "Hello there!!\n";

print SOCKET $line;

close SOCKET or die "close: $!";
---------

The server script runs continuously. The client script connects to the
server, then exits.

Cheers,
Rob


 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      08-18-2006

<(E-Mail Removed)> wrote in message
..
..
>
> Pls can any one tell me what is that error " bind: Unknown error


I'm not sure - I usually use IO::Socket instead of Socket for this sort of
thing. I think it might be because the second argument to sockaddr_in() is
inappropriate.

Here's a server script and a client script, based on what you provided and
on code from the Perl Cookbook, that do work for me (on Windows 2000).
Perhaps you can build on them to do what you want. (They run from different
consoles on localhost.):

server.pl
------------
use strict;
use warnings;
use Socket;

# use port 7890 as default
my $host = shift || 'localhost';
my $port = shift || 7890;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";

# grab a port on this machine
my $paddr = sockaddr_in($port, INADDR_ANY);
#my $paddr = sockaddr_in($port,inet_aton('proddev'));

# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port\n";

while (accept(CLIENT, SERVER)) {
print "Got a connection which said:\n";
my $got = <CLIENT>;
print $got, "\n";
}
-----------

client.pl
-----------
use strict;
use warnings;
use Socket;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 7890;

my $proto = getprotobyname('tcp');

# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";

my $line = "Hello there!!\n";

print SOCKET $line;

close SOCKET or die "close: $!";
---------

The server script runs continuously. The client script connects to the
server, then exits.

Cheers,
Rob



 
Reply With Quote
 
Swanand.Kakade@gmail.com
Guest
Posts: n/a
 
      08-18-2006

But i want connect to specific ip address and specific port...
INADDR_ANY connects to 0.0.0.0: 0(port) ....
which will work at any time?
but how can i connect to specific and send and receive messages...

Is there any another way to do this ?
pls help me out.....


Sisyphus wrote:
> <(E-Mail Removed)> wrote in message
> .
> .
> >
> > Pls can any one tell me what is that error " bind: Unknown error

>
> I'm not sure - I usually use IO::Socket instead of Socket for this sort of
> thing. I think it might be because the second argument to sockaddr_in() is
> inappropriate.
>
> Here's a server script and a client script, based on what you provided and
> on code from the Perl Cookbook, that do work for me (on Windows 2000).
> Perhaps you can build on them to do what you want. (They run from different
> consoles on localhost.):
>
> server.pl
> ------------
> use strict;
> use warnings;
> use Socket;
>
> # use port 7890 as default
> my $host = shift || 'localhost';
> my $port = shift || 7890;
> my $proto = getprotobyname('tcp');
>
> # create a socket, make it reusable
> socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
> setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
>
> # grab a port on this machine
> my $paddr = sockaddr_in($port, INADDR_ANY);
> #my $paddr = sockaddr_in($port,inet_aton('proddev'));
>
> # bind to a port, then listen
> bind(SERVER, $paddr) or die "bind: $!";
> listen(SERVER, SOMAXCONN) or die "listen: $!";
> print "SERVER started on port $port\n";
>
> while (accept(CLIENT, SERVER)) {
> print "Got a connection which said:\n";
> my $got = <CLIENT>;
> print $got, "\n";
> }
> -----------
>
> client.pl
> -----------
> use strict;
> use warnings;
> use Socket;
>
> # initialize host and port
> my $host = shift || 'localhost';
> my $port = shift || 7890;
>
> my $proto = getprotobyname('tcp');
>
> # get the port address
> my $iaddr = inet_aton($host);
> my $paddr = sockaddr_in($port, $iaddr);
> # create the socket, connect to the port
> socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
> or die "socket: $!";
> connect(SOCKET, $paddr) or die "connect: $!";
>
> my $line = "Hello there!!\n";
>
> print SOCKET $line;
>
> close SOCKET or die "close: $!";
> ---------
>
> The server script runs continuously. The client script connects to the
> server, then exits.
>
> Cheers,
> Rob


 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      08-18-2006
(E-Mail Removed) wrote:

> my $paddr = sockaddr_in($port, gethostbyname('proddev'));


You are calling gethostbyname() in a list context.

I think you meant to say.

my $paddr = sockaddr_in($port, scalar(gethostbyname('proddev')));

> bind(SERVER, $paddr) or die "bind: $!";
> listen(SERVER, SOMAXCONN) or die "listen: $!";


Anyhow, is there any reason you listen on a specific IP address rather
than just INADDR_ANY?

 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      08-18-2006

"Brian McCauley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> (E-Mail Removed) wrote:
>
> > my $paddr = sockaddr_in($port, gethostbyname('proddev'));

>
> You are calling gethostbyname() in a list context.
>
> I think you meant to say.
>
> my $paddr = sockaddr_in($port, scalar(gethostbyname('proddev')));
>
> > bind(SERVER, $paddr) or die "bind: $!";
> > listen(SERVER, SOMAXCONN) or die "listen: $!";

>
> Anyhow, is there any reason you listen on a specific IP address rather
> than just INADDR_ANY?
>


Mind you ... I *do* have a local network, and I placed the server script
that I posted, on 192.168.0.3, amending it so that 'my $host =
'192.168.0.3';'. That was the only change I made to the server script.

I changed the client script (running on 192.168.0.1) so that $host =
'192.168.0.3';'. That was the only change that I made to the client script
that I posted.

The server script still runs fine ... it just sits there waiting for a
connection. But whenever I run the client script it dies with "connect:
Unknown error at client.pl line 21", for reasons that I don't understand.

I assume it dies because Socket.pm is basically a heap of **** ... but
that's unlikely to be a correct assumption

Anyway ... I'm just thankful that the socket scripts I use (using
IO::Socket) work without *any* aggravation.

Cheers,
Rob


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-18-2006
>>>>> "SK" == Swanand Kakade <(E-Mail Removed)> writes:

SK> But i want connect to specific ip address and specific port...
SK> INADDR_ANY connects to 0.0.0.0: 0(port) ....
SK> which will work at any time?
SK> but how can i connect to specific and send and receive messages...

first off, don't top post. read the group guidelines which are posted
regularly.

as others have said, you should use IO::Socket as it will clean up your
code and make your life easier.

and finally, you say connect but you use a listen call which is used for
servers waiting for others to connect to. this means you are confused
about what socket calls to make for what purpose. bind/listen are meant
for servers. for client connections you need the connect call. or in
IO::Socket you just need the new() constructor with either a single
hostort string or a list of key/values. it can be used to do a client
connect or setup a listen socket for a server but not both.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not able to display color in excel not able to display color in excel using xml sed_y XML 0 02-15-2012 09:46 PM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
I CAN connect socket to any localhost port but I shouldn't be able to qvx Python 2 07-30-2008 07:02 AM
Aliant TV - Trying to connect a wireless router, not able to connect to Internet rich irving Computer Support 5 01-11-2006 06:30 PM



Advertisments