problem to setup a socket

L

Lim kiang Leng

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);
 
P

Paul Lalli

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
 
P

Paul Lalli

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
 
G

greger

Lim kiang Leng said:
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
 

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,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top