develooper Front page | perl.beginners | Postings from April 2002

IO::Socket need help

From:
Morgan Norell
Date:
April 3, 2002 04:26
Subject:
IO::Socket need help
Message ID:
1017836519.5052.18.camel@raven.ebl.se
Hi

I'm about to lern the basics of perls IO::Socket.

I have managet to set up a small server and connected to it with a
client (I had some help of a perl book). But I want to be able to send a
command to it and get a response.

For exapmle request the servers hostname.

Can anyone please help me with a few hints.

I use RH 7.2 and perl 5.6.1

thanks
Morgan


-------
SERVER
-------
#!/usr/bin/perl -w

use IO::Socket;

$server_host = "192.168.1.120";
$server_port = 9500;

$sock = new IO::Socket::INET (LocalHost => $server_host,
				LocalPort => $server_port,
				Proto     => 'tcp',
				Reuse     => 1,
				Listen    => 10 )
	or die "Couldn't connect to tcp server on port $server_port : $@\n";

while ($new_sock = $sock->accept()) {
	while (defined ($buf = <$new_sock>)) {
		print $buf;
	}
}

close ($sock);


-------
client
-------
#!/usr/bin/perl -w

use IO::Sock;

$remote_host = "192.168.1.120";
$remote_port = 9500;

$sock = new IO::Socket::INET (PeerAddr => $remote_host,
				PeerPort => $remote_port,
				Proto    => 'tcp',
				);
die "Socket could not be created. Reason: $!\n" unless $sock;

print "$sock\n";

close ($sock);




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About