UPDATE: So I was FINALLY able to get the basic part of it working but now I'm running into an odd problem that I can't seem to work out.

I got the following code to complete one full request from a Windows server basic Perl client to the Ubuntu machine OR I can telnet to localhost on the Ubuntu machine and type a manual command. Either option results in a connection refused message if I try to send a second request from either side and for the life of me I can't figure out how (or where) to fix it.

#!/usr/bin/perl -w use strict; use warnings; use IO::Socket::INET; my $sock = IO::Socket::INET->new( LocalPort => 8000, Listen => 10, Reuse => 1 ); die "Cannot create socket $!\n" unless $sock; print "Server waiting for client to connect on port 8000\n"; $| = 1; # autoflush my $client = $sock->accept(); my $handle; my $clientHost = "10.20.0.30"; my $clientPort = "8000"; while(){ while ($client) { # receive data, pass data via variable to dedicated subroutine # based on pattern matching my $recv_msg = &recv(); # handles recv() activity if ($recv_msg =~ /ADD/) { ADD( $recv_msg ); # shutdown ($client, 2); } elsif ( $recv_msg =~ /STK/ ) { STK( $recv_msg ); # shutdown ($client, 2); } } } # $sock->close(); sub connection { my ($host, $port) = @_; $handle = IO::Socket::INET->new ( PeerHost => $host, PeerPort => $port, Proto => "tcp") or $handle = 0; if ($handle != 0) { $handle->autoflush(1); # output gets there right away } else { print "Can't connect to host from sub connection."; } return $handle; } sub ADD { my ( @data ) = @_; sendRequest($data[0]); print "Data after socket->send: $data[0]\n"; print "This message is an ADD request.\n"; $handle->close(); $sock->close(); } sub STK { my ( @data ) = @_; sendRequest($data[0]); print "Data after socket->send: $data[0]\n"; print "This message is a STK request.\n"; $handle->close(); $sock->close(); } sub recv { my $msg = ""; $client->recv($msg, 12000); return $msg; } sub sendRequest { my ($reqSock, $msg) = @_; &sender($reqSock, $msg); } sub sender { my ($reqMsg) = @_; $handle = connection($clientHost, $clientPort); eval { $handle->send($reqMsg); }; print 'Crash: '.$@ if $@; if ($@) { print "Connection Reset"; $handle->close; # $handle = connection($clientHost, $clientPort); eval { $handle->send($reqMsg); }; } }


In reply to Re: Bidirectional Client/Server - to fork or not to fork? by ljamison
in thread Bidirectional Client/Server - to fork or not to fork? by ljamison

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.