There two via: 1) C language style, 2)using a module.

1) ##example client
use Socket;  ##export costants
socket(SK,PF_INET,SOCK_STREAM,getprotobyname('tcp')) || die"$!\n";
###declaration: SK is a name gived at the socket; PF_INET is a costant: 
socket internet; SOCK_STREAM is a costant:
## type of "flux" data(stream,for examople for tcp; datagram for example 
for udp).


$dest=sockaddr_in($port,inet_aton($host)); ##where $host is in dot or 
symbolic notation
connect(SK,$dest) || die"$!\n";

##now you can print or read on the socket like a file.

##print
select SK;
$|=1;
print "HELLO\n";
select STDOUT;

##read
while(<SK>){
    print"$_\n";  ###read from socket and print on STDOUT
}
close(SK);
###################################
##example 1b) server
use Socket;  ##export costants
socket(SK,PF_INET,SOCK_STREAM,getprotobyname('tcp')) || die"$!\n";
$serv=sockaddr_in($port,INADDR_ANY));  ### INADDR_ANY is a costant: call 
the local IP.
### there are others costants: INADDR_BROADCAST, 
INADDR_LOOPBACK,INADDR_NONE.

bind(NEW,$serv) || die"$!\n"; ## open a new socket descriptor
listen(NEW,$num);  ##  $num is num of connection max in wait state.
for(;;){
    accept(NEW,SK);  ### accept the connecton. The function wait the 
connection request from a client.
    ##
    ## now you can write and read like a file on NEW descriptor socket-
   
    close(NEW); ##end of connection from the client
}
close (SK);

## if you want you can use the fork for have a concorrent server(another 
mail !!!!).



2) exaple client with the module.
use IO::Socket;
$socket=IO::Socket::INET->new(PeerAddr =>$host, PeerPort=>80, 
Proto=>"TCP") || die"$!\n";
print $socket "GET / HTTP/1.0\r\n\r\n";
while(<$socket>){
    print"$_\n";
}
close($socket);



2b) server with module

use IO::Socket;
$socket=IO::Socket::INET->new(LocalAddr =>$your_ip, LocalPort=>80, 
Proto=>"TCP",Listen=>5) || die"$!\n";
##Do the socket,bind and liste function !!!
##Listen is like the $num above.  

for(;;){
    $new=$socket->accept();
    ##
    ## you can read and write on the $new descriptor like a file.

    close($new);  ##and
}
close($socket);




    Walter


>How can I use sockets in Perl?
>
>
>
>
>
>
>
>
>
>
>
>
>




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to