So you are trying to create your own protocol. First of all, you have to clearly define your request and response before you start coding, especially the boundary of each request and response.
The complexity you introduced by using fork() is not neccessary at all. I know why you used fork(), because your protocol is not well defined, and made you unsure about the ending of the request or response. If the protocol is clearly defined, you should be able to handle this within one process.
Client - no more fork():
$| ++; use IO::Socket; use strict; use warnings; my $handle = IO::Socket::INET->new( PeerAddr => "localhost", Proto => 'tcp', PeerPort => "6941") or die "can't connect to port 6941 on host localhost: $!"; print "[Connected to host:6941]\n"; my $line = <$handle>; print $line; $line = <STDIN>; print $handle $line; $line = <$handle>; print $line;
Server - small change, and only for the communication part.
#!/usr/bin/perl -w use strict; use IO::Socket; my $port = 6941; my $auth = " " ; my $code = " " ; #PREVENT ZOMBIES $SIG{CHLD} = 'IGNORE'; #CREATE SOCKET my $listen_socket = IO::Socket::INET->new(LocalPort => $port, Listen => 10, + Proto => 'tcp', Reuse => 1); die unless $listen_socket; warn "Server ready. Waiting for connections ... \n"; #GET CONNECTIONS my $socket = $listen_socket->accept; #REQUEST AUTHORISATION print $socket "auth code:\n"; $code = <$socket>; #GET CODE print "$code\n"; verify($code,$socket); #SEND FOR VERIFICATION exit; #SHOULD NEVER GET HERE #VERIFY AUTHORISATION CODE sub verify{ my ($code,$socket) = @_; my $auth_code = "6941"; if($code eq $auth_code) { $auth = "1"; print $socket "Accepted\n\n"; load_interface($socket); } else { $auth = "0"; print $socket "Good bye\n\n"; exit; } } sub load_interface{ my $socket = $_[0]; exit unless( $auth == "1"); open(CONFIG,"conifg") || die "Can't load config file\n"; #THIS WILL B +E A LIST OF COMMANDS my $data = " "; while(<CONFIG>) { $data = $data . $_; } print $socket $data; print $socket "\n\nWhat would you like to do (0\1\2\3\....): "; my $cmd = <$socket>; #GET COMMAND cmd($cmd,$socket); } #THIS WILL DO COMMAND. ONLY FOR TEST PURPOSES AT THE MOMENT sub cmd{ my( $cmd,$socket) = @_; exit if($cmd == "q"); print $socket "\n\nYou chose option $cmd!!!!!!\n\n"; load_interface(); }
In reply to Re: Un-initalised values in Server\Client
by pg
in thread Un-initalised values in Server\Client
by eoin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |