Streen has asked for the wisdom of the Perl Monks concerning the following question:

Hello, After a long time not programming in perl I recently tried to write somesthing new. I tried to make a server in perl using sockets. This is all fine, but I want to put some of the code in differen Modules, and this is where the problem starts. Part of the code:
while (($Timeout eq "false") && accept(Client, Server)) { #Server may run and a connection to a client has been found Messages::PrintToLog("Client Connected\n"); my $clientHandle = getpeername (Client) or print "Could not id +entify Client: $!\n"; my ($port, $iaddr) = unpack_sockaddr_in($clientHandle); my $clientIP = inet_ntoa($iaddr); Messages::PrintToLog("Client IP: $clientIP \n"); # Check which branch to go my $answer = <Client>; if ($answer eq "AdminUserLogin\n") { print Client "test"; AdminFunctions::MainCall($clientHandle); #$Timeout = "true"; } elsif($answer eq "ShutDown\n") { #DEBUG ONLY $Timeout = "true"; } Messages::PrintToLog("Got something from the Client: $answer") +; Messages::PrintToLog("Closing Client.\n"); close Client; } Messages::PrintToLog("Closing Server.\n"); close Server;
The problem is the line: AdminFunctions::MainCall($clientHandle); I want to put the client in the module, so that part of the communication is in this case in the "AdminFunctions.pm". in the Module for example the MainCall Function is:
sub MainCall { $ClientHandle = shift @_; Messages::PrintToLog("Admin: MainCall"); print $ClientHandle "Testing"; my $Answer = <$ClientHandle>; #some more function calls }
The program runs until it hits the line: "print $clientHandle" after that no error message is displayed and the program stops. How could i relaize this problem? What are my faults? I hope my english is not as bad as I think, its been a long time since I wrote a text in english (which is not my native language) regards Streen Edit: Repasted the code for better reading, sorry it was so unreadable

Replies are listed 'Best First'.
Re: Socket Client to Module
by shmem (Chancellor) on Dec 04, 2006 at 16:31 UTC
    Don't pass the return value from getpeername to your MainCall method, but the Client socket filehandle:
    AdminFunctions::MainCall(*Client);

    getpeername returns a packed socket address, not a socket handle.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Doh, this seems logical.
      And in the MainCall Method itself, do I get the Client the same way it is now?
Re: Socket Client to Module
by grep (Monsignor) on Dec 04, 2006 at 16:25 UTC

    Seriously, Can you read unindented code? I'm not talking about right after you wrote it, but when you come back a week or a month later. Can you actually figure out where the blocks of code are?

    It doesn't really matter if you can. Because, you are asking for help here and most people here will most likely have problems reading your code like that (well, it'll take longer). Which will make them less inclined to help you. They'll move on to the next question where the person asking the question has taken the time to indent their code before posting it.

    Remember to Clean your room

    grep
    XP matters not. Look at me. Judge me by my XP, do you?

      You're right, I was quite busy today morning and somehow the formatting was missed, when I copied the code. I editet it now for a besser reading. regards Streen
Re: Socket Client to Module
by Joost (Canon) on Dec 04, 2006 at 16:34 UTC
Re: Socket Client to Module
by liverpole (Monsignor) on Dec 04, 2006 at 16:36 UTC
    Hi Streen,

    First of all, I'm in complete agreement with grap, that you should be indenting your code; if not for yourself, for anyone else trying to read it.

    Secondly, are you running into a buffering issue, perhaps?  What happens if you append a newline to the data being sent to the socket, to try to flush the buffer?  For example:

    print $ClientHandle "Testing\n";

    Does that get you any further?


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/