- 论坛徽章:
- 1
|
soket编程的问题帮我看看~
#!/usr/bin/perl
use Socket;
$AF_INET=2;
$SOCK_STR=1;
$PROTOCOL=0;
$port=6667;
$PACKIT='S n C4x8';
select(NEWSOCK);
$|=1;
select(STDOUT);
socket(MY_SOCKET,$AF_INET,$SOCK_STR,$PROTOCOL)||die "\n $0:Cannot open socket !";
print "socket successfull opended!\n";
$my_ip_addr=pack($PACKIT,$AF_INET,$port,127.0.0.1);
bind(MY_SOCKET,$my_ip_addr) || die "bind fail!";
print "bound to socket!";
listen(MY_SOCKET,5)|| die "cann't listen!";
print "listening..";
while(1){
$remote=accept(NEWSOCKET,MY_SOCKET) || die "unaccept!";
defined(my $pid=fork()) || &cleanup;
if($pid==0){
sleep(3);
print NEWSOCKET "weclome to this server!";
close NEWSOCKET;
close MY_SOCKET;
exit;
}
sleep 10;
close MY_SOCKET;
close NEWSOCKET;
exit;
}
sub cleanup {close NEWSOCKET;close(MY_SOCKET);die "cann't fork!";} |
|