tags:

views:

90

answers:

1

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 being prompted for the command on my server i have created, if i press 'enter', the server messes up. Why is this happening?

Here is part of my code:

print "\nConnection recieved from IP address $peer_address on port $peer_port "; $closed_message = "\n\tTerminated 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";
}

}

+3  A: 

Your server is blocking in the call to $client_socket->recv(...) -- server and client are deadlocked, each waiting for the other to speak.

Try putting this line after your chop():

next unless length $send_data;  # restart the loop if no command submitted

Now, reworking your example, here's what I speculate is happening:

$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 speculation. As @DVK commented, we don't actually know your symptoms, and it is difficult from your description to guess what is going on. It does, however, resemble a problem I've been bitten by in the past.

pilcrow
Thanks! It worked perfectly!
David
@David, I'm glad it worked. Perhaps you might reward me with an upvote and selection for the correct answer? :)
pilcrow
@pilcrow - have an upvote on me.
ire_and_curses
I tried to upvote you, but it said i have to have 15 reputation
David
Well, we've all been there. :) Thanks, David.
pilcrow
You get reputation from accepting answers :)
Chris Dennett