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