Not able to connect socket ....

S

Swanand.Kakade

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 (e-mail address removed)
 
S

Sisyphus

..
..
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
 
S

Sisyphus

..
..
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
 
S

Swanand.Kakade

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.....
 
B

Brian McCauley

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?
 
S

Sisyphus

Brian McCauley said:
You are calling gethostbyname() in a list context.

I think you meant to say.

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


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 shit ... 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
 
U

Uri Guttman

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
host:port 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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top