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

Réseau C Discussion :

problème pour utiliser fonction system


Sujet :

Réseau C

  1. #1
    Membre averti Avatar de Neolex
    Homme Profil pro
    Recherche emploi Securité informatique
    Inscrit en
    Avril 2011
    Messages
    243
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Recherche emploi Securité informatique

    Informations forums :
    Inscription : Avril 2011
    Messages : 243
    Points : 333
    Points
    333
    Par défaut problème pour utiliser fonction system
    Bonjour à tous ,

    J'ai commencer à suivre hier le cours sur les sockets en C ( de broux ) .

    J'ai donc essayé un programme avec un serveur et un client , le client envoi une chaîne de caractere ("ls") pour l'instant .
    Le code fonctionne lorsque je veux afficher cette chaîne avec un printf("%s",buffer");
    "ls" est bien affiché ...
    par contre lorsque j'essaye de faire mon "server" me renvoi
    sh: 1: lsDz�: not found
    Je ne comprends donc pas pourquoi buffer est bien "ls" lorsque je veux juste l'afficher , et "lsDz�" lorsque je l'utilise dans la fonction system() ...

    Voici mes codes sources

    Server.c :
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include "server.h"
    #define PORT 4445
     
     
    int main()
    {	
    	init();
        SOCKET sock = socket(PF_INET, SOCK_STREAM, 0);
        if(sock == INVALID_SOCKET)
        {
            perror("socket()");
            exit(errno);
        }
     
        SOCKADDR_IN sin = { 0 };
     
        sin.sin_addr.s_addr = htonl(INADDR_ANY); /* nous sommes un serveur, nous acceptons n'importe quelle adresse */
     
        sin.sin_family = AF_INET;
     
        sin.sin_port = htons(PORT);
     
        if(bind (sock, (SOCKADDR *) &sin, sizeof sin) == SOCKET_ERROR)
        {
            perror("bind()");
            exit(errno);
        }
     
     
        if(listen(sock, 5) == SOCKET_ERROR)
        {
            perror("listen()");
            exit(errno);
        }
     
     
        SOCKADDR_IN csin = { 0 };
        SOCKET csock;
     
        socklen_t sinsize = sizeof csin;
     
        csock = accept(sock, (SOCKADDR *)&csin, &sinsize);
     
        if(csock == INVALID_SOCKET)
        {
            perror("accept()");
            exit(errno);
        }
    	char buffer[1024];
        read(csock,&buffer,1024);
    	 buffer[strlen(buffer)+1]='\0';
     
    	//printf("%s\n",buffer);
    	system(buffer);
        closesocket(sock);
        closesocket(csock);
     
    	end();
        return 0 ;
    }
     
     
     
     
     
     
     
     
    static void init(void)
    {
    #ifdef WIN32
        WSADATA wsa;
        int err = WSAStartup(MAKEWORD(2, 2), &wsa);
        if(err < 0)
        {
            puts("WSAStartup failed !");
            exit(EXIT_FAILURE);
        }
    #endif
    }
     
    static void end(void)
    {
    #ifdef WIN32
        WSACleanup();
    #endif
    }
    client.c :
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include "client.h"
    #define PORT 4445
     
     
    int main()
    {
        init();
        SOCKET sock = socket(PF_INET, SOCK_STREAM, 0);
        if(sock == INVALID_SOCKET)
        {
            perror("socket()");
            exit(errno);
        }
     
      /* struct hostent *hostinfo = NULL;
        const char *hostname = "www.developpez.com";
     
        hostinfo = gethostbyname(hostname); // on récupère les informations de l'hôte auquel on veut se connecter
     
        if (hostinfo == NULL) // l'hôte n'existe pas
        {
            fprintf (stderr, "Unknown host %s.\n", hostname);
            exit(EXIT_FAILURE);
        }
    */
     
        SOCKADDR_IN sin = { 0 }; // initialise la structure avec des 0
       //sin.sin_addr = *(IN_ADDR *) hostinfo->h_addr; /* l'adresse se trouve dans le champ h_addr de la structure hostinfo */
        sin.sin_addr.s_addr = inet_addr("192.168.1.10");
        sin.sin_port = htons(PORT); /* on utilise htons pour le port */
        sin.sin_family = AF_INET;
     
        if(connect(sock,(SOCKADDR *) &sin, sizeof(SOCKADDR)) == SOCKET_ERROR)
        {
            perror("connect()");
            exit(errno);
        }
    	char buffer[1024] = {"ls"};
        if(send(sock, buffer, strlen(buffer), 0) < 0)
        {
            perror("send()");
            exit(errno);
        }
     
     
        closesocket(sock);
        end();
        return 0;
    }
     
     
     
     
     
    static void init(void)
    {
    #ifdef WIN32
        WSADATA wsa;
        int err = WSAStartup(MAKEWORD(2, 2), &wsa);
        if(err < 0)
        {
            puts("WSAStartup failed !");
            exit(EXIT_FAILURE);
        }
    #endif
    }
     
    static void end(void)
    {
    #ifdef WIN32
        WSACleanup();
    #endif
    }

    Merci

  2. #2
    Membre émérite
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2011
    Messages
    1 255
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2011
    Messages : 1 255
    Points : 2 627
    Points
    2 627
    Par défaut
    salut

    Ton problème est flagrant, il ne vient pas de la fonction system mais de ce que tu lui donnes en paramètre.
    il faut que tu testes le code retour de read pour savoir quelle est la longueur réellement lue.
    Grosso modo ça donne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    	char buffer[1024];
             int retRead = read(csock,&buffer,1024);
    .... test retRead est != -1 ....
    	 buffer[retRead]='\0';

  3. #3
    Membre averti Avatar de Neolex
    Homme Profil pro
    Recherche emploi Securité informatique
    Inscrit en
    Avril 2011
    Messages
    243
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Recherche emploi Securité informatique

    Informations forums :
    Inscription : Avril 2011
    Messages : 243
    Points : 333
    Points
    333
    Par défaut
    Merci de ta réponse si rapide .

    Effectivement , une fois ta réponse donnée ça me parait évident...

    J'avais pensé que strlen(buffer)+1 aurais fonctionné mais visiblement non .

    Merci beaucoup à toi , désolé du dérangement !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème pour utiliser ma fonction
    Par nicosmash dans le forum Langage
    Réponses: 5
    Dernier message: 02/11/2012, 22h09
  2. Réponses: 9
    Dernier message: 29/04/2008, 13h38
  3. Problème pour utiliser JWS
    Par yas2006 dans le forum JWS
    Réponses: 11
    Dernier message: 02/07/2007, 13h28
  4. Problème pour utiliser split avec "\"
    Par Nicolas_555 dans le forum Langage
    Réponses: 6
    Dernier message: 03/08/2006, 14h42
  5. Réponses: 1
    Dernier message: 05/04/2006, 14h22

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