Мне в свое время пришлось связывать OS/2 и Linux. Для этих вещей я как раз и использовал обмен через TCP/IP.
Пример взял из книги Perl Cookbook (которая с бараном).
Вот привожу его ниже. (надеюсь это то, что тебе нужно)
17.2. Writing a TCP Server
Problem
You want to write a server that waits for clients to connect over the network to a particular port.
Solution
This recipe assumes you\'re using the Internet to communicate. For TCP-like communication within a single Unix machine, see Recipe 17.6.
Use the standard (as of 5.004) IO::Socket::INET class:
use IO::Socket;
$server = IO::Socket::INET->new(LocalPort => $server_port,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10 ) # or SOMAXCONN
or die "Couldn\'t be a tcp server on port $server_port : $@\\n";
while ($client = $server->accept()) {
# $client is the new connection
}
close($server);
Or, craft it by hand for better control:
use Socket;
# make the socket
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname(\'tcp\'));
# so we can restart our server quickly
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
# build up my socket address
$my_addr = sockaddr_in($server_port, INADDR_ANY);
bind(SERVER, $my_addr)
or die "Couldn\'t bind to port $server_port : $!\\n";
# establish a queue for incoming connections
listen(SERVER, SOMAXCONN)
or die "Couldn\'t listen on port $server_port : $!\\n";
# accept and process connections
while (accept(CLIENT, SERVER)) {
# do something with CLIENT
}
close(SERVER);
17.1. Writing a TCP Client
Problem
You want to connect to a socket on a remote machine.
Solution
This solution assumes you\'re using the Internet to communicate. For TCP-like communication within a single machine, see Recipe 17.6.
Either use the standard (as of 5.004) IO::Socket::INET class:
use IO::Socket;
$socket = IO::Socket::INET->new(PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto => "tcp",
Type => SOCK_STREAM)
or die "Couldn\'t connect to $remote_host:$remote_port : $@\\n";
# ... do something with the socket
print $socket "Why don\'t you call me anymore?\\n";
$answer = <$socket>;
# and terminate the connection when we\'re done
close($socket);
or create a socket by hand for better control:
use Socket;
# create a socket
socket(TO_SERVER, PF_INET, SOCK_STREAM, getprotobyname(\'tcp\'));
# build the address of the remote machine
$internet_addr = inet_aton($remote_host)
or die "Couldn\'t convert $remote_host into an Internet address: $!\\n";
$paddr = sockaddr_in($remote_port, $internet_addr);
# connect
connect(TO_SERVER, $paddr)
or die "Couldn\'t connect to $remote_host:$remote_port : $!\\n";
# ... do something with the socket
print TO_SERVER "Why don\'t you call me anymore?\\n";
# and terminate the connection when we\'re done
close(TO_SERVER);