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 :

Connexion Multi Clients


Sujet :

Réseau C

  1. #1
    Membre à l'essai

    Homme Profil pro
    Inscrit en
    Février 2012
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2012
    Messages : 3
    Points : 22
    Points
    22
    Par défaut Connexion Multi Clients
    Bonsoir !

    Je programme une application serveur qui accepte plusieurs clients, elle liste les connexions possibles puis, c'est a l'utilisateur de choisir avec quel client il va se connecter et échanger des fichiers, le probleme, c'est que, lorsque je sélectionne le client avec qui je veux me connecter, rien ne lui est envoyé.

    Voici mon code : (Je suis débutant en programmation réseau, et je n'ai eu aucun support donc ça peux vous paraitre désastreux, et je m'en excuse...)

    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <winsock2.h>
    typedef int socklen_t;
     
    typedef struct INFO_CLIENT INFO_CLIENT;
     
    struct INFO_CLIENT
    {
        int client_valide;
        SOCKET csock;
        SOCKADDR_IN csin;
    };
     
    int main()
    {
        WSADATA WSAData;
        int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
     
        SOCKET sock;
        SOCKADDR_IN sin;
        int sock_err;
     
        struct timeval tv;
     
        int PORT = 0;
     
        INFO_CLIENT sock_client[10];
     
        socklen_t crecsize;
        crecsize = sizeof(sock_client[0].csin);
     
        tv.tv_sec = 8;
        tv.tv_usec = 0;
     
        int i;
        char PATH[50] = {'\0'}, extension[10] = {'\0'}, NEW_PATH[50] = {'\0'}, chaine[500] = {'\0'};
        char caractere;
        int nombreConnexion = 1, clientChoisie = 0, clientTotal = 0;
     
        if(!erreur)
        {
            sock = socket(AF_INET, SOCK_STREAM, 0);
     
            /* Si la socket est valide */
            if(sock != INVALID_SOCKET)
            {
                /* Configuration */
                sin.sin_addr.s_addr    = htonl(INADDR_ANY);   /* Adresse IP automatique */
                sin.sin_family         = AF_INET;             /* Protocole familial (IP) */
                sin.sin_port           = htons(PORT);         /* Listage du port */
                sock_err = bind(sock, (SOCKADDR*)&sin, sizeof(sin));
     
                /* Si la socket fonctionne */
                if(sock_err != SOCKET_ERROR)
                {
                    /* Démarrage du listage (mode server) */
                    sock_err = listen(sock, 5);
     
                    /* Si la socket fonctionne */
                    if(sock_err != SOCKET_ERROR)
                    {
                        nombreConnexion--;
     
                        clientTotal = nombreConnexion;
     
                        if((nombreConnexion+1) > 1)
                        {
                            printf("\nRecherche de %d connexion(s) sur le port %d...\n", nombreConnexion+1, PORT);
     
                            fd_set readfs;
     
                            FD_ZERO(&readfs);
                            FD_SET(sock, &readfs);
     
                            while(nombreConnexion >= 0)
                            {
                                if((select(sock + 1, &readfs, NULL, NULL, &tv)) == 0)
                                {
                                    printf("\n\n%d. Aucune connexion", nombreConnexion);
                                    FD_ZERO(&readfs);
                                    FD_SET(sock, &readfs);
                                }
     
                                else
                                {
                                    sock_client[nombreConnexion].client_valide = 1;
                                    sock_client[nombreConnexion].csock = accept(sock, (SOCKADDR*)&sock_client[nombreConnexion].csin, &crecsize);
                                    printf("\n\n%d. %s:%d  ->  ", nombreConnexion, inet_ntoa(sock_client[nombreConnexion].csin.sin_addr), htons(sock_client[nombreConnexion].csin.sin_port));
                                    if(nomIP == 'O')
                                    voirClient(inet_ntoa(sock_client[nombreConnexion].csin.sin_addr), 0);
                                }
     
                                nombreConnexion--;
                            }
     
                            printf("\n\n\nChoisissez le client (100 pour tous) : ");
                            scanf("%d", &clientChoisie);
     
                            nombreConnexion = clientTotal;
     
                            if(modeRecherche == 2)
                            {
                                printf("\n\n");
                                menu(PATH, NEW_PATH, chaine, extension);
     
                                if((fichier = fopen(PATH, "rb")) == NULL)
                                    {
                                        printf("\nImpossible d'ouvrir : %s\n\n", PATH);
                                        goto stop;
                                    }
     
                                fseek(fichier, 0, SEEK_END);
                                tailleFichier = ftell(fichier);
                                rewind(fichier);
                            }
     
                            if(clientChoisie != 100)
                            {
                                if(nomIP == 'O')
                                voirClient(inet_ntoa(sock_client[clientChoisie].csin.sin_addr), 2);
     
                                while(nombreConnexion >= 0)
                                {
                                    if(nombreConnexion != clientChoisie && sock_client[nombreConnexion].client_valide != 0)
                                    {
                                        sprintf(NEW_PATH, "RIEN");
                                        send(sock_client[nombreConnexion].csock, NEW_PATH, sizeof(NEW_PATH), 0);
                                        shutdown(sock_client[nombreConnexion].csock, 2);
                                        closesocket(sock_client[nombreConnexion].csock);
                                    }
     
                                    nombreConnexion--;
                                }
                            }
                        }
     
                        else
                        {
                            if(IPaRechercher[0] == 'N')
                            {
                                printf("\nRecherche d'une connexion sur le port %d...\n\n\n", PORT);
     
                                sock_client[1].csock = accept(sock, (SOCKADDR*)&sock_client[1].csin, &crecsize);
                                clientChoisie = 1;
     
                                printf("Connecte avec %s:%d  ->  ", inet_ntoa(sock_client[1].csin.sin_addr), htons(sock_client[1].csin.sin_port));
                                if(nomIP == 'O')
                                voirClient(inet_ntoa(sock_client[1].csin.sin_addr), 1);
     
                                printf("\n");
                            }
     
                            else
                            {
                                printf("\nRecherche de l'IP suivante : %s\n\n", IPaRechercher);
     
                                while(clientChoisie != 10)
                                {
                                    sock_client[1].csock = accept(sock, (SOCKADDR*)&sock_client[1].csin, &crecsize);
     
                                    if(strcmp(IPaRechercher, inet_ntoa(sock_client[1].csin.sin_addr)) == 0)
                                        clientChoisie = 10;
     
                                    else
                                    {
                                        shutdown(sock_client[1].csock, 2);
                                        closesocket(sock_client[1].csock);
                                    }
                                }
     
                                clientChoisie = 1;
                            }
                        }
     
                        if(clientChoisie == 100)
                        {
                            for(i = nombreConnexion; i >= 0; i--)
                            {
                                if(sock_client[i].client_valide != 0)
                                {
                                    if(send(sock_client[i].csock, NEW_PATH, sizeof(NEW_PATH), 0) != SOCKET_ERROR);
     
                                    else
                                    printf("Connexion interrompue avec le client %d\n", i);
                                }
                            }
     
                            printf("\n\nEnvoie du fichier \"%s\" a tout les clients en cour...\n\n", PATH);
     
                            while(!feof(fichier))
                            {
                                caractere = fgetc(fichier);
     
                                for(i = nombreConnexion; i >= 0; i--)
                                {
                                    if(sock_client[i].client_valide != 0)
                                    {
                                        sock_err = send(sock_client[i].csock, &caractere, sizeof(caractere), 0);
     
                                        if(sock_err == SOCKET_ERROR)
                                        {
                                            printf("\nErreur transmission client numero : %d\n", i);
                                            goto stop;
                                        }
                                    }
                                }
                            }
                        }
     
     
                        else
                        {
                            if(send(sock_client[clientChoisie].csock, NEW_PATH, sizeof(NEW_PATH), 0) != SOCKET_ERROR)
                                printf("\n\nEnvoie du fichier \"%s\" en cour...\n\n", PATH);
     
                            else
                            {
                                printf("Erreur de transmission\n");
                                goto stop;
                            }
     
                            while(!feof(fichier))
                                {
                                    caractere = fgetc(fichier);
     
                                    sock_err = send(sock_client[clientChoisie].csock, &caractere, sizeof(caractere), 0);
     
                                    if(sock_err == SOCKET_ERROR)
                                    {
                                        printf("Erreur de transmission\n");
                                        goto stop;
                                    }
                                }
                        }
                    }
                }
            }
        }
     
        stop :
     
        if(clientChoisie == 100)
        {
            for(i = nombreConnexion; i >= 0; i--)
            {
                if(sock_client[i].client_valide != 0)
                {
                    shutdown(sock_client[i].csock, 2);
                    closesocket(sock_client[i].csock);
                }
            }
        }
     
        else
        {
            shutdown(sock_client[clientChoisie].csock, 2);
            closesocket(sock_client[clientChoisie].csock);
        }
     
        closesocket(sock);
     
        WSACleanup();
        return EXIT_SUCCESS;
    }

  2. #2
    Membre à l'essai

    Homme Profil pro
    Inscrit en
    Février 2012
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2012
    Messages : 3
    Points : 22
    Points
    22
    Par défaut
    Le probleme viens au niveau du while -> sprintf(NEW_PATH, "RIEN");

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

Discussions similaires

  1. Réponses: 0
    Dernier message: 07/01/2013, 16h06
  2. connexion multi-clients / serveur
    Par maxou483 dans le forum Plateformes (Java EE, Jakarta EE, Spring) et Serveurs
    Réponses: 4
    Dernier message: 06/05/2011, 21h48
  3. [Socket] Comment faire du multi-client ?
    Par eric30eric dans le forum Web & réseau
    Réponses: 5
    Dernier message: 05/01/2005, 22h39
  4. Serveur Multi-clients
    Par darsky dans le forum C++Builder
    Réponses: 5
    Dernier message: 16/04/2004, 10h53
  5. Création d'un Serveur Multi Client
    Par N*E*R*D dans le forum Autres éditeurs
    Réponses: 5
    Dernier message: 16/03/2004, 18h13

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