IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Programmation et administration système Perl Discussion :

Serveur Soket tcp interactif


Sujet :

Programmation et administration système Perl

  1. #1
    Membre actif Avatar de mobscene
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    331
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 331
    Points : 234
    Points
    234
    Par défaut Serveur Soket tcp interactif
    Bonjour , je doit conçevoir un serveur Socket Tcp, acceptant de multiple connexion sur différent ports (si besoin), et entièrement interactif

    J'ai fouillé sur le CPAN j'ai trouver ce module http://search.cpan.org/~rhandom/Net-...r/Multiplex.pm

    Qui me conviendrait mais , après avoir lue et relue la documentation , j'ai toujours rien comprit .

    L'exemple suivant est fournit avec le module mais je voie pas comment l'adapter a mes besoins


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    #!/usr/bin/perl -w
     
    package SampleChatServer;
     
    use strict;
    use Net::Server::Multiplex;
    use vars qw(@ISA);
    @ISA = qw(Net::Server::Multiplex);
     
     
    # Demonstrate a Net::Server style hook
    sub allow_deny_hook {
      my $self = shift;
      my $prop = $self->{server};
      my $sock = $prop->{client};
     
      return 1 if $prop->{peeraddr} =~ /^127\./;
      return 0;
    }
     
     
    # Another Net::Server style hook
    sub request_denied_hook {
      print "Go away!\n";
      print STDERR "DEBUG: Client denied!\n";
    }
     
     
    # IO::Multiplex style callback hook
    sub mux_connection {
      my $self = shift;
      my $mux  = shift;
      my $fh   = shift;
      my $peer = $self->{peeraddr};
      # Net::Server stores a connection counter in the {requests} field.
      $self->{id} = $self->{net_server}->{server}->{requests};
      # Keep some values that I might need while the {server}
      # property hash still contains the current client info
      # and stash them in my own object hash.
      $self->{peerport} = $self->{net_server}->{server}->{peerport};
      # Net::Server directs STDERR to the log_file
      print STDERR "DEBUG: Client [$peer] (id $self->{id}) just connected...\n";
      # Notify everyone that the client arrived
      $self->broadcast($mux,"JOIN: (#$self->{id}) from $peer\r\n");
      # STDOUT is tie'd to the correct IO::Multiplex handle
      print "Welcome, you are number $self->{id} to connect.\r\n";
      # Try out the timeout feature of IO::Multiplex
      $mux->set_timeout($fh, 20);
      # This is my state and will be unique to this connection
      $self->{state} = "junior";
    }
     
     
    # If this callback is ever hooked, then the mux_connection callback
    # is guaranteed to have already been run once (if defined).
    sub mux_input {
      my $self = shift;
      my $mux  = shift;
      my $fh   = shift;
      my $in_ref = shift;  # Scalar reference to the input
      my $peer = $self->{peeraddr};
      my $id   = $self->{id};
     
      print STDERR "DEBUG: input from [$peer] ready for consuming.\n";
      # Process each line in the input, leaving partial lines
      # in the input buffer
      while ($$in_ref =~ s/^(.*?)\r?\n//) {
        next unless $1;
        my $message = "[$id - $peer] $1\r\n";
        $self->broadcast($mux, $message);
        print " - sent ".(length $message)." byte message\r\n";
      }
      if ($self->{state} eq "senior") {
        $mux->set_timeout($fh, 40);
      }
    }
     
     
    # It is possible that this callback will be called even
    # if mux_connection or mux_input were never called.  This
    # occurs when allow_deny or allow_deny_hook fails to
    # authorize the client.  The callback object will be the
    # default listen object instead of a client unique object.
    # However, both object should contain the $self->{net_server}
    # key pointing to the original Net::Server object.
    sub mux_close {
      my $self = shift;
      my $mux  = shift;
      my $fh   = shift;
      my $peer = $self->{peeraddr};
      # If mux_connection has actually been run
      if (exists $self->{id}) {
        $self->broadcast($mux,"LEFT: (#$self->{id}) from $peer\r\n");
        print STDERR "DEBUG: Client [$peer] (id $self->{id}) closed connection!\n";
      }
    }
     
     
    # This callback will happen when the mux->set_timeout expires.
    sub mux_timeout {
      my $self = shift;
      my $mux  = shift;
      my $fh   = shift;
      print STDERR "DEBUG: HEARTBEAT!\n";
      if ($self->{state} eq "junior") {
        print "Whoa, you must have a lot of patience.  You have been upgraded.\r\n";
        $self->{state} = "senior";
      } elsif ($self->{state} eq "senior") {
        print "If you don't want to talk then you should leave. *BYE*\r\n";
        close(STDOUT);
      }
      $mux->set_timeout($fh, 40);
    }
     
     
    # Routine to send a message to all clients in a mux.
    sub broadcast {
      my $self = shift;
      my $mux  = shift;
      my $msg  = shift;
      foreach my $fh ($mux->handles) {
        # NOTE: All the client unique objects can be found at
        # $mux->{_fhs}->{$fh}->{object}
        # In this example, the {id} would be
        #   $mux->{_fhs}->{$fh}->{object}->{id}
        print $fh $msg;
      }
    }
     
     
    __PACKAGE__->run();
    Est ce que je fait fausse route et ce module est bon ? ou sinon

    Pouvez vous m'aider a faire ce serveur ???

    meci !!!!
    Everybody have in their the potential to be their own god : Marilyn Manson

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 46
    Points : 49
    Points
    49
    Par défaut
    Pour un serveur qui écoute du tcp sur une socket :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
     use Socket;
     $SIG{"CHLD"}="IGNORE";# permet de tuer le fils après un fork
    	my $server_port = $le_port_que_tu_désires;
    	my $server_proto = "tcp";
    	my $addr = INADDR_ANY;
    	my $proto = getprotobyname ($server_proto);
    	socket(SOCK_SRV, PF_INET, SOCK_STREAM, $proto) or die "socket : $!\n";
    	setsockopt (SOCK_SRV, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
    	bind (SOCK_SRV, sockaddr_in($server_port,$addr)) or die "bind : $!\n";
    	listen (SOCK_SRV, 5);
    	#On passe les socket en écriture sans buffer
    	select SOCK;
    	$|=1;
    	select STDOUT;
    	$|=1;
    while (1) {
            # On attend une connexion
    	$remote_host = accept (SOCK, SOCK_SRV);
    	# On fork, le père retourne en attente et le fils gère la connexion
    	defined($pid = fork)  or die "impossible de forker : $!\n";
    	if ($pid != 0) {
    		close (SOCK);
    		next;
    	}
    	close (SOCK_SRV);
            # là tu traites ce que tu reçoit sur <SOCK>
            ....................................
            close(SOCK);	
    	exit (0);
    }
    Par contre interactif, je vois pas trop ce que tu veux ?

  3. #3
    Membre actif Avatar de mobscene
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    331
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 331
    Points : 234
    Points
    234
    Par défaut
    Heu par interactif j'entend

    un ensemble client / serveur capable de discuter

    le client envoie un donné au serveur qui la traite et celui si lui renvoie le résultat .
    Everybody have in their the potential to be their own god : Marilyn Manson

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 46
    Points : 49
    Points
    49
    Par défaut
    Bon alors avec mon code tu as la partie serveur, il ne te reste qu'à écrire le traitement de ce que reçoit le server sur la socket SOCK.
    Pour le client, un truc du genre devrait fonctionner :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     my $proto = "tcp";
     my $proto_srv = getprotobyname($proto);
     my $addr_srv = gethostbyname($server);
     socket SOCK_CLI, PF_INET, SOCK_STREAM, $proto_srv or die "socket : $!\n";
     connect(SOCK_CLI, sockaddr_in($port, $addr_srv)) or die "connect : $!\n";
    où $server contient le nom du server et $port le numéro du port

    Une fois la connexion ouverte, il ne te reste plus qu'à transmettre ce que tu veux via la socket SOCK_CLI.

    En espérant t'avoir aidé.

    Rv

  5. #5
    Membre actif Avatar de mobscene
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    331
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 331
    Points : 234
    Points
    234
    Par défaut
    Heu juste trois questions

    dans $server je doit mettre localhost ou 127.0.0.1 parce que j'ai essayé avec les deux bé sa donne rien

    Je voudrais que le serveur retourne au client le résultat des données qu'il a traité comment ont fait sa ???

    Dans le server dans quelle variable , array ou hash je récupère les données que je doit traiter ????
    Everybody have in their the potential to be their own god : Marilyn Manson

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 46
    Points : 49
    Points
    49
    Par défaut
    Citation Envoyé par mobscene
    Heu juste trois questions

    dans $server je doit mettre localhost ou 127.0.0.1 parce que j'ai essayé avec les deux bé sa donne rien
    $server ="localhost" car on utilise gethostbyname, si tu veux utiliser 127.0.0.1 alors c'est gethostbynumber

    Je voudrais que le serveur retourne au client le résultat des données qu'il a traité comment ont fait sa ???
    Dans le server tu fais
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    print SOCK "ce que tu veux"

    Dans le server dans quelle variable , array ou hash je récupère les données que je doit traiter ????
    Tu récupère les données à travers un handle : <SOCK>
    donc un truc du genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    while (<SOCK>) {
     print $_;
    }
    Je ne suis pas chez moi, je te dis tout ça de mémoire, donc à vérifier dans perldoc !!

    Rv

Discussions similaires

  1. Serveur / client TCP
    Par shepounet dans le forum Réseau
    Réponses: 6
    Dernier message: 24/11/2010, 14h09
  2. programme client serveur mode TCP/UDP en java
    Par ouss01 dans le forum Débuter avec Java
    Réponses: 1
    Dernier message: 03/04/2009, 09h05
  3. Serveur / client tcp
    Par margou dans le forum Débuter
    Réponses: 3
    Dernier message: 08/06/2008, 10h47
  4. [Réseau] Problème Serveur Client TCP linux embarqué
    Par FabienpERRIN dans le forum Réseau
    Réponses: 2
    Dernier message: 31/07/2007, 20h47
  5. Réponses: 2
    Dernier message: 23/10/2006, 13h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo