in reply to Re^4: Perl Sockets
in thread Perl Sockets
#!/usr/bin/perl -w # server start first use strict; use IO::Socket; use IO::Select; our ($find_min_request_rec, $find_response_rec, $price_call_rec); our $read_set ; our $rh; our $ns; our $buf; our $iroam_req_type; # Create the receiving socket my $s = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '18490', Proto => 'tcp', Listen => 5, Reuse => 1, ); die "Could not create socket: $!\n" unless $s; $read_set = new IO::Select(); # create handle set $read_set->add($s);# add main socket to the set print "At Host/Port listening. Waiting to receive transactions..\n"; while (1) { # Continous Listening on Port # get a set of readable handles my ($rh_set) = IO::Select->select($read_set, undef, undef, 0); # take all readable handles in turn foreach $rh (@$rh_set) { print "Process each readable handle...\n"; if ($rh == $s) { print "Main socket/incoming connection..\n"; $ns = $rh->accept(); $read_set->add($ns); # otherwise it is an ordinary socket and # we should read and process the request }else{ print "An ordinary socket..\n"; print "Connected from: ", $rh->peerhost(); print " Port: ", $rh->peerport(), "\n"; my $bytecount = sysread($rh, $buf, 1024); $iroam_req_type = substr($buf,0,8); print "iroam_req_type-> $iroam_req_type\n"; if($buf) { # return normal input and process $buf if ($iroam_req_type =~ /FindMin/){ $find_min_request_rec = $buf; }elsif ($iroam_req_type =~ /PriceCall/){ $price_call_rec = $buf; } $find_response_rec = 'GCD 0003002296392 ENGLISH YNNNNN00 +0024160000'; print "sending\n"; $rh->send($find_response_rec); }else { # the client has closed the socket $read_set->remove($rh); close($rh); } } } }
#!/usr/bin/perl use warnings; use strict; use IO::Socket; our $iroam_req = 'FindMin TLK 91271558098436895219S 95TestRSU3 +33333333 888888885555520070718103045'; our($sock); main_process(); sub main_process{ print "main process sub....\n"; open_socket(); print $sock $iroam_req; my $return; $sock->recv( $return, 1024 ); print "return-> $return\n"; <>; # hit a key to exit } sub open_socket{ $sock = new IO::Socket::INET ( PeerHost=> 'localhost', PeerPort => '18490', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Perl Sockets
by BrowserUk (Patriarch) on Jul 21, 2007 at 14:36 UTC |