Not able to connect socket ....

Discussion in 'Perl Misc' started by Swanand.Kakade@gmail.com, Aug 18, 2006.

  1. Guest

    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
    , Aug 18, 2006
    #1
    1. Advertising

  2. Sisyphus Guest

    <> 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
    Sisyphus, Aug 18, 2006
    #2
    1. Advertising

  3. Sisyphus Guest

    <> 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
    Sisyphus, Aug 18, 2006
    #3
  4. Guest

    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:
    > <> 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
    , Aug 18, 2006
    #4
  5. 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?
    Brian McCauley, Aug 18, 2006
    #5
  6. Sisyphus Guest

    "Brian McCauley" <> wrote in message
    news:...
    > 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 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
    Sisyphus, Aug 18, 2006
    #6
  7. Uri Guttman Guest

    >>>>> "SK" == Swanand Kakade <> 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
    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Aug 18, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yoshitha

    not able to connect to sqlserver

    yoshitha, Jan 5, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    423
    yoshitha
    Jan 5, 2006
  2. qvx
    Replies:
    2
    Views:
    332
  3. sed_y
    Replies:
    0
    Views:
    999
    sed_y
    Feb 15, 2012
  4. Replies:
    5
    Views:
    254
  5. HP

    not able to connect to mysql

    HP, Jan 6, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    152
Loading...

Share This Page