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 :

Client serveur multicast en C sous windows


Sujet :

Réseau C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    39
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 39
    Par défaut Client serveur multicast en C sous windows
    (Re) Bonjour,

    J'ai posté 2 sujet sur le forum que j'ai finalement pu résoudre tout seul, mais là je suis bloqué.

    J'essaye de faire un client serveur multicast sous windows.

    J'ai donc créé un programme "sender" qui envoi du text dont voici le code :
    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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <time.h>
     
    #include "client.h"
     
    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
    }
     
    static void app(const char *address, const char *name)
    {
       SOCKADDR_IN sin = { 0 };
       SOCKET sock = init_connection(address, &sin);
       char buffer[BUF_SIZE];
       strcpy(buffer,"hello i am your server you must obey !");
       printf("the sent buffer is %s\n",buffer);
       while(1)
       {
     
           time_t t = time(0);    /* get current time */
           sprintf(buffer, "time is %-24.24s", ctime(&t)); /* make readable */
           printf("sending: %s\n", buffer);
           write_server(sock, &sin, buffer);
           sleep(3);
       }
     
       end_connection(sock);
    }
     
    static int init_connection(const char *address, SOCKADDR_IN *sin)
    {
       /* UDP so SOCK_DGRAM */
       SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
       struct hostent *hostinfo;
     
       if(sock == INVALID_SOCKET)
       {
          perror("socket()");
          exit(errno);
       }
     
       hostinfo = gethostbyname(address);
       if (hostinfo == NULL)
       {
          fprintf (stderr, "Unknown host %s.\n", address);
          exit(EXIT_FAILURE);
       }
     
     
         sin->sin_addr.s_addr = htonl(address);//*(IN_ADDR *) hostinfo->h_addr;
       sin->sin_port = htons(PORT);
       sin->sin_family = AF_INET;
     
       return sock;
    }
     
    static void end_connection(int sock)
    {
       closesocket(sock);
    }
     
    static int read_server(SOCKET sock, SOCKADDR_IN *sin, char *buffer)
    {
       int n = 0;
       size_t sinsize = sizeof *sin;
     
       if((n = recvfrom(sock, buffer, BUF_SIZE - 1, 0, (SOCKADDR *) sin, &sinsize)) < 0)
       {
          perror("recvfrom()");
          exit(errno);
       }
     
       buffer[n] = 0;
     
       return n;
    }
     
    static void write_server(SOCKET sock, SOCKADDR_IN *sin, const char *buffer)
    {
       if(sendto(sock, buffer, strlen(buffer), 0, (SOCKADDR *) sin, sizeof *sin) < 0)
       {
          perror("sendto()");
          exit(errno);
       }
    }
     
    int main(int argc, char **argv)
    {
       if(argc < 2)
       {
          printf("Usage : %s [address] [pseudo]\n", argv[0]);
          return EXIT_FAILURE;
       }
     
       init();
       printf("port emission  sender = %d; adresse broadcast=%s",PORT, argv[1]);
     
       app(argv[1], argv[2]);
     
       end();
     
       return EXIT_SUCCESS;
    }
     
     
     
    Puis un programme "receiver" qui est sensé lire le text envoyé :
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <time.h>
     
     
    #include "client.h"
     
    static void init(void)
    {
    #ifdef WIN32
    printf("init in");   
        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
    }
     
    static void app(const char *address, const char *name) {
        SOCKADDR_IN sin = {0
        };
        struct ip_mreq mreq;
        SOCKET sock = init_connection(address, &sin, &mreq);
        char buffer[BUF_SIZE];
     
     
        /* send our name */
        //write_server(sock, &sin, name);
     
     
     
     
     
        while (1) {
            /* set the timer to 1.5 second */
          struct timeval timeout;
     
          timeout.tv_sec = 1; /* 1 s */
          timeout.tv_usec = 5 * 100 * 1000; /* 500 ms */
            fd_set rdfs;
            FD_ZERO(&rdfs);
     
            FD_SET(sock, &rdfs);
            //FD_ISSET(sock, &rdfs);
     
            int err = select(FD_SETSIZE, &rdfs, NULL, NULL, &timeout);
     
            switch (err) {
                case 0:
                    /* timeout */
                    break;
     
                case -1 :
                    /* error */
                    perror("error select !!!\n");
                    printf("errno=%d\n",errno);
                    exit(1);
                    break;
     
                default:
                    /* stream event */
     
                    /* - data has been received */
     
                    if (FD_ISSET(sock, &rdfs)) {
                        int n = read_server(sock, &sin, buffer);
                        printf("valeur de n=%d",n);
                        /* server down */
                        if (n == 0) {
                            printf("Server disconnected !\n");
                            break;
                        }
                        puts(buffer);
                    }
            }
        }
     
        end_connection(sock);
    }
     
    static int init_connection(const char *address, SOCKADDR_IN *sin, struct ip_mreq *mreq)
    {
       /* UDP so SOCK_DGRAM */
       SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
       struct hostent *hostinfo;
       u_int yes=1;            /*** MODIFICATION TO ORIGINAL */
       //SOCKADDR_IN addr;
       if(sock == INVALID_SOCKET)
       {
          perror("socket()");
          exit(errno);
       }
     
       hostinfo = gethostbyname(address);
       if (hostinfo == NULL)
       {
          fprintf (stderr, "Unknown host %s.\n", address);
          exit(EXIT_FAILURE);
       }
     
     
       /**** MODIFICATION TO ORIGINAL */
        /* allow multiple sockets to use the same PORT number */
        if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes)) < 0) {
           perror("Reusing ADDR failed");
           exit(1);
           }
     
    /*** END OF MODIFICATION TO ORIGINAL */
     
     
       /* memset(&addr, 0, sizeof(addr));
        addr.sin_addr.s_addr = htonl(INADDR_ANY);//*(IN_ADDR *) hostinfo->h_addr;
       addr.sin_port = htons(PORT);
       addr.sin_family = AF_INET;
        */
     
       sin->sin_addr.s_addr = htonl(INADDR_ANY);//*(IN_ADDR *) hostinfo->h_addr;
       sin->sin_port = htons(PORT);
       sin->sin_family = AF_INET;
     
       int error=bind(sock,(struct sockaddr *) sin,sizeof(*sin));
       printf("error=%d",error);
       if (error < 0) {
    	  perror("bind");
    	  exit(1);
         }
     
         /* use setsockopt() to request that the kernel join a multicast group */
         mreq->imr_multiaddr.s_addr=inet_addr(address);
         mreq->imr_interface.s_addr=htonl(INADDR_ANY);
         error=setsockopt(sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char*) mreq,sizeof(*mreq));
         if (error < 0) {
    	  perror("setsockopt2");
    	  exit(1);
         }
     
     
       return sock;
    }
     
    static void end_connection(int sock)
    {
       closesocket(sock);
    }
     
    static int read_server(SOCKET sock, SOCKADDR_IN *sin, char *buffer)
    {
       int n = 0;
       size_t sinsize = sizeof *sin;
     
       if((n = recvfrom(sock, buffer, BUF_SIZE - 1, 0, (SOCKADDR *) sin, &sinsize)) < 0)
       {
          perror("recvfrom()");
          exit(errno);
       }
     
       buffer[n] = 0;
     
       return n;
    }
     
    static void write_server(SOCKET sock, SOCKADDR_IN *sin, const char *buffer)
    {
       if(sendto(sock, buffer, strlen(buffer), 0, (SOCKADDR *) sin, sizeof *sin) < 0)
       {
          perror("sendto()");
          exit(errno);
       }
    }
     
    int main_1(int argc, char **argv)
    {
       if(argc < 2)
       {
          printf("Usage : %s [address] [pseudo]\n", argv[0]);
          return EXIT_FAILURE;
       }
     
       init();
       printf("port ecoute receiver = %d; adresse broadcast=%s",PORT, argv[1]);
       app(argv[1], argv[2]);
     
       end();
     
       return EXIT_SUCCESS;
    }

    Pour plus de lisibilité j'attache mes sources également.

    Mon problème c'est que le select plante et si jamais je commente le code du select le recvfrom bloque sans rien recevoir.

    Par pitié aidez moi ça fait trois jour que je me creuse les méninges.

    Merci à tous.
    Fichiers attachés Fichiers attachés

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    39
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 39
    Par défaut
    Bon,

    Je vois que mon message n'a pas vraiment eu beaucoup de succes.

    On dirait que c'est sur le recvfrom que cela bloque. le client attend alors que le serveur ne cesse d'emettre.

    Y a t il quelque chose que j'ai loupé ???

    Merci par avance.

  3. #3
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    Je ne vois pas de bind dans ton émetteur.
    Je ne vois pas de P_DROP_MEMBERSHIP dans ton récepteur.

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    39
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 39
    Par défaut
    Je pensais que l'emetteur ne devais pas faire de bind.
    Je l'ai quand même rajouté au cas où sans succès.

    Merci quand même.

  5. #5
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    As-tu essayé de positionner ta socket en non bloquant et de vérifier la réception.

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    39
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 39
    Par défaut
    Non je n'ai pas essayé.
    Je dois avouer que je ne connais pas ce mode as tu un exemple ?
    Sinon je vais chercher.

    Merci,

  7. #7
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    De mémoire cela doit être quelque chose comme ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    const unsigned long L_ulNonBlocking = 1;
    ioctlsocket(ma_socket, FIONBIO, &L_ulNonBlocking);

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

Discussions similaires

  1. Réponses: 7
    Dernier message: 22/06/2011, 09h49
  2. client serveur multicast en C sous windows
    Par maarek dans le forum Réseau
    Réponses: 1
    Dernier message: 13/07/2008, 17h17
  3. Client FireBird pour un pda sous Windows CE.NET ?
    Par Invité dans le forum Installation
    Réponses: 1
    Dernier message: 04/02/2005, 14h21

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