Use the IO::Socket::INET module.
You can connect to a port on localhost
$sock = IO::Socket::INET->new('127.0.0.1:2525');
or to another address
$sock = IO::Socket::INET->new("host.example.com:6789");
These examples assume the Perl program will be the client and you've written the server in C#. If it's the other way around, use the IO::Select module. Below is an example from its documentation:
use IO::Select;
use IO::Socket;
$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
$sel = new IO::Select( $lsn );
while (@ready = $sel->can_read) {
foreach $fh (@ready) {
if ($fh == $lsn) {
# Create a new socket
$new = $lsn->accept;
$sel->add($new);
}
else {
# Process socket
# Maybe we have finished with the socket
$sel->remove($fh);
$fh->close;
}
}
}
Using this code, you'd then connect from C# to port 8080 on the localhost.
The choice of port is mostly arbitrary. Both sides need to agree on the rendezvous port, and you want to avoid ports below 1024. Whether you connect to localhost or another address is determined by the address to which the server is bound. To bind to a network-accessible address, modify the above code to use
$lsn = new IO::Socket::INET(Listen => 1, LocalAddr => "host.example.com:8080");
The backlog of size 1 (the Listen
parameter) is unusual. A typical size is the value of SOMAXCONN
from sys/socket.h
.