Précédent   Forum du club des développeurs et IT Pro > C et C++ > C > Réseau
Réseau Forum d'entraide sur la programmation réseau en C
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 11/09/2012, 16h03   #1
Neolex
Invité régulier
 
Homme Kévin
Étudiant
Inscription : avril 2011
Messages : 21
Détails du profil
Informations personnelles :
Nom : Homme Kévin
Âge : 19
Localisation : France, Finistère (Bretagne)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : avril 2011
Messages : 21
Points : 8
Points : 8
Envoyer un message via MSN à Neolex
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
Citation:
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 :
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 :
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
Neolex est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/09/2012, 16h22   #2
mala92
Expert Confirmé
 
Homme
Développeur informatique
Inscription : décembre 2011
Messages : 1 250
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Développeur informatique

Informations forums :
Inscription : décembre 2011
Messages : 1 250
Points : 2 524
Points : 2 524
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 :
1
2
3
4
	char buffer[1024];
         int retRead = read(csock,&buffer,1024);
.... test retRead est != -1 ....
	 buffer[retRead]='\0';
mala92 est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 11/09/2012, 17h30   #3
Neolex
Invité régulier
 
Homme Kévin
Étudiant
Inscription : avril 2011
Messages : 21
Détails du profil
Informations personnelles :
Nom : Homme Kévin
Âge : 19
Localisation : France, Finistère (Bretagne)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : avril 2011
Messages : 21
Points : 8
Points : 8
Envoyer un message via MSN à Neolex
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 !
Neolex est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 03h12.


 
 
 
 
Partenaires

Hébergement Web