CLIENT PORTION: (you need to change PeerAddr to reflect who you want to connect to)###################################################################### +### ## ## myServer.pl by Jake Roberts. ## ## A newbies attempt at TCP socket networking and Perl ## ## This is a forking TCP server. This is my first try with forks, IP +C, ## sockets, and protocols. ## ## The server opens a socket and waits for a connection. When one is ## recieved a fork is made. The child starts then tells the parent h +e ## is ready for him to close is connection (I don't know if it is nec +essary ## for the child to talk to the parent but I found that sometimes the + parent ## seemed to close the connection to fast. I could have used sleep() + but ## that seems like a shody way to do it.) Then the child begins ping + pong, etc. ## ## The parent continues waiting for connections and making babies. ## ## ## That idea was a real trip up for me as a newbie. ## ## The network/socket/fork/ipc stuff was taken from examples from ## www.perlmonks.org (an excellent web site) and pieced together by ## yours truely ## ## ###################################################################### +### #!/perl/bin/perl -w use strict; use warnings; #use Socket; use IO::Handle; # thousands of lines just for autoflush :-( use IO::Socket::INET; # We say AF_UNIX because although *_LOCAL is the # POSIX 1003.1g form of the constant, many machines # still don't have it. #ignore child processes to prevent zombies $SIG{CHLD} = 'IGNORE'; my $socket = IO::Socket::INET->new( LocalPort => 1776, Type => SOCK_STREAM, Reuse => 1, Listen => 10 ) or die "IO::Socket::INET->new : $!\n\n"; warn "Server Started...\n"; warn "Waiting for connections...\n"; while ( my $client = $socket->accept() ) { ## Wait for a connecti +on { my $child; ## Setup IPC so the parent waits until the child is ready +before closing the connection. ## Taken from www.perlmonks.org search: perlipc I hones +tly don't fully understand whats going on. pipe(PARENT_RDR, CHILD_WTR); # XXX: failure +? pipe(CHILD_RDR, PARENT_WTR); # XXX: failure +? CHILD_WTR->autoflush(1); PARENT_WTR->autoflush(1); # perform the fork or exit die "Can't fork: $!" unless defined ($child = fork()); ## Now there are two identical programs running, one is t +he child and should do one thing ## One is the parent and should do something else if ($child == 0) { # I'm the child! # Close the child's listen socket, we dont need it the + parent is the one listening. $socket->close; ## More confusing IPC stuff. There we tell the parent + that I'm up and going print PARENT_WTR "Going\n"; ## Tell the parent I'm re +ady close CHILD_RDR; close CHILD_WTR; # Close my end IPC +since I don't want to talk to myself close PARENT_RDR; close PARENT_WTR; # Done talking to + the Parent so close IPC to parent ######### # Main child rountine ######### ## The client needs this line to begin to talk print $client "Connection Established\n"; ## Ping Pong while (1) { my $response = <$client>; ## Wait for the client t +o talk if ($response) { # Make sure we really got someth +ing chomp $response; if ($response eq "ping") { ## If the client s +aid ping its our turn to say pong warn "ping\n"; print $client "pong\n"; } elsif ($response eq "quit") { ## Not really u +sed but can be used for clean closure (We all need closure) warn "Closing ",$client->peerhost,"\n"; last; } } else { ## We got an undef and therefore the sock +et is closed warn $client->peerhost," lost connection\n"; +## print who was lost to the console last; ## and exit } } ######### # If the child subroutine returns, then clean up and e +xit; ######### close($client); exit 0; } else { # I'm the parent! my $line; close PARENT_RDR; close PARENT_WTR; # Close my end IPC + because I don't need to talk to myself # Send Connection notice to Console warn "Connection recieved ... ",$client->peerhost,"\n +"; ## Wait for the child to talk to me and say he's start +ed up ## We do this so I don't close the socket connection b +efore he can get started up while(chomp($line = <CHILD_RDR>)) { if ($line eq "Going") { last; } sleep(1); ## Check every one second....I don't kno +w if this is needed } close CHILD_RDR; close CHILD_WTR; # Done talking to th +e child so close the IPC # Close the connection, its been passed it off to a ch +ild. $client->close(); } } } close($socket);
###################################################################### +### ## ## myClient.pl by Jake Roberts. ## ## A newbies attempt at TCP socket networking and Perl ## ## This client plays ping pong with the server. Its rather simple. ## ## It does demonstrate the importance of defining a protocol for spea +king ## to the server. The client must wait his turn to print to the $soc +ket or ## you can get locked up. ## ## That idea was a real trip up for me as a newbie. ## ## Most of the network/socket stuff was taken from examples from ## www.perlmonks.org an excellent web site ## ## ###################################################################### +### #!/perl/bin/perl -w use strict; use warnings; use IO::Socket::INET; ## Open a connection to the server my $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => '1776', Proto => "tcp", Type => SOCK_STREAM) or die "Socket::INET->new: $!\n\n"; ## Modes # 1 = Waiting for server connection # 2 = Wait for Pong then Send Ping ## Play Ping ping with the server my $mode = 1; while (1) { my $response = <$socket>; ## Read data from Server ## It wi +ll wait here until it recieves a \n from the server ## Reading data from the server th +is way kind of sucks because you don't have any type of timeout if ($response) { ## Important to check because if the connect +ion is closed then $response will be undef chomp $response; ## Remove the \n ############ ## This is my ping Protocal. ## The client and server MUST take turns talking or they +will get stuck ## So...first the client waits for a Connection Establish +ed signal.... ## then the client sends a ping to the server and waits h +is turn and listens ## for a pong. When a pong is recieved a ping is sent ag +ain...and so on. ## They must play like good children and take turns talki +ng and listening ############ ## Mode 1 -- Waiting for connection signal if ($mode == 1) { if ($response eq "Connection Established") { print $response,"\n"; print $socket "ping\n"; ## The server send a Conn +ect so now its our turn to say ping print "ping..."; $mode = 2; } } elsif ($mode == 2) { ## We sent a ping and now we must + wait for a pong before talking again. if ($response eq "pong") { ## We recieved a pong print "pong\n"; sleep(3); ## Wait just so things to fly by too +fast print $socket "ping\n"; ## Now its our turn to ta +lk...say ping print "ping..."; $mode = 2; ## Now that we said ping wait for a po +ng } } else { ## Just in case some wierd thing happens close($socket); die "Entered invalid mode"; } } else { ## We got an undef and therefore?? the connection wa +s closed. print "Lost connection\n"; last; } } close($socket); exit 0;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Newbies TCP Networking
by tadman (Prior) on Jul 07, 2001 at 12:49 UTC | |
by Anonymous Monk on Jul 10, 2001 at 23:06 UTC | |
by tadman (Prior) on Jul 10, 2001 at 23:38 UTC | |
by otijim (Acolyte) on Jul 11, 2001 at 23:12 UTC | |
by otijim (Acolyte) on Jul 10, 2001 at 23:09 UTC |