0
J'essaie de rediriger vers un socket la sortie d'un autre programme, J'ai essayé avec dup2 (STDOUT_FILE, socketfd); execl ("/ bin/ls", "ls", (char *) 0) dans le code source du serveur, et read (socketfd, buff, 1) dans le code source du client .. Mais la sortie du client semble juste une chaîne vide ..C - La redirection ne fonctionne pas
int main(){
struct sockaddr_in my_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(5200);
my_addr.sin_addr.s_addr= htonl (INADDR_ANY);
int fd1, fd2;
fd1=socket (PF_INET, SOCK_STREAM, 0);
bind (fd1, (struct sockaddr *) &my_addr, sizeof(my_addr));
listen (fd1, 5);
while (1){
fd2= accept (fd1, NULL,NULL);
char *stringa,*mex;
int lenght;
struct stat buf;
mex=(char*)malloc(sizeof(int)+1);
read (fd2,&lenght,sizeof(int));
stringa=(char*)malloc(sizeof(char)*lenght);
read(fd2,stringa,lenght);
write (1,"\n",1);
if (lstat(stringa, &buf)<0){
sprintf(mex,"%d",-1);
write (fd2,mex,sizeof(int));
}
else if (S_ISDIR(buf.st_mode)){
DIR* direct= opendir(stringa);
struct dirent *directory;
int howmany=0;
while((directory=readdir(direct))!=NULL){
lstat(directory->d_name,&buf);
if (S_ISREG(buf.st_mode))
howmany++;
}
sprintf(mex,"%d",howmany);
write (fd2,mex,sizeof(int));
}else if (buf.st_mode & S_IXUSR){
dup2(STDOUT_FILENO, fd2);
execl("/bin/ls","ls",(char*)0);
}
write (fd2,"$",1);
}
}`
n'a pas d'importance, je suis juste essayer de pratiquer la redirection de stdout de la fonction exec .. Alors, comment puis-je faire? Je ne pense pas que lancer le retour de malloc signifie quelque chose pour mon problème .. –