Why does my perl server stop working when I hit 'enter'?
I have created a server in perl that sends messages or commands to the client. I can send commands just fine, but when I am prompted to enter a command on my server I created, if I hit 'enter' the server messes up. Why is this happening?
Here is a portion of my code:
print "\ nConnection received from IP address $ peer_address on port $ peer_port"; $ closed_message = "\ n \ tConfident client session ...";
while (1)
{
print "\nCommand: ";
$send_data = <STDIN>;
chop($send_data);
if ($send_data eq 'e' or $send_data eq 'E' or $send_data eq ' E' or $send_data eq ' E ' or $send_data eq 'E ' or $send_data eq ' e' or $send_data eq ' e ' or $send_data eq 'e')
{
$client_socket->send ($send_data);
close $client_socket;
print "$closed_message\n";
&options;
}
else
{
$client_socket->send($send_data);
}
$client_socket->recv($recieved_data,8000);
print "\nRecieved: $recieved_data";
}
}
a source to share
Your server is blocked when called $client_socket->recv(...)
- the server and client are deadlocked, each waiting for the other to speak.
Try putting this line after chop()
:
next unless length $send_data; # restart the loop if no command submitted
Now, after reworking your example, here's what I'm thinking:
$send_data = <STDIN>; # $send_data := "\n"
# you just input a blank line with [ENTER]
chop($send_data); # $send_data := ""
$client_socket->send($send_data) # you send a zero-length buffer
# On my system, this does generate a syscall for
# the sender, but no data is transmitted
$client_socket->recv($buf, 8192) # Hang indefinitely. Your client application
# received no command, and so it has sent no
# response.
This is just a guess. As @DVK commented, we don't actually know your symptoms, and it's hard to guess from your description what's going on. However, this seems to be a problem I have bitten in the past.
a source to share