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

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.