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

RE: :Socket need help

Thread Next
Bruno Figueira
April 3, 2002 05:48
RE: :Socket need help
Message ID:

Just after the "print $buf" line in the server code, add:

                print $new_sock "$cmd";

You actually don't need to use a client a this time. Certainly you can, but,
to make this simpler, just telnet to the server in that port (telnet 9500) and issue commands. Their output will return to you.
Pretty unsafe...

I guess that from this point you can improve your scripts. A socket
connection is just an interface between two scripts/applications. You'll
have to build your protocols above this "layer" so that one side is making
resquets and and other is providing the answers. That's a client/server


Bruno Figueira

-----Original Message-----
From: Morgan Norell []
Sent: Quarta-feira, 3 de Abril de 2002 10:22
Subject: IO::Socket need help


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


#!/usr/bin/perl -w

use IO::Socket;

$server_host = "localhost";
$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);

#!/usr/bin/perl -w

use IO::Sock;

$remote_host = "";
$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);

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About