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; }

In reply to Threaded Sockets, access from outside thread? by ecuguru

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.