By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

print to socket causes unexpected exit [please help]

P: 1
Hi, I have strange problem with my perl program. My program consists of two parts, first written in python and second in perl. Perl part is a server, but also uses two sockets for "talking" with gui written in python. Problem is, that from totally unknown reason perl program quits at line, where i'm trying to 'print' anything for AWAY gui socket. Here's part of my code :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. use strict; 
  3. use warnings; 
  4. use IO::Socket; 
  5. use threads; 
  6. use Thread::Queue; 
  7. use File::stat; 
  8.  
  9. my $port = $ARGV[0] || 2347; 
  10. my $awayport = $ARGV[1] || 2346; 
  11. my %clients; 
  12. my $queue = Thread::Queue -> new; 
  13.  
  14. #no zombies 
  15. $SIG{CHLD} = 'IGNORE'; 
  16.  
  17. #create the LISTEN socket 
  18. my $listen_socket = IO::Socket::INET->new(LocalPort => $port, 
  19.                                           Listen => 10, 
  20.                                           Proto => 'tcp', 
  21.                  LocalAddr => 'localhost', 
  22.                                           ReuseAddr => 1); 
  23.  
  24.  
  25. print " * Waiting for lock..\n"; 
  26. while(!open(Lockfile,"<.lock.")){} 
  27. close(Lockfile); 
  28.  
  29. # now crate socket for GUI communication, so I can write to gui, for example status, or anything else 
  30. my $guiListenSock = IO::Socket::INET->new(LocalAddr => "127.0.0.1", 
  31.                              LocalPort => 2346, 
  32.                              Proto    => "tcp", 
  33.                              Type     => SOCK_STREAM) 
  34.     or die "Couldn't Crt sock : $@\n"; 
  35.  
  36. open(LCKF,">.lock.lock.") or die("Can't create .lock.lock."); 
  37. close(LCKF); 
  38.  
  39. my $awayConn; 
  40. my $passwd; 
  41. my $login; 
  42.  
  43. warn "Server ready. Waiting for connections on $port ... \n"; 
  44.  
  45. while (my $connection = $listen_socket->accept) { 
  46.    my $child = threads->create ("myFunc", $queue, $connection, $guiListenSock, $awayConn)->detach; 
  47.  
  48. sub myFunc { 
  49.    my ($queue, $socket, $myoutSock, $awaySock) = @_; 
  50.    select($socket);$|=1; # force flushing 
  51.    select($myoutSock);$|=1; 
  52.    select(STDOUT);$|=1; 
  53.    print $myoutSock "DFJDLJLDKFJLDKFJLKDF\n"; # HERE IS A MOMENT WHEN PROGRAM SUDDENLY CRASHES WITHOUT ANY MESSAGE 
  54.         ... 
  55.         .... 
  56.         ... 
  57. }
Apr 13 '08 #1
Share this Question
Share on Google+
1 Reply


numberwhun
Expert Mod 2.5K+
P: 3,503
Hi, I have strange problem with my perl program. My program consists of two parts, first written in python and second in perl. Perl part is a server, but also uses two sockets for "talking" with gui written in python. Problem is, that from totally unknown reason perl program quits at line, where i'm trying to 'print' anything for AWAY gui socket. Here's part of my code :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. use strict; 
  3. use warnings; 
  4. use IO::Socket; 
  5. use threads; 
  6. use Thread::Queue; 
  7. use File::stat; 
  8.  
  9. my $port = $ARGV[0] || 2347; 
  10. my $awayport = $ARGV[1] || 2346; 
  11. my %clients; 
  12. my $queue = Thread::Queue -> new; 
  13.  
  14. #no zombies 
  15. $SIG{CHLD} = 'IGNORE'; 
  16.  
  17. #create the LISTEN socket 
  18. my $listen_socket = IO::Socket::INET->new(LocalPort => $port, 
  19.                                           Listen => 10, 
  20.                                           Proto => 'tcp', 
  21.                  LocalAddr => 'localhost', 
  22.                                           ReuseAddr => 1); 
  23.  
  24.  
  25. print " * Waiting for lock..\n"; 
  26. while(!open(Lockfile,"<.lock.")){} 
  27. close(Lockfile); 
  28.  
  29. # now crate socket for GUI communication, so I can write to gui, for example status, or anything else 
  30. my $guiListenSock = IO::Socket::INET->new(LocalAddr => "127.0.0.1", 
  31.                              LocalPort => 2346, 
  32.                              Proto    => "tcp", 
  33.                              Type     => SOCK_STREAM) 
  34.     or die "Couldn't Crt sock : $@\n"; 
  35.  
  36. open(LCKF,">.lock.lock.") or die("Can't create .lock.lock."); 
  37. close(LCKF); 
  38.  
  39. my $awayConn; 
  40. my $passwd; 
  41. my $login; 
  42.  
  43. warn "Server ready. Waiting for connections on $port ... \n"; 
  44.  
  45. while (my $connection = $listen_socket->accept) { 
  46.    my $child = threads->create ("myFunc", $queue, $connection, $guiListenSock, $awayConn)->detach; 
  47.  
  48. sub myFunc { 
  49.    my ($queue, $socket, $myoutSock, $awaySock) = @_; 
  50.    select($socket);$|=1; # force flushing 
  51.    select($myoutSock);$|=1; 
  52.    select(STDOUT);$|=1; 
  53.    print $myoutSock "DFJDLJLDKFJLDKFJLKDF\n"; # HERE IS A MOMENT WHEN PROGRAM SUDDENLY CRASHES WITHOUT ANY MESSAGE 
  54.         ... 
  55.         .... 
  56.         ... 
  57. }

I am not sure as I haven't done anything with sockets and such, but hopefully one of our experts will be able to guide you in the right direction.

Regards,

Jeff
Apr 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.