problem to setup a socket

Discussion in 'Perl Misc' started by Lim kiang Leng, Jun 24, 2004.

  1. I am new to try up the socket programming, I try to set up a socket in
    my company but seem like it failed. Althught , it know the present of
    the server, but seem like they cant talk. Below is my server and
    client's program. Is any one know why i failed to communicate?


    ########
    #server#
    ########
    print "prgram is in running ... \n" ;
    use IO::Socket;
    my $sock = new IO::Socket::INET (
    LocalHost => 'machine.mycompany.com',
    LocalPort => '7000',
    Proto => 'tcp',
    Listen => 1,
    Reuse => 1,
    );
    die "Could not create socket: $!\n" unless $sock;
    my $new_sock = $sock->accept();
    print "$_" ;
    while(defined(<$new_sock>)) {
    print $_;
    }
    close($sock);




    ########
    #client#
    ########

    use IO::Socket;
    print "programming " ;
    my $sock = new IO::Socket::INET (
    PeerAddr =>
    'machine.mycompany.com',
    PeerPort => '7000',
    Proto => 'tcp',
    );
    die "Could not create socket: $!\n" unless $sock;
    print $sock "Hello there!\n";
    close($sock);
    Lim kiang Leng, Jun 24, 2004
    #1
    1. Advertising

  2. Lim kiang Leng

    Paul Lalli Guest

    On Thu, 24 Jun 2004, Lim kiang Leng wrote:

    > I am new to try up the socket programming, I try to set up a socket in
    > my company but seem like it failed. Althught , it know the present of
    > the server, but seem like they cant talk. Below is my server and
    > client's program. Is any one know why i failed to communicate?
    >
    >
    > ########
    > #server#
    > ########
    > print "prgram is in running ... \n" ;
    > use IO::Socket;
    > my $sock = new IO::Socket::INET (
    > LocalHost => 'machine.mycompany.com',
    > LocalPort => '7000',
    > Proto => 'tcp',
    > Listen => 1,
    > Reuse => 1,
    > );
    > die "Could not create socket: $!\n" unless $sock;
    > my $new_sock = $sock->accept();
    > print "$_" ;
    > while(defined(<$new_sock>)) {
    > print $_;
    > }


    I don't know if this is your only problem, but it's at least one. There
    is special magic involved when a <$file> construct is the only thing in
    the conditional of the while loop. But the catch is that it must be the
    ONLY thing. The magic is that perl translates this:
    while (<$file>) { ... }
    to this:
    while (defined($_ = <$file>)) { ... }

    This does not happen in any other case. It does not happen when you add
    anything else, including a call to defined() in the conditional. What's
    happening here is that $_ is never getting assigned. If you had enabled
    warnings, you would likely see an uninitialized value warning for the
    `print $_` line.

    Try changing the while loop to:
    while (<$new_sock>) {
    print;
    }

    and see if that changes anything. If not, there are other problems with
    the code as well.

    Paul Lalli



    > close($sock);
    >
    >
    >
    >
    > ########
    > #client#
    > ########
    >
    > use IO::Socket;
    > print "programming " ;
    > my $sock = new IO::Socket::INET (
    > PeerAddr =>
    > 'machine.mycompany.com',
    > PeerPort => '7000',
    > Proto => 'tcp',
    > );
    > die "Could not create socket: $!\n" unless $sock;
    > print $sock "Hello there!\n";
    > close($sock);
    >
    Paul Lalli, Jun 24, 2004
    #2
    1. Advertising

  3. Lim kiang Leng

    Paul Lalli Guest

    On Thu, 24 Jun 2004, greger wrote:

    > "Lim kiang Leng" <> wrote in message
    > news:...
    >
    > > while(defined(<$new_sock>)) {
    > > print $_;
    > > }
    > > close($sock);
    > >

    > you probably need to have a look at reading from a socket, instead of
    > checking if the socket is defined.
    > check out:
    > http://www.infocopter.com/perl/socket-server.htm


    That doesn't check if the socket is defined. It checks if the return
    value from <$new_sock> is defined. That return value is then thrown away,
    since the 'magic' of the while loop was not invoked to auto-assign it to
    $_.

    Paul Lalli
    Paul Lalli, Jun 24, 2004
    #3
  4. Lim kiang Leng

    greger Guest

    "Lim kiang Leng" <> wrote in message
    news:...
    > I am new to try up the socket programming, I try to set up a socket in
    > my company but seem like it failed. Althught , it know the present of
    > the server, but seem like they cant talk. Below is my server and
    > client's program. Is any one know why i failed to communicate?
    >
    >
    > ########
    > #server#
    > ########
    > print "prgram is in running ... \n" ;
    > use IO::Socket;
    > my $sock = new IO::Socket::INET (
    > LocalHost => 'machine.mycompany.com',
    > LocalPort => '7000',
    > Proto => 'tcp',
    > Listen => 1,
    > Reuse => 1,
    > );
    > die "Could not create socket: $!\n" unless $sock;
    > my $new_sock = $sock->accept();
    > print "$_" ;
    > while(defined(<$new_sock>)) {
    > print $_;
    > }
    > close($sock);
    >

    you probably need to have a look at reading from a socket, instead of
    checking if the socket is defined.
    check out:
    http://www.infocopter.com/perl/socket-server.htm
    best R
    /G
    --
    www.gh-webinteractive.com
    >
    >
    >
    > ########
    > #client#
    > ########
    >
    > use IO::Socket;
    > print "programming " ;
    > my $sock = new IO::Socket::INET (
    > PeerAddr =>
    > 'machine.mycompany.com',
    > PeerPort => '7000',
    > Proto => 'tcp',
    > );
    > die "Could not create socket: $!\n" unless $sock;
    > print $sock "Hello there!\n";
    > close($sock);
    greger, Jun 24, 2004
    #4
    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. Laszlo Nagy
    Replies:
    1
    Views:
    4,753
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    940
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    524
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    641
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    696
Loading...

Share This Page