#!/usr/bin/perl use IO::Socket; my $server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => 2345, Proto => 'tcp' ) ; my $client = $server->accept; while (<$client>) { print $_ ;} exit; #### #!/usr/bin/perl sub SOCKET_init { # Creat the socket for the server: $| = 1 ; use Socket;my $p=getprotobyname('tcp');socket(SRV,PF_INET,SOCK_STREAM,$p); setsockopt(SRV,SOL_SOCKET,SO_REUSEADDR,pack('l',1));my($opn,$try); while($opn!=1 && $try<=20){if(bind(SRV,sockaddr_in($_[0],INADDR_ANY))){$opn=1} else{$try++;sleep(1)}}if($opn!=1){print"ERROR! Port $_[0] in use.\n";exit} listen(SRV,SOMAXCONN);my($wpid,$pad)=(0);$SIG{CHLD}=\&REAPER;my $CLT; for($wpid=0;&ACT_CLT;$wpid=0,close(NS)) {next if $wpid and not $pad;my($p,$a)=sockaddr_in($pad); my @i=unpack('C4',$a);$CLT++;$PORTS{$CLT}=$p;$IPS{$CLT}=join('.',@i);my $sl=select(NS);$|=1; select($sl);if($_[1]=~/^(1|ye?s?|si?m?)$/i){&SPAWN($CLT)}else{&Server_DO($CLT)}} sub REAPER{$wpid=wait;$SIG{CHLD}=\&REAPER} sub ACT_CLT{return($pad=accept(NS,SRV)) || $wpid} ## Fork to accept other connections while connected with client: (not gruped for you modify) sub SPAWN { my ( $CLIENT_id ) = @_ ; my $pid ; if (!defined($pid = fork)) { print "cannot fork: $!\n" ; return ;} elsif ($pid) { return ;} exit &Server_DO($CLIENT_id) ; } } &SOCKET_init(2345) ; sub Server_DO { my ( $CLIENT_id ) = @_ ; print "New Client!\n" ; while( my $buf = ) { print $buf ; } close(NS) ; exit; }