CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
big problem with perl sockets

 



some_random_kid
Novice

Jan 2, 2010, 12:25 AM

Post #1 of 15 (2270 views)
big problem with perl sockets Can't Post

I'm just going to start this off by saying, this is not a syntax problem. There is something wrong with perl on my computer. I built a new computer, installed windows xp on it and have had it for about a year. I decided to start working with perl again as its really useful. I made my own socket server that takes input and just echoes it back to the client. I made a client that takes keyboard input and sends it to the socket and concurrently using fork, receives from the socket. The relationship wasn't working. For some reason the scripts weren't working. I tried to figure it out, maybe i was forgetting something stupid. Then I tried looking around, and decided to try connecting to my server via the telnet program on my computer. Telnet worked 100%! Now, that is a WTF moment. I now narrowed it down to my client. I decided to copy a perl script off the internet thats supposed to be proven to work and works exactly like telnet but uses IO::Socket instead. The script DID NOT WORK! That was really shocking. A entire new problem has opened up. I tried opening up ports, see if my internet was blocking it, and nothing seemed to be working. So now I am here with questions for this problem. What the heck is going on! Even a fresh installation of perl does not make a difference.

The symptoms of the problem are when I type in input, the program all of a sudden stops and just sits there doing nothing. No sign of server or client responses. When I make it so the client only sends and does not recieve it seems to work. But not when both are happening, and its not syntax thats the problem.


FishMonger
Veteran / Moderator

Jan 2, 2010, 5:01 AM

Post #2 of 15 (2260 views)
Re: [some_random_kid] big problem with perl sockets [In reply to] Can't Post

How do you know it's not a syntax issue?

If it's not syntax, it's a logic issue.

Without seeing your code it's utterly impossible for anyone to say how to fix the problem.


some_random_kid
Novice

Jan 2, 2010, 12:47 PM

Post #3 of 15 (2243 views)
Re: [FishMonger] big problem with perl sockets [In reply to] Can't Post

Like I said, the source code is fine. Its a problem with the environment for perl. Telnet works with the server fine, i've tried many scripts that are supposed to work exactly like telnet and were proven to work, none of them are actually working. Which means it cannot be a source code problem. I can post up my source but it is a waste. I'm actually considering going back to an earlier version now if this continues to be a problem. I'm not going to waste more than two weeks trying to figure this out, so if that doesn't work, then i'm giving up on this computer.


savo
User

Jan 2, 2010, 1:00 PM

Post #4 of 15 (2240 views)
Re: [some_random_kid] big problem with perl sockets [In reply to] Can't Post

With the source it can be tested by others which would prove or disprove its a problem with perl.


some_random_kid
Novice

Jan 2, 2010, 1:52 PM

Post #5 of 15 (2235 views)
Re: [savo] big problem with perl sockets [In reply to] Can't Post

CLIENT:


Code
  

#!/usr/bin/perl -w
use strict;
use IO::Socket;

my ( $host, $port, $kidpid, $handle, $line );

( $host, $port ) = ('localhost',9000);


# create a tcp connection to the specified host and port
$handle = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $host,
PeerPort => $port
)
or die "can't connect to port $port on $host: $!";
print STDERR "[Connected to $host:$port]\n";

# split the program into two processes, identical twins
die "can't fork: $!" unless defined( $kidpid = fork() );

# the if{} block runs only in the parent process
if ($kidpid) {

# copy the socket to standard output
while ($line = <$handle>) {
print STDOUT $line;
}
kill( "TERM", $kidpid ); # send SIGTERM to child
}

# the else{} block runs only in the child process
else {

# copy standard input to the socket
while ( defined( $line = <STDIN> ) ) {
print $handle "$line";
}
}



SERVER:


Code
  

#!/usr/bin/perl -w
use IO::Socket;


$PORT = 9000; # pick something not in use

$server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1);
die "can't setup server" unless $server;

while ($client = $server->accept()) {
while ( <$client>) {
print "$_";
print $client "$_";
}
}



savo
User

Jan 2, 2010, 2:36 PM

Post #6 of 15 (2232 views)
Re: [some_random_kid] big problem with perl sockets [In reply to] Can't Post

All i can say is that it works fine here

This is perl, v5.10.1 (*) built for i486-linux-thread-multi


FishMonger
Veteran / Moderator

Jan 2, 2010, 10:00 PM

Post #7 of 15 (2213 views)
Re: [some_random_kid] big problem with perl sockets [In reply to] Can't Post

That code is from `perldoc perlipc` and does work.

Here's a (slightly modified) version from "Network Programming with Perl by Lincon Stein".

server.pl

Code
#!/usr/bin/perl 

use strict;
use warnings;
use IO::Socket;
use constant ECHO_PORT => 9000;

my ($bytes_out, $bytes_in, $quit) = (0,0,0);

$SIG{INT} = sub { $quit++ };

my $port = shift || ECHO_PORT;

my $sock = IO::Socket::INET->new(Listen => 20,
LocalPort => $port,
Timeout => 60,
Reuse => 1,
) or die "Can't create listening socket: $!\n";

warn "waiting for connections on port $port...\n";

while ( !$quit ) {
next unless my $session = $sock->accept;

my $peer = gethostbyaddr($session->peeraddr, AF_INET) || $session->peerhost;
my $port = $session->peerport;

warn "Connection from [$peer, $port]\n";

while ( <$session> ) {
$bytes_in += length($_);
$bytes_out += length($_);
print "client $peer sent '$_'";
print $session $_;
$quit++ if $bytes_in eq 'q';
}
warn "Connection from [$peer, $port] finished\n";
close $session;
}
warn "bytes_received = $bytes_in, bytes_sent = $bytes_out\n";
close $sock or warn $@;


client.pl

Code
#!/usr/bin/perl 

use strict;
use warnings;
use IO::Socket;

my ($bytes_out, $bytes_in) = (0,0);
my $host = shift || 'localhost';
my $port = shift || 9000;

my $socket = IO::Socket::INET->new( Proto => 'tcp',
PeerAddr => $host,
PeerPort => $port,
) or die "can't connect to port $port on $host: $!";

$socket->autoflush(1);

while ( my $msg_out = STDIN->getline ) {
last if $msg_out =~ /^q/i;
print $socket $msg_out;

my $msg_in = <$socket>;
print $msg_in;

$bytes_out += length($msg_out);
$bytes_in += length($msg_in);
}

$socket->close or warn $@;
print "bytes_sent = $bytes_out, bytes_received = $bytes_in\n";


If that doesn't work for you, then you'll need to provide more details, such as:

1) A copy/paste of both the server and client side output.

2) What version and distro of perl are you using?

3) Are you able to run other scripts?

3) Is your firewall enabled?

4) What AV software are you using?

5) Are both scripts on the same LAN?

6) Are both scripts on the same host?


some_random_kid
Novice

Jan 3, 2010, 2:07 AM

Post #8 of 15 (2198 views)
Re: [FishMonger] big problem with perl sockets [In reply to] Can't Post

Interesting, when i'm activating those scripts from the cmd line they only activate for a flash of time then stop. Is that normal? I'm assuming its not.

Well, here are the things that have changed with my new system (i'm running these on the same computer, i have a network running providing me internet, telnet works with my original server)

1. Now running a dual core

2. New network and router (Not sure if I had the same ISP at that time, went from dsl to comcast)

3. New, more powerful graphics card and drivers

4. Disabled WMP300N process as it was using too much cpu (shouldn't matter because all its supposed to do is monitor)

5. Got a new USB mouse

6. Got a new motherboard/harddrive/dvddrive/ram

7. Installed new games (ArmA2, ArmA1, CC3, Civ4....)

Some things that are no different...

1. Unblocked perl for port access

2. Opened up 9000 manually to see if it worked (it didn't lol)

3. Freshly installed perl twice


some_random_kid
Novice

Jan 3, 2010, 2:09 AM

Post #9 of 15 (2197 views)
Re: [FishMonger] big problem with perl sockets [In reply to] Can't Post


In Reply To
That code is from `perldoc perlipc` and does work.

Here's a (slightly modified) version from "Network Programming with Perl by Lincon Stein".
****************************************************************** ******************************************************************
If that doesn't work for you, then you'll need to provide more details, such as:

1) A copy/paste of both the server and client side output.

2) What version and distro of perl are you using?

3) Are you able to run other scripts?

3) Is your firewall enabled?

4) What AV software are you using?

5) Are both scripts on the same LAN?

6) Are both scripts on the same host?



Oh yeah, and all of the perl scripts work just fine. I have not, however, done much testing with the fork function in non socket scripts. Could the problem on my computer have to do with fork()?


FishMonger
Veteran / Moderator

Jan 3, 2010, 8:23 AM

Post #10 of 15 (2183 views)
Re: [some_random_kid] big problem with perl sockets [In reply to] Can't Post

Correct terminology is important when talking about computers and programing.

Exactly what do you mean by "activating those scripts"?

Are you executing the scripts from the Start->Run dialog box, or are you executing them from within a cmd prompt (some call DOS) window?

Most, if not all, of the changes to your system that you listed have nothing to do with the execution of these scripts. The only possible item would be #4.

What do you mean by "Disabled WMP300N process"? If you disable some of the functionality of your network card, then that could explain why the scripts are failing.

Items #1 and #2 in your second group tells me that the problem may be related to your firewall. For the purposes of testing, turn off your firewall and AV software.

Then open 2 cmd windows and execute the server.pl script in one, and then the client.pl script in the other and post print screens of the test results.

I'm attaching print screens of my test.
Attachments: server.bmp (51.4 KB)
  client.bmp (51.4 KB)


some_random_kid
Novice

Jan 3, 2010, 2:22 PM

Post #11 of 15 (2161 views)
Re: [FishMonger] big problem with perl sockets [In reply to] Can't Post

I'm setting the directory to the folder its in then typing Server.pl and it pops up for a flash of time then goes back like the script was only supposed to be run for a millisecond. I'm assuming thats not right though. It does the same thing when i double click the script icon as well. WMP300N is not a needed process, it is used to monitor the the connection you have. There is a common bug with it where it bounces up and down in cpu usage, so I disabled it. Feel free to research about it.



EDIT: Also, i've already tried turning off my firewall and it doesn't work either. But like I said, telnet works fine, which is the strangest part about it. I do have AVG running, its a pain in the but to disable (it likes to run in the background and restart itself), but I can completely uninstall it as it would be easier.


(This post was edited by some_random_kid on Jan 3, 2010, 2:26 PM)


FishMonger
Veteran / Moderator

Jan 3, 2010, 9:12 PM

Post #12 of 15 (2152 views)
Re: [some_random_kid] big problem with perl sockets [In reply to] Can't Post


Quote
I'm setting the directory to the folder its in then typing Server.pl and it pops up for a flash


That tells me that you're not following the directions that we provide. If you're unwilling to follow our troubleshooting directions, how do you expect use to help you?

Since the window opens and closes in a flash, then that should tell you that there was a problem and error message that you ignored. Unless/until you decide to read and troubleshoot those error messages, there's nothing that anyone of us can do to help you.


some_random_kid
Novice

Jan 4, 2010, 12:34 AM

Post #13 of 15 (2140 views)
Re: [FishMonger] big problem with perl sockets [In reply to] Can't Post

If you read my earlier post after you posted your source, i said I activated the script from the command line. I also activated it from from the folder by ICON. The command line is the cmd line. THE COMMAND PROMPT. There is no output to read either way. I'm pretty close to leaving this forum and not recommending it to anybody that wants help. This is the second time i've hit a brick wall here.



NO ERROR MESSAGES TO READ!


FishMonger
Veteran / Moderator

Jan 4, 2010, 8:10 AM

Post #14 of 15 (2133 views)
Re: [some_random_kid] big problem with perl sockets [In reply to] Can't Post

I'm sorry you feel that you're not getting the quality of help that you deserve, but all of my responses were based on your statements.

Let's try another test. Open 2 cmd windows and in both of them execute a "Hello World" script.

Code
#!/usr/bin/perl 

print "Hello World!\n\n";


Then execute server.pl in one window and client.pl in the other. If they work correctly, which based on your prior statements they probably won't, enter 1 or 2 lines of test text in the client window and then quit both scripts.

Next type out the contents of each script in their respective windows and post prints screens of both windows.

I'm attaching an example of what I'm asking you to do.
Attachments: client.bmp (59.9 KB)


kevindai
Novice

Jun 1, 2010, 5:52 PM

Post #15 of 15 (1760 views)
Re: [FishMonger] big problem with perl sockets [In reply to] Can't Post


In Reply To
That code is from `perldoc perlipc` and does work.

Here's a (slightly modified) version from "Network Programming with Perl by Lincon Stein".

server.pl

Code
#!/usr/bin/perl 

use strict;
use warnings;
use IO::Socket;
use constant ECHO_PORT => 9000;

my ($bytes_out, $bytes_in, $quit) = (0,0,0);

$SIG{INT} = sub { $quit++ };

my $port = shift || ECHO_PORT;

my $sock = IO::Socket::INET->new(Listen => 20,
LocalPort => $port,
Timeout => 60,
Reuse => 1,
) or die "Can't create listening socket: $!\n";

warn "waiting for connections on port $port...\n";

while ( !$quit ) {
next unless my $session = $sock->accept;

my $peer = gethostbyaddr($session->peeraddr, AF_INET) || $session->peerhost;
my $port = $session->peerport;

warn "Connection from [$peer, $port]\n";

while ( <$session> ) {
$bytes_in += length($_);
$bytes_out += length($_);
print "client $peer sent '$_'";
print $session $_;
$quit++ if $bytes_in eq 'q';
}
warn "Connection from [$peer, $port] finished\n";
close $session;
}
warn "bytes_received = $bytes_in, bytes_sent = $bytes_out\n";
close $sock or warn $@;


client.pl

Code
#!/usr/bin/perl 

use strict;
use warnings;
use IO::Socket;

my ($bytes_out, $bytes_in) = (0,0);
my $host = shift || 'localhost';
my $port = shift || 9000;

my $socket = IO::Socket::INET->new( Proto => 'tcp',
PeerAddr => $host,
PeerPort => $port,
) or die "can't connect to port $port on $host: $!";

$socket->autoflush(1);

while ( my $msg_out = STDIN->getline ) {
last if $msg_out =~ /^q/i;
print $socket $msg_out;

my $msg_in = <$socket>;
print $msg_in;

$bytes_out += length($msg_out);
$bytes_in += length($msg_in);
}

$socket->close or warn $@;
print "bytes_sent = $bytes_out, bytes_received = $bytes_in\n";


If that doesn't work for you, then you'll need to provide more details, such as:

1) A copy/paste of both the server and client side output.

2) What version and distro of perl are you using?

3) Are you able to run other scripts?

3) Is your firewall enabled?

4) What AV software are you using?

5) Are both scripts on the same LAN?

6) Are both scripts on the same host?

In the server code above, how do I send message to client, & in client code how do I print message from server?
thanks in advance

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives