a socket example in perl

From: bad_knee (bl8n8r_at_yahoo.com)
Date: 04/13/04


Date: 13 Apr 2004 14:27:15 -0700

This is an example for all the folks new to perl that are
looking for a way to connect to a server and retrieve some
data.

This program will connect to a newserver, download the list
of newsgroups, and then print it to the screen.

Note: You may not want to do this on a dialup connection.

cheers,
bl8n8r

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

use strict;
use IO::Socket;

#
# server to contact must be first arg from command line
# eg: getnews news.creativelabs.com
#
my $NewsServer = $ARGV[0];

# make sure an argument was given
if (length ($NewsServer) < 1)
{
   die ("arg0 == server to connect to. quitting");
}

# the main subroutine returns the list of newsgroups
sub Main()
{
   my $buf = "";
   my $sock = new IO::Socket::INET (PeerAddr => $NewsServer, PeerPort
=> '119', Proto => 'tcp') || die "$!\n";
   my $data = "";
   my $bytes = 0;
   
   # connect to news server
   print "connecting to $NewsServer\n";
   
   # send list command
   $sock->send("list\r\n");

   # read list
   while (1)
   {
      # read a chunk of data
      $data = "";
      $sock->recv ($data, 1024, 0);
      $bytes = length ($data);

      # append data to buffer
      $buf = $buf.$data;
   
      # read until "dot" all by itelf as in RFC0977
      # we search for \r\n.\r\n cuz the buffer itself
      # may end with a dot in the middle of the listing
      if ($data =~ m/\x0d\x0a.\x0d\x0a$/)
      {
         last;
      }
      
   }

   # send quit command to server
   $sock->send("quit\r\n");
   
   # close socket to server
   close($sock);
   
return ($buf);
}

# print buffer results
print "".Main();



Relevant Pages

  • Re: dcdiag - advertising errors on newley promoted domain controller
    ... Can you do the following on uksccmads01 ... The problem may actually have nothing to do with the new server but the ... From a command prompt try and see if you get any additional info ... Skipping site UK-CCM, this site is outside the scope ...
    (microsoft.public.windows.server.active_directory)
  • Re: dcdiag - advertising errors on newley promoted domain controller
    ... Can you do the following on uksccmads01 ... The problem may actually have nothing to do with the new server but the ... Active Directory Forest Replication GUIDs Found: ... From a command prompt try and see if you get any additional info ...
    (microsoft.public.windows.server.active_directory)
  • Re: dcdiag - advertising errors on newley promoted domain controller
    ... Default Server: uksccmads01.jw-uk.jameswalker.co.uk ... Output from dnslint on uksccmads01: No erros found, ... Active Directory Forest Replication GUIDs Found: ... From a command prompt try and see if you get any additional info ...
    (microsoft.public.windows.server.active_directory)
  • Re: clients losing server connection
    ... We can use the nbtstat.exe command to remove or correct preloaded entries. ... Check if the server name - Ip are appeared in pair. ... Newsgroups: microsoft.public.win2000.networking ...
    (microsoft.public.win2000.networking)
  • Re: News protocol (RFC 977) implementation problem
    ... > There's a NEWNEWS command in the RFC specs, which should retrieve the ... I don't think NEWNEWS is still much in use nowadays, ... The server says here that it has a total of 20120 messages between #1 ...
    (microsoft.public.dotnet.languages.vb)