Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: problem with SOCKET

by Starky (Chaplain)
on Feb 10, 2003 at 19:06 UTC ( #234203=note: print w/replies, xml ) Need Help??

in reply to problem with SOCKET

Here is your code in working form:


#!/usr/bin/perl use strict; use Socket; my $port = 2000; my $hostname = 'localhost'; my $protocol = getprotobyname ("tcp"); my $host = gethostbyname ($hostname); my $iaddr = sockaddr_in($port,$host); socket (SOCKET, PF_INET, SOCK_STREAM, $protocol) || die ("Error establ +ishing socket to [$hostname:$port]: $!"); connect (SOCKET, $iaddr); while (defined(my $line = <SOCKET>)) { chomp $line; print "Received from [$hostname:$port]: $line\n"; } close (SOCKET); __END__ Output will look like: [starky@freak bin]$ ./ Received from [localhost:2000]: Hello, world! Received from [localhost:2000]: [starky@freak bin]$


#!/usr/bin/perl use strict; use Socket; my $line = "Hello, world!\n"; my $port = 2000; my $protocol = getprotobyname ("tcp"); my $addr = INADDR_ANY; my $clientaddr; my $serveraddr = sockaddr_in($port,$addr); socket (SERVER, PF_INET, SOCK_STREAM, $protocol) || die ("Error establ +ishing socket to port [$port]: $!"); print "Binding to port [$port] ...\n"; bind (SERVER, $serveraddr) || die ("Can't bind: $!"); print "Listening on port [$port] ...\n"; listen (SERVER, SOMAXCONN) || die ("Can't listen: $!"); print "Accepting client connections on port [$port] ...\n"; ($clientaddr = accept (CLIENT, SERVER)) || die ("Can't accept: $!"); my ($clientport,$clientiaddr) = sockaddr_in($clientaddr); my $client = inet_ntoa($clientiaddr); print "Received connection from [$client:$clientport]. Saying hello . +..\n"; print CLIENT "$line\n"; close (CLIENT); close (SERVER); print "Done.\n"; __END__ Output will look like: [starky@freak bin]$ ./ Binding to port [2000] ... Listening on port [2000] ... Accepting client connections on port [2000] ... Received connection from []. Saying hello ... Done. [starky@freak bin]$

Some comments:

  • First and foremost: Use strict. If you are a beginner, this one step, which will seem like a pain in the butt now, will pay whatever pains it causes back tenfold. Trust me.
  • Use the Socket module. It provides constants such as PF_INET, SOCK_STREAM, etc. which will make your code both readable and portable.
  • Next, do a perldoc perlipc and memorize it. Or at least work through all the examples. Then read the documentation for IO::Handle, IO::Socket, and IO:Socket::INET. Then read the documentation for Net::Daemon. If you are a beginner, the Net::Daemon module may be a bit to digest, but it really does simplify the process of creating daemons considerably.
  • I don't know quite what you're doing with the while (...) { $port++; }, so I got rid of it.
  • Dispense with manual packing. Use sockaddr_in or a combination of pack_sockaddr_in and unpack_sockaddr_in.
  • You don't need to bind and connect on the client side. Just connect.
  • Note the (more informative) use of $! in the die statements.
  • You don't need to select on the server side. What you're doing is essentially selecting SOCKET as the default filehandle, then explicitly printing to the SOCKET filehandle, a redundant process. Print to the socket filehandle directly and save yourself the select so you can print instructive debugging output to STDOUT or a log file. If you want to make sure output is autoflushed, you can use IO::Socket::INET to generate a $handle (see below), then $handle->autoflush(1).

In regards to IO::Socket::INET, here is a simplified version of your client (that works just the same):

#!/usr/bin/perl use strict; use IO::Socket::INET; my ($port,$hostname,$protocol) = (2000,'localhost','tcp'); my $handle = IO::Socket::INET->new( Proto => $protocol, PeerAddr => $hostname, PeerPort => $port ) || die "Cannot connect to host [$hostname] on port [$port]: $!"; while (defined(my $line = <$handle>)) { chomp $line; print "Received from [$hostname:$port]: $line\n"; } $handle->close();
Hope this helps!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://234203]
hippo is still undecided on YAPC::EU
LanX hippo doesn't want to leave his country ... ehm .... riverbed?
[marto]: Corion, I'll talk to you later, but that are is generally fine :)
[hippo]: Just not sure that the expense and hassle is worth the benefit.
[LanX]: expense? .... meeting ppl is inspiring ...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2018-06-20 09:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.