Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > problem to setup a socket

Reply
Thread Tools

problem to setup a socket

 
 
Lim kiang Leng
Guest
Posts: n/a
 
      06-24-2004
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);
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-24-2004
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);
>

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-24-2004
On Thu, 24 Jun 2004, greger wrote:

> "Lim kiang Leng" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>
> > 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
 
Reply With Quote
 
greger
Guest
Posts: n/a
 
      06-24-2004

"Lim kiang Leng" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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);



 
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
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
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



Advertisments