Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 22nd, 2004, 4:00 AM   #1
tinah_pro
Newbie
 
Join Date: Aug 2004
Posts: 2
Rep Power: 0 tinah_pro is on a distinguished road
I'm programming a remote shell using the protocol UDP.
Therefore a server (server_udp.c) to listen one port number, and a client (client_udp.c) which send the command to the server, get the result of command and display it in the client screen.

The communication is already work but the my problem concerns the redirection of the standard output (1) in a file descriptor:
// server_udp.c

if(dup2(fd,1)<0){
perror("Error dup2"); exit(1);
}
if(read(fd,buf_response,1024)<0) {
perror("Error dup2"); exit(1);
}
puts(buf_response);

And it displays nothing. The string "buf_response" is empty.
I think that you'd understand and can help me.
Thanks
tinah_pro is offline   Reply With Quote
Old Aug 22nd, 2004, 11:53 PM   #2
sarumont
Hobbyist Programmer
 
sarumont's Avatar
 
Join Date: Apr 2004
Location: /dev/urandom
Posts: 154
Rep Power: 5 sarumont is on a distinguished road
Send a message via ICQ to sarumont Send a message via AIM to sarumont Send a message via Yahoo to sarumont
Can you post all of the server? Or at least more of the pertinent blocks of the code?
__________________
"Time is an illusion. Lunchtime doubly so."
-the late, great Douglas Adams
sarumont is offline   Reply With Quote
Old Aug 22nd, 2004, 11:54 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I agree.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 23rd, 2004, 12:37 PM   #4
tinah_pro
Newbie
 
Join Date: Aug 2004
Posts: 2
Rep Power: 0 tinah_pro is on a distinguished road
// server_udp.c
[user@localhost user]$ cc -o server server_udp.c
[user@localhost user]$ server 1024
the server listen the port #1024

//server_udp.c
#include <unistd.h>
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <signal.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <arpa/inet.h>
   #include <ctype.h>

   #define TAILLE_TAMPON 1000
   #define CMD "CMD_OK"
   static int fd, fd2;

   void ErreurFatale(char message[])
   {
       printf("SERVEUR> Erreur fatale\n");
       perror(message);
       exit(EXIT_FAILURE);
   }

   void ArretServeur(int signal)
   {
       close(fd);
       printf("SERVEUR> Arrêt du serveur (signal %d)\n", signal);
       exit(EXIT_SUCCESS);
   }

   int main(int argc, char *argv[])
   {
       struct sockaddr_in adresse_serveur;
       size_t taille_adresse_serveur;
       int numero_port_serveur, nb_car_lu, i;
       char *src, *dst;
       struct sigaction a;

       /* 1. réception des paramètres de la ligne de commande */

       if (argc != 2) {
           printf("usage: %s port\n", argv[0]);
           exit(1);
       };
       numero_port_serveur = atoi(argv[1]);

       /* 2. Si une interruption se produit, arrêt du serveur */
       /* signal(SIGINT,ArretServeur); */

       a.sa_handler = ArretServeur;
       sigemptyset(&a.sa_mask);
       a.sa_flags = 0;
       sigaction(SIGINT, &a, NULL);

       /* 3. Initialisation du socket de réception */

       /* 3.1 Création du socket en mode non-connecté
         (datagrammes) */

       fd = socket(AF_INET, SOCK_DGRAM, 0);
       if (fd < 0)
           ErreurFatale("socket");

       /* 3.2 Remplissage de l'adresse de réception
         (protocole Internet TCP-IP, réception acceptée sur toutes
         les adresses IP du serveur, numéro de port indiqué)
        */

       adresse_serveur.sin_family = AF_INET;
       adresse_serveur.sin_addr.s_addr = INADDR_ANY;
       adresse_serveur.sin_port = htons(numero_port_serveur);

       /* 3.3 Association du socket au port de réception */

       taille_adresse_serveur = sizeof adresse_serveur;
       if (bind(fd,
            (struct sockaddr *) &adresse_serveur,
            taille_adresse_serveur) < 0)
           ErreurFatale("bind");

       printf("SERVEUR> Le serveur écoute le port %d\n",
           numero_port_serveur);

       while (1) {
           struct sockaddr_in adresse_client;
           int taille_adresse_client;
           char tampon_requete[TAILLE_TAMPON],
             tampon_reponse[TAILLE_TAMPON];
           int lg_requete, lg_reponse;

           /* 4. Attente d'un datagramme (requête) */

           taille_adresse_client = sizeof(adresse_client);
           lg_requete = recvfrom(fd, tampon_requete, TAILLE_TAMPON, 0,   /* flags */
                      (struct sockaddr *)
                      &adresse_client,
                      (socklen_t *) &
                      taille_adresse_client);
           if (lg_requete < 0)
               ErreurFatale("recfrom");

           /* 5. Affichage message avec sa provenance et sa longueur */

           printf("%s:%d [%d]\t: %s\n",
               inet_ntoa(adresse_client.sin_addr),
               ntohs(adresse_client.sin_port),
               lg_requete, tampon_requete);
    system(tampon_requete);
    puts("OK 0");
    if(dup2(fd2,1)<0) ErreurFatale("Erreur dup2");
    if(read(newout,tampon_reponse,TAILLE_TAMPON)<0) ErreurFatale("Erreur dup2");
    puts("OK 1");
    puts(tampon_reponse);

    /* 6. Fabrication d'une réponse */
           src = tampon_requete;
           dst = tampon_reponse;
           while ((*dst++ = toupper(*src++)) != '\0');
    puts("OK 2");
    puts(tampon_reponse);
           lg_reponse = strlen(tampon_reponse) + 1;
 	puts("OK 3");
           /* 7. Envoi de la réponse */

           if (sendto(fd2,
                 tampon_reponse,
                lg_reponse,
                0,
                (struct sockaddr *) &adresse_client,
                taille_adresse_client)
             < 0)
               ErreurFatale("Envoi de la réponse");
       };
   }

// client_udp.c
[user@localhost user]$ cc -o client client_udp.c
[user@localhost user]$ client localhost 1024 ls
// client_udp.c
   #include <unistd.h>
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <signal.h>
   #include <stdio.h>
   #include <netdb.h>
   #include <string.h>
   #include <stdlib.h>

   #define TAILLE_TAMPON 1000
   static int fd;

   void ErreurFatale(char message[])
   {
       printf("CLIENT> Erreur fatale\n");
       perror(message);
       exit(EXIT_FAILURE);
   }

   int main(int argc, char *argv[])
   {
       struct sockaddr_in adresse_socket_serveur;
       struct hostent *hote;
       int taille_adresse_socket_serveur;
       char *nom_serveur;
       int numero_port_serveur;
       char *requete, reponse[TAILLE_TAMPON];
       int longueur_requete, longueur_reponse;
       /* 1. réception des paramètres de la ligne de commande */

       if (argc != 4) {
           printf("Usage: %s hote port message\n", argv[0]);
           exit(EXIT_FAILURE);
       };
       nom_serveur = argv[1];
       numero_port_serveur = atoi(argv[2]);
       requete = argv[3];
       /* 2. Initialisation du socket */

       /* 2.1 création du socket en mode datagramme */

       fd = socket(AF_INET, SOCK_DGRAM, 0);
       if (fd < 0)
           ErreurFatale("Creation socket");

       /* 2.2 recherche de la machine serveur */

       hote = gethostbyname(nom_serveur);
       if (hote == NULL)
           ErreurFatale("Recherche serveur");

       /* 2.3 Remplissage adresse serveur */

       adresse_socket_serveur.sin_family = AF_INET;
       adresse_socket_serveur.sin_port = htons(numero_port_serveur);
       adresse_socket_serveur.sin_addr =
         *(struct in_addr *) hote->h_addr;

       taille_adresse_socket_serveur = sizeof adresse_socket_serveur;

       longueur_requete = strlen(requete) + 1;

       /* 3. Envoi de la requête */
       printf("REQUETE> %s\n", requete);
       if (sendto(fd, requete, longueur_requete, 0,  /* flags */
            (struct sockaddr *) &adresse_socket_serveur,
            taille_adresse_socket_serveur)
         < 0)
           ErreurFatale("Envoi requete");

       /* 4. Lecture de la réponse */
       longueur_reponse = recvfrom(fd,
                     reponse,
                     TAILLE_TAMPON, 0, NULL, 0);
       if (longueur_reponse < 0)
           ErreurFatale("Attente réponse");
       printf("REPONSE> %s\n", reponse);
       close(fd);

       printf("CLIENT> Fin.\n");
       exit(0);
   }

that's all
tinah_pro is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC