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 :

Problème de serveur sous Linux


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
    Mars 2010
    Messages
    52
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 52
    Par défaut Problème de serveur sous Linux
    Bonjour

    voila j'ai créé un "tchat" avec une application client et une application serveur sous Linux.

    L'application Client marche bien, mais j'ai un problème avec le serveur

    j'explique :
    Lorsque un client se déconnecte, le serveur s'arrête (pour je ne sais quelle raison)

    Lors de la compilation (avec gcc) j'ai ceci:
    Warning: pointer targets in passing argument 3 of accept differ in signedness
    mais le problème ne vient pas d'ici je suppose.

    Voila la source (Je sais le code est sale )
    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
     
    #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <arpa/inet.h>
        #include <unistd.h>
        #define INVALID_SOCKET -1
        #define SOCKET_ERROR -1
        #define closesocket(s) close(s)
        #define PORT 23
        typedef int SOCKET;
        typedef struct sockaddr_in SOCKADDR_IN;
        typedef struct sockaddr SOCKADDR;
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    #define CLIENT_MAX 1000
     
    struct vari
    {
    SOCKET s_server;
    SOCKET s_client;
    SOCKADDR_IN client;
    };
     
    void* readAndSend(void* data);
     
    SOCKET s_global[CLIENT_MAX];
    int index_s = 0;
     
    int main(void)
    {
     int erreur = 0;
     
     
     SOCKET s_server;
     
            s_server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
     
            if (s_server == INVALID_SOCKET)
                fprintf(stderr, "La fonction socket a echoue.\n");
     
            else
            {
                SOCKADDR_IN server;
     
                server.sin_family       = AF_INET;
                server.sin_addr.s_addr  = htonl(INADDR_ANY);
                server.sin_port         = htons(PORT);
                memset(&server.sin_zero, '\0', sizeof(server.sin_zero));
     
                if (bind(s_server, (SOCKADDR *)&server, sizeof(server)) == SOCKET_ERROR)
                    fprintf(stderr, "La fonction bind a echoue.\n");
                else
                {
    	while(1)
    	      {
                    if (listen(s_server, 0) == SOCKET_ERROR) 
                        fprintf(stderr, "La fonction listen a echoue.\n");
                    else
                    {
                        SOCKET s_client;
                        SOCKADDR_IN client;
                        int csize = sizeof(client);
     
                        s_client = accept(s_server, (SOCKADDR *)&client, &csize);
                        s_global[index_s++] = s_client;
                        if (s_client == INVALID_SOCKET)
                            fprintf(stderr, "La fonction accept a echoue.\n");
                        else
                        {
                            char buffer[100];
                            int n;
     
                            printf("Le client %s s'est connecte !\n", inet_ntoa(client.sin_addr));
     
                            strcpy(buffer, "Bonjour\n");
                            send(s_client, buffer, (int)strlen(buffer), 0);
     
                            n = recv(s_client, buffer, sizeof(buffer) - 1, 0);
     
                            if (n != SOCKET_ERROR)
                            {
                                buffer[n] = '\0';
                                printf("%s", buffer);
                            }
     
     
    				pthread_t thread;
                                    pthread_create(&thread, NULL, readAndSend, &s_client); // thread pour recevoir et renvoyer les messages
     
    		    }
     
     
                    }
     
    	      }
     
     
                }
     
     
            }
     
     
        return 0;
    }						  
     
     
     
    void* readAndSend(void* data)
    {
     
        struct vari res;
        res.s_client = *(int*)data;
     
    char buffer[1024];
    char buff[1024];
    int n;
     
        while (1) //boucle de réception et d'envois
        {
            memset(buffer, '\0', sizeof(buffer));
            n = recv(res.s_client, buffer, sizeof(buffer) - 1, 0);  // je reçois le message dans "buffer"
            printf("%s\n", buffer);    // je l'affiche		
            sprintf(buff, "%s", buffer);   // je copie le "buffer" dans "buff"
     
    	int i;  
            for (i = 0; i < index_s; i++)
             {
                send (s_global[i], buff, (int)strlen(buff), 0);  // Et là , on envoie à  tous les clients !
             }	
     
        }
    }
    Merci et bon noël

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 82
    Par défaut
    Bonjour,

    Et avec :

    Il reste toujours un warning?

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    52
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 52
    Par défaut
    Merci, en effet en remplacent cela, le warning a été corrigé, mais j'ai toujours le problème du serveur qui se coupe

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 82
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
        while (1) //boucle de réception et d'envois
        {
            memset(buffer, '\0', sizeof(buffer));
            n = recv(res.s_client, buffer, sizeof(buffer) - 1, 0);  // je reçois le message dans "buffer"
            printf("%s\n", buffer);    // je l'affiche		
            sprintf(buff, "%s", buffer);   // je copie le "buffer" dans "buff"
     
    	int i;  
            for (i = 0; i < index_s; i++)
             {
                send (s_global[i], buff, (int)strlen(buff), 0);  // Et là , on envoie à  tous les clients !
             }	
     
        }
    Probablement ici la boucle infini, car tu ne testes pas si il y a ou pas réception de données. n != 0 devrait corriger le problème.

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    52
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 52
    Par défaut
    j'ai modifié le serveur, il n'y a plus de coupure à la déconnexion mais quand un client se déconnecte, au bout de 2 messages (venant d'un autre client) le serveur se coupe

    La je vois vraiment pas pourquoi au bout de 2 messages

    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
     
    #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <arpa/inet.h>
        #include <unistd.h>
        #define INVALID_SOCKET -1
        #define SOCKET_ERROR -1
        #define closesocket(s) close(s)
        #define PORT 23
        typedef int SOCKET;
        typedef struct sockaddr_in SOCKADDR_IN;
        typedef struct sockaddr SOCKADDR;
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    #define CLIENT_MAX 1000
     
    struct vari
    {
    SOCKET s_server;
    SOCKET s_client;
    SOCKADDR_IN client;
    };
     
    void* readAndSend(void* data);
     
    SOCKET s_global[CLIENT_MAX];
    int index_s = 0;
     
    int main(void)
    {
     int erreur = 0;
     
     
     SOCKET s_server;
     
            s_server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
     
            if (s_server == INVALID_SOCKET)
    	  {
                printf("La fonction socket a echoue.\n");
    	  }
     
            else
            {
                SOCKADDR_IN server;
     
                server.sin_family       = AF_INET;
                server.sin_addr.s_addr  = htonl(INADDR_ANY);
                server.sin_port         = htons(PORT);
                memset(&server.sin_zero, '\0', sizeof(server.sin_zero));
     
                if (bind(s_server, (SOCKADDR *)&server, sizeof(server)) == SOCKET_ERROR)
    		{
                    printf("La fonction bind a echoue.\n");
    		}
     
                else
                {
     
    		char buffer[100];
                    int n;
     
    	while(1)
    	      {
                    if (listen(s_server, 0) == SOCKET_ERROR)
    	       {
    		printf("la fonction listen a echoue\n");
    	       }
     
                    else
                    {
                        SOCKET s_client;
                        SOCKADDR_IN client;
                        socklen_t csize = sizeof(client);
     
                        s_client = accept(s_server, (SOCKADDR *)&client, &csize);
                        s_global[index_s++] = s_client;
                        if (s_client == INVALID_SOCKET)
    			{
                             printf("La fonction accept a echoue.\n");
    			}
     
                        else
                        {                        
     
                            printf("Le client %s s'est connecte !\n", inet_ntoa(client.sin_addr));
     
                            strcpy(buffer, "Bonjour\n");
                            send(s_client, buffer, (int)strlen(buffer), 0);
     
                            n = recv(s_client, buffer, sizeof(buffer) - 1, 0);
     
                            if (n != SOCKET_ERROR)
                            {
                                buffer[n] = '\0';
                                printf("%s", buffer);
                            }
     
    			else
    			{
    			    printf("La fonction recv a echou.\n");
    			}
     
    			pthread_t thread;
                            pthread_create(&thread, NULL, readAndSend, &s_client); // thread pour recevoir et renvoyer les messages
     
    		    }
     
     
                    }
     
    	      }
     
     
                }
     
     
            }
     
     
        return 0;
    }						  
     
     
     
    void* readAndSend(void* data)
    {
     
        struct vari res;
        res.s_client = *(int*)data;
     
    char buffer[1024];
    char buff[1024];
    int n;
     
        while (1) //boucle de réception et d'envois
        {
            memset(buffer, '\0', sizeof(buffer));
            n = recv(res.s_client, buffer, sizeof(buffer) - 1, 0);  // je reçois le message dans "buffer"
     
    	if (n != 0)
    	{
    	buffer[n] = '\0';
            printf("%s\n", buffer);    // je l'affiche		
            sprintf(buff, "%s", buffer);   // je copie le "buffer" dans "buff"
     
    	int i;  // Et là, on envoie à tous les clients !
            for (i = 0; i < index_s; i++)
             {
                send (s_global[i], buff, (int)strlen(buff), 0);  // j'envoie le buff
             }
     
    	}
     
    	else
    	{
    	printf("La fonction recv a echoue.\n");
    	break;
    	}	
     
        }
    return NULL;
    }

Discussions similaires

  1. Problème de portabilité sous Linux
    Par eG.dam dans le forum Linux
    Réponses: 4
    Dernier message: 23/06/2006, 23h06
  2. Connexion à un serveur sous linux ?
    Par VLDG dans le forum Installation
    Réponses: 3
    Dernier message: 08/06/2006, 10h36
  3. [WebService](SSL] Problème de SSL sous Linux
    Par Tueur_a_gage dans le forum JOnAS
    Réponses: 4
    Dernier message: 09/01/2006, 16h38
  4. Crére un client/serveur sous linux en TCP
    Par Darknicosh dans le forum Développement
    Réponses: 11
    Dernier message: 20/06/2005, 10h19
  5. Problème avec glutFullScreen() sous linux...
    Par MaxPayne dans le forum OpenGL
    Réponses: 1
    Dernier message: 29/11/2004, 11h30

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