Socket's session

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Infrastructure Monitoring Socket's session
# 1  
Old 03-01-2009
Socket's session

Hi there,
I need a socket (server) in perl that when session created by a client request, take a string and after some proccesing on it, send a string(response) and close the session. but socket wait for future requests.
It's possible to lead me?! Smilie? or write a psudo code?
Thanks in advance.
# 2  
Old 03-01-2009
This is my server code
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
use threads;
use POSIX 'setsid';

use Connection; #Connection Interface plus below line
use IO::Socket qw(:DEFAULT :crlf);
use XML::DOM;

our $session;
use ObjectAddress;
use WebFilter;
#&Daemonization();
#######################################
my ($bytes_out,$bytes_in) = (0,0);
our $quit=0;
$SIG{INT} = sub { $quit++};
my $sock = Connection->new();

# Declare a few variables for XML Handling.
my ($packet, %protoCount, $nodeName, @pktInfoArray, $pktInfo);
# Instantiate a new XML::DOM parser.
my $parser = XML::DOM::Parser->new();
########################################
##### Engine's Instantiates #########
my $OBJ = ObjectAddress->new();
########################################

while (!$quit)

{
    die "Can't fork" unless defined (my $child = fork);
    exit 0 if $child;
    next unless $session = $sock->accept;

#############################Session & Requests & Responses ###########################
my $peer = gethostbyaddr($session->peeraddr,AF_INET) || $session->peerhost;
my $port = $session->peerport;
warn "Connection from [$peer,$port]\n";
my $message = <$session>;#Read Requests
############## XML DOM & Decision Maker Arena #####################

    my $doc = $parser->parsestring($message);
    my $root = $doc->getDocumentElement();

    # Retrieve an array of all nodes.
    my @packetArray = $root->getChildNodes();

    # Loop through the array.
    foreach $packet (@packetArray)
    {
        if ($packet->getNodeType() == ELEMENT_NODE)
        {
            print "Seqnum: " . $packet->getAttribute("seqnum") . "\n";
            @pktInfoArray = $packet->getChildNodes();

            foreach $pktInfo (@pktInfoArray)
            {
                if ($pktInfo->getNodeType() == ELEMENT_NODE)
                {
                    $nodeName = $pktInfo->getNodeName();
                    $nodeName =~ s/(\w+)/\u \L$1/g;
                    print "$nodeName";

                    if ($pktInfo->getNodeName() eq "protocol")
                    {
                        if ($protoCount{$pktInfo->getFirstChild()->getData})
                        {
                            $protoCount{$pktInfo->getFirstChild()->getData}++;
                        }else{
                        $protoCount{$pktInfo->getFirstChild->getData} = 1;
                        }
                    }
                    print  " : " . $pktInfo->getFirstChild()->getData . "\n";
                }
            }
            print "\n";
        }
    }
    my ($key);
    print  "\n----------------------------\n";
    print  "Protocol Summary\n";
    foreach $key (keys %protoCount) {
    print "$key\t$protoCount{$key}\n";
    }
    $doc->dispose();
############# END OF XML DOM & Decision Maker Arena ##################

syswrite ($session, "My answer string");
close $session;

    
    #warn "Connection from [$peer,$port] finished\n";
    
##################### END OF Session & Requests & Responses #####################################
    
}#For Listen mode
sub Daemonization
{
    die "Can't fork" unless defined (my $child = fork);
    exit 0 if $child;
    setsid();
    open(STDIN, "</dev/null");
    open(STDOUT, ">/dev/null");
    open(STDERR, ">&STDOUT");
    chdir '/';
    umask(0);
    $ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin';
    return $$;
}

And this is my client code
Code:
#!/usr/bin/perl
use strict;
use Socket qw(:DEFAULT :crlf);
use IO::Handle;
our $array="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE sniffer_data SYSTEM \"netlog2.dtd\"> 
<sniffer_data> 
   <packet seqnum=\"tx_1\"> 
      <protocol>SMTP</protocol> 
      <timestamp>1</timestamp>
      <source>scooby</source> 
      <dest>smtp_server</dest> 
   </packet> 
   <packet seqnum=\"tx_2\"> 
      <protocol>FTP</protocol> 
      <timestamp>4</timestamp> 
      <source>shaggy</source> 
      <dest>ftp_server</dest> 
   </packet> 
   <packet seqnum=\"tx_3\"> 
      <protocol>SNMP</protocol> 
      <timestamp>7</timestamp> 
      <source>sys_admin</source> 
      <dest>gateway</dest> 
   </packet> 
   <packet seqnum=\"tx_4\"> 
      <protocol>DNS</protocol> 
      <timestamp>8</timestamp> 
      <source>casper</source> 
      <dest>dns_server</dest> 
   </packet> 
   <packet seqnum=\"tx_5\"> 
      <protocol>HTTP</protocol> 
      <timestamp>9</timestamp> 
      <source>casper</source> 
      <dest>web_server</dest> 
   </packet> 
   <packet seqnum=\"tx_6\"> 
      <protocol>SSH</protocol> 
      <timestamp>11</timestamp> 
      <source>speedracer</source> 
      <dest>accounting</dest> 
   </packet> 
   <packet seqnum=\"tx_7\"> 
      <protocol>SMTP</protocol> 
      <timestamp>13</timestamp> 
      <source>scooby</source> 
      <dest>smtp_server</dest> 
   </packet> 
   <packet seqnum=\"tx_8\"> 
      <protocol>SMTP</protocol> 
      <timestamp>13</timestamp> 
      <source>scooby</source> 
      <dest>smtp_server</dest> 
   </packet> 
</sniffer_data>
";
my ($bytes_out,$bytes_in) = (0,0);

my $host = shift || 'localhost';
my $port = shift || getservbyname('echo','tcp');

my $protocol = getprotobyname('tcp');
$host = inet_aton($host) or die "$host: unknown host";

socket(SOCK, AF_INET, SOCK_STREAM, $protocol) or die "socket() failed: $!";
my $dest_addr = sockaddr_in($port,$host);
connect(SOCK,$dest_addr) or die "connect() failed: $!";

SOCK->autoflush(1);



     syswrite(SOCK, $array);
 
#     $_=<SOCK>;
#    print $_;
close SOCK;

I wish, server get a xml parse it and send a response. but it does not work when client or server be responsible even for receiving a str and sending a str! why?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

2. Solaris

Difference between the desktop session and console session

what is the difference between desktop session and console session in solaris as i am wondering we use option -text for the former and -nowin for the later (1 Reply)
Discussion started by: kishanreddy
1 Replies

3. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

4. Shell Programming and Scripting

Determining if session is a login session

Besides 'who am i' and 'tty' what commands could be used to determine if a session is interactive as compared to a web process or cron process. Any command should work with the common unix variants. (3 Replies)
Discussion started by: jgt
3 Replies

5. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

6. Solaris

I am not able to login in gnome session and java session in Sun solaris 9& 10

I am not able to login in gnome session and java session in Sun solaris 9& 10 respectively through xmanager as a nis user, I am able to login in common desktop , but gnome session its not allowing , when I have given login credentials, its coming back to login screen, what shoul I do to allow nis... (0 Replies)
Discussion started by: durgaprasadr13
0 Replies

7. Shell Programming and Scripting

Hiding Directories on a Session by Session basis

Hi, Apologies if anyone has read my recent post on the same subject in the Linux forum, just thought actually the solution might more likely come from scripting. Essentially, I am trying to restrict access to directories based on the user's name AND their location on a session-by-session... (3 Replies)
Discussion started by: en7smb
3 Replies

8. Shell Programming and Scripting

sqlplus session being able to see unix variables session within a script

Hi there. How do I make the DB connection see the parameter variables passed to the unix script ? The code snippet below isn't working properly. sqlplus << EOF user1@db1/pass1 BEGIN PACKAGE1.perform_updates($1,$2,$3); END; EOF Thanks in advance, Abrahao. (2 Replies)
Discussion started by: 435 Gavea
2 Replies

9. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies
Login or Register to Ask a Question