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

C Discussion :

chat multithread serveur/client


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Homme Profil pro
    ugb
    Inscrit en
    Août 2012
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : ugb
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2012
    Messages : 12
    Par défaut chat multithread serveur/client
    j essai de faire un chat simple en utilisant des threads ! le probléme c est que le programme n arrive pas a s exécuter et on me signal des erreur sur le serveur et notamment sur le client !

    serveur.c
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <pthread.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
     
    #define SIZE 10000
    #define port 1000
     
     
    struct thread_info{
    	int socket_tmp;}
     
    int create_tcp_server( int port, int nb_max_clients ) {
    	int socket_id;
    	int optval = 1;
    	struct sockaddr_in sockname;
     
    	//On établi la connexion
    	if( -1 == (socket_id = socket( AF_INET, SOCK_STREAM, 0 ) ) ) {
    		fprintf( stderr, "problème création socket\n" );
    		exit( EXIT_FAILURE );
    	}
     
    	setsockopt( socket_id, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof( int ) );
    	memset( (char*)&sockname, 0, sizeof( struct sockaddr_in ) );
     
    	/* Bind socket to a port */
     
    	sockname.sin_family = AF_INET; /* Internet address family */
    	sockname.sin_port = htons( port ); /* Local port */
    	sockname.sin_addr.s_addr = htonl( INADDR_ANY );/* Any incoming interface */
     
    	if( (bind( socket_id, (struct sockaddr*)&sockname, sizeof(struct sockaddr_in))) == -1)
    	{
    		fprintf( stderr, "problème avec bind\n" );
    		exit( EXIT_FAILURE );
    	}
    	/* Set socket to listen : Mark the socket so it will listen for incoming connections */
    	if((listen(socket_id,nb_max_clients)) == -1)
    	{
    		fprintf( stderr, "problème avec listen\n" );
    		exit( EXIT_FAILURE );
    	}
     
    	return socket_id;
    }
     
    static void search(char *chaine){
     
        	char *p = strchr(chaine, '\n');
     
       	if (p)
          	*p = 0;
    }
     
    static void* envoi(void* arg){
    	char msg[32]="";
    	short int client_id1=0;
    	struct thread_info *info=(struct thread_info *)arg;
    	int socket_id=info->socket_tmp;
     
    	if((client_id1 = accept((int)socket_id,NULL,0)) == -1)
    	{
    		fprintf( stderr, "problème avec accept\n" );
    		exit( EXIT_FAILURE );
    	}
    	do{
                 printf ("Client1 %s\n >", inet_ntoa(sockname.sin_addr.s_addr)); 
    		fgets(msg, sizeof(msg), stdin);
    		search(msg);
    		send(client_id1, msg,sizeof(msg),0);
    	}
    	while(strcmp(msg,"exit")!=0);
    	return NULL;
    }
     
    int main( int argc, char** argv ) {
    	int socket_id,client_id;
    	char buffer[128]= "";
    	pthread_t thread;
    	//permet le passage d'arguments aux threads
    	struct thread_info *info;
    	info = malloc(sizeof(struct thread_info));
    	//SERVEUR TEXTE
    	socket_id = create_tcp_server(1000,4);
    	info->socket_tmp=socket_id;
    	pthread_create(&thread, NULL, envoi,info);
     
    	if((client_id = accept(socket_id,NULL,0)) == -1){
    		fprintf( stderr, "probleme avec accept texte\n" );
    		exit( EXIT_FAILURE );
    	}
    	else{
    		printf("Connexion serveur texte reussi sur socket %d\n",client_id);
    		while(recv(client_id,buffer, sizeof(buffer),0)){
    			printf("Stranger: %s\n", buffer);
    		}
    	}
     
    	// 0, pour fermer la socket en réception, 1, en émission, 2 dans les deux sens.
    	shutdown( socket_id, 2 );
    	close( socket_id );
    	shutdown( client_id, 2 );
    	close( client_id );
     
    	pthread_cancel(thread);
    	free(info);
             exit( EXIT_SUCCESS );}
    client.c
    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <pthread.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    #define port 1000
    #define SIZE 10000
     
    struct thread_info{
    	int socket_tmp;
    };
     
    int create_tcp_client( const char* hostname, int port ) {
    	struct hostent* host_address;
    	struct sockaddr_in sockname;
    	int optval;
    	int socket_id;
     
    	if( NULL == ( host_address = gethostbyname( hostname ) ) ) {
    		fprintf( stderr, "machine %s inconnue\n", hostname );
    		return( EXIT_FAILURE );
    	}
    	/* Create a reliable, stream socket using TCP */
    	if( -1 == ( socket_id = socket( AF_INET, SOCK_STREAM, 0 ) ) ) {
    		fprintf( stderr, "création de socket impossible\n" );
    		return( EXIT_FAILURE );
    	}
     
    	optval = 1;
    	setsockopt(socket_id, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int) );
     
    	/*Establish connection*/
    	sockname.sin_family = host_address->h_addrtype;
            sockname.sin_addr.s_addr = inet_addr(10.0.3.15);
     /* l'adresse se trouve dans le champ h_addr de la structure host_adress */
    	sockname.sin_port = htons( port );/* Server port */
    	memcpy( (char*)&(sockname.sin_addr.s_addr), host_address->h_addr, host_address->h_length );
     
    	if((connect( socket_id, (struct sockaddr*)&sockname, sizeof(struct sockaddr_in)))==-1) {
    		fprintf( stderr, "connexion impossible socket au server %s\n", hostname );
    		return( EXIT_FAILURE );
    	}
     
    	return socket_id;
    }
     
    static void search(char *chaine){
     
        	char *p = strchr(chaine, '\n');
     
       	if (p)
          	*p = 0;       
    }
     
    void* recep(void* socket_id){
     
    	char buffer[64] = "";
    	int socket_id1 = create_tcp_client( "localhost", 1000 );
     
    	while(recv(socket_id1,buffer, sizeof(buffer), 0))
    		printf("Stranger: %s\n", buffer);
    	return 0;
    }
     
    int main( int argc, char** argv ) {
    	int socket_id=0;
    	char msg[128]="";
    	pthread_t thread;
    	struct thread_info *info; //permet le passage d'arguments aux threads
    	info = malloc(sizeof(struct thread_info));
     
    	//SOCKET TEXTE
    	socket_id = create_tcp_client( "localhost", 2000);
    	info->socket_tmp=socket_id;
    	pthread_create(&thread, NULL, recep, info);
     
    	//ENVOI DE TEXTE
    	do{	
    		fgets(msg, sizeof(msg), stdin);
    		search(msg);
    		send(socket_id, msg,sizeof(msg),0);
    	}
    	while(strcmp(msg,"exit")!=0);
     
    	//On ferme les threads
    	pthread_cancel(thread);
    	//On ferme les sockets
    	shutdown( socket_id, 2 );
    	close( socket_id );
    	free(info);
    	exit( EXIT_SUCCESS );
     
    }

  2. #2
    Inactif  


    Homme Profil pro
    Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Inscrit en
    Décembre 2011
    Messages
    9 026
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Doctorant sécurité informatique — Diplômé master Droit/Économie/Gestion
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2011
    Messages : 9 026
    Par défaut
    Bonjour,

    Pourrais-tu au moins nous donner ces erreurs qu'on sache où chercher?

  3. #3
    Membre habitué
    Homme Profil pro
    ugb
    Inscrit en
    Août 2012
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : ugb
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2012
    Messages : 12
    Par défaut
    pour le client pour le moment y a plus de probléme j avais fais une erreur en fixant le la valeur du port ! mais pour le serveur je comprend pas il me dique "serveurchat1.c:18:1: erreur: expected ‘;’, identifier or ‘(’ before ‘int’ c est sur la ligne 20 int create_tcp_server( int port, int nb_max_clients ) {

  4. #4
    Modérateur

    Avatar de Bktero
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2009
    Messages
    4 493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2009
    Messages : 4 493
    Billets dans le blog
    1
    Par défaut
    Met un ; après la déclaration de la structure, comme te le dit ton compilateur.

  5. #5
    Membre habitué
    Homme Profil pro
    ugb
    Inscrit en
    Août 2012
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : ugb
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2012
    Messages : 12
    Par défaut
    merci Bktero! le serveur s execute! mais y a un nouveau probléme ! il me retourne "problème avec bind " surement a partir du ligne 40 !

  6. #6
    Membre Expert
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2011
    Messages
    1 255
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2011
    Messages : 1 255
    Par défaut
    il semblerait que tu sois sous linux, quel est la valeur de errno ? (quand tu as l'erreur avec bind)

  7. #7
    Membre habitué
    Homme Profil pro
    ugb
    Inscrit en
    Août 2012
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : ugb
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2012
    Messages : 12
    Par défaut
    si la reponse retournée est égale a -1

Discussions similaires

  1. Probleme simulation d'un bateau serveur/client multithread
    Par hacker33 dans le forum Débuter avec Java
    Réponses: 0
    Dernier message: 25/02/2013, 15h43
  2. Simulation d'un bateau serveur/client multithread
    Par hacker33 dans le forum Langage
    Réponses: 0
    Dernier message: 25/02/2013, 14h47
  3. chat serveur/client en un programme
    Par Erolgamer dans le forum Linux
    Réponses: 2
    Dernier message: 25/11/2010, 09h36
  4. socket multiThread serveur JAVA / client FLEX
    Par aliong dans le forum Flex
    Réponses: 2
    Dernier message: 28/08/2009, 20h06
  5. Serveur/Client sous linux
    Par black is beautiful dans le forum Réseau
    Réponses: 2
    Dernier message: 13/08/2004, 13h34

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