usbmgmt/usbmgrtest/t_ncm/netutil/server.pl
changeset 59 bbdce6bffaad
equal deleted inserted replaced
58:84c26be382f0 59:bbdce6bffaad
       
     1 #!/usr/bin/perl -w
       
     2 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 #
       
    16 use strict;
       
    17 use IO::Socket qw(:DEFAULT :crlf);
       
    18 
       
    19 use constant MY_ECHO_PORT	=> 5000;
       
    20 $/ = "\n";
       
    21 $| = 1;
       
    22 my $tcp = 0;
       
    23 print "\nUsage: server.pl tcp|udp [port]\n\n";
       
    24 
       
    25 my ($byte_out, $byte_in) = (0,0);
       
    26 
       
    27 my $quit = 0;
       
    28 $SIG{INT}  = sub { $quit++ };
       
    29 
       
    30 my $tcp = shift eq 'tcp';
       
    31 my $port = shift || MY_ECHO_PORT;
       
    32 my $sock;
       
    33 
       
    34 if ($tcp)
       
    35 {
       
    36     my $sock = IO::Socket::INET->new (
       
    37 	Listen	=> 20,
       
    38 	LocalPort => $port,
       
    39 	Timeout	=> 60*60,
       
    40 	Reuse	=> 1 )
       
    41 	or die "Can not create listening socket: $!\n";
       
    42     warn "waiting for the incoming connections on port $port...\n";
       
    43 
       
    44     while ($tcp and !$quit) {
       
    45 	next unless my $session = $sock->accept;
       
    46 	my $peer = gethostbyaddr($session->peeraddr, AF_INET) || $session->peerhost;
       
    47 	warn "Connection from [$peer, $port]\n";
       
    48 
       
    49 	while(<$session>) {
       
    50 	    $byte_in += length($_);
       
    51 	    chomp;
       
    52 	    print "in: $_\n";
       
    53 	    my $msg_out = (scalar reverse $_) . CRLF;
       
    54 	    print "send back: $msg_out";
       
    55 	    print $session $msg_out;
       
    56 	    $byte_out += length($msg_out);
       
    57 	}
       
    58 	warn "Connection from [$peer, $port] finished\n";
       
    59 	close $session;
       
    60     }
       
    61 }
       
    62 
       
    63 if (not $tcp)
       
    64 {
       
    65     my $sock = IO::Socket::INET->new(
       
    66 	LocalPort  => $port,
       
    67 	Type   => SOCK_DGRAM,
       
    68 	Proto  => 'udp') or die "Can't connect: $!\n";
       
    69 
       
    70     warn "waiting for the incoming udp data on port $port...\n";
       
    71 
       
    72     while (!$quit) {
       
    73 	my $msg_in;
       
    74 	$sock->recv($msg_in, 10000);
       
    75 	$byte_in += length($msg_in);
       
    76 	chomp($msg_in);
       
    77 	print "in: $msg_in\n";
       
    78 	my $msg_out = (scalar reverse $msg_in) . CRLF;
       
    79 	print "send back: $msg_out";
       
    80 	$sock->send($msg_out);
       
    81 	$byte_out += length($msg_out);
       
    82     }
       
    83 }
       
    84 
       
    85 print STDERR "out = $byte_out, in = $byte_in\n";
       
    86 close $sock;
       
    87