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

RE: :Socket need help

Thread Next
From:
Bruno Figueira
Date:
April 3, 2002 05:48
Subject:
RE: :Socket need help
Message ID:
31E38B53D182D51195FA00508BE3A334A612E2@zwnbc004.cala.nortel.com
Morgan,

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

                $cmd=`$buf`;
                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
192.168.1.120 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
application.


Regards,

Bruno Figueira


-----Original Message-----
From: Morgan Norell [mailto:norell@ebl.se]
Sent: Quarta-feira, 3 de Abril de 2002 10:22
To: beginners@perl.org
Subject: IO::Socket need help


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


-------
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);



Thread Next


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