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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |