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

Hi guys
I'm trying to write some socket middleware, where a perl script sits inbetween a single server socket AND keeps a socket open for a single client to continually reconnect and post data to. So client connects to the middleware and dumps some data over a socket, the middleware repackages it and passes it to the master socket. Occasionally the master socket will pipe something to the middleware.

Problem is that unless the function that the thread spawned makes the call, I can't access the sockets from outside their threads. There must be a way to call something like print threadID=>socket "hi", but I can't figure it out.

The code below is pretty close. Compiles/runs. Connects to the master control program, and leaves a socket open for a telnet session to connect to. The only problem is that when the client connects, I can't print anything to the master server socket via sendServer. It dies with:
thread failed to start: Can't use an undefined value as a symbol refer +ence at server_client_thread.pl line 18, <GEN1> line 4.
Stumped. Any help appreciated.
Thanks!
#!/usr/bin/perl -w use strict; use threads; use threads::shared; use IO::Socket; use Net::hostent; my $controlSocket; my $server; my $controlSocketThread = threads->new (\&commandPath); my $serverHostThread = threads->new (\&serverStart); sleep(); ##Talk to the command server sub sendServer(){ my $buff = $_[0]; print "I am sending from Client to controlServer: $buff\n"; print $controlSocket "$buff\n"; ##Code Dies Here $controlSocket->autoflush(); } ##Talk back to the client who connected (initiated from server) sub sendClient(){ my $buff = $_[0]; printf $server ("$buff"); $server->autoflush(); print "<---$buff"; } ##This initiates command to the Control Server sub commandPath { while (1){ $controlSocket = IO::Socket::INET->new("localhost:1024") or pr +int "Can`t open feed\n"; if (defined($controlSocket)){ last; } } print "-->HBAK\n"; print $controlSocket "HBAK\n"; print $controlSocket "STREAM\n"; while ( <$controlSocket> ) { print "I got $_"; if (defined($server)){ &sendClient("I got $_\n"); } } } ##clients connect through this function sub serverStart(){ my $port= 2048; $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $port, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 is running]\n"; while ($server = $server->accept()) { $server->autoflush(1); my $hostinfo = gethostbyaddr($server->peeraddr); printf "[Connect from %s]\n", $hostinfo->name || $server-> +peerhost; &sendClient ("HELO\n"); #Say hi to everyone who connects while ( <$server>) { print "Client requested $_\n"; next unless /\S/; # blank line if (/quit|exit/i) { last;} elsif (/RELAY/i) { &sendClient("Sending to Server\n"); &sendServer("RELAY\n"); } elsif (/STARTING/i) { } else { print $server "Command unknown $_"; } } } close $server; }

Replies are listed 'Best First'.
Re: Threaded Sockets, access from outside thread?
by BrowserUk (Patriarch) on Jun 16, 2006 at 07:48 UTC

    threads won't allow you to share blessed objects directly, and IO::Socket handles are blessed globs. The only workaround that I've discovered so far is to share the fileno of sockets that you wish to use from multiple threads (which Perl sees as simple scalars), and then use this to dup the socket into other threads that need to access the socket.

    See Re: FileHandles and threads & Re: multithreaded tcp listener with IO::Socket and associated subthreads for some background and detail.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I'm willing to pay to get the code above to be able to share sockets, or to have a program that can host socket connections while shooting data back/forth to a single socket server. Any recommendations on where I can post for a mercenary perl coder to fix the code? And in general, for random bits of code that I'd be willing to outsource, where can I go to find pay for talent?
      thanks! You can reach me at dingofan at g mail dot com
Re: Threaded Sockets, access from outside thread?
by Anonymous Monk on Jun 16, 2006 at 05:35 UTC
    I can't print anything to the master server socket via sendServer
    What socket? $controlSocket is undefined (Can't use an undefined value as a symbol refer). Try to share.
      $controlSocket is declared globally uptop, and then is configured in the sub commandPath to initiate the socket to the master server.
      Even though I declared it globally, I can't access it from outside the thread.
      Unless you're seeing something else and I'm misinterpreting what you're saying?

        I think he means
        my $controlSocket;
        should be
        my $controlSocket :shared;
        Variables aren't automatically shared.