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 :

Multiple socket pour un chat [TCP/IP - C]


Sujet :

Réseau C

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 12
    Points : 5
    Points
    5
    Par défaut Multiple socket pour un chat [TCP/IP - C]
    Bonjour à tous, pour un projet d'école je dois créer un chat avec image, son et texte.

    Pour faire passer tous ces flux j'ai pensé les faire passer sur des ports différents..Soit un socket par flux. Est ce la meilleure solution? Étant celle que j'ai choisit et implémentée, mon premier socket s'ouvre correctement mais les suivants non. Y'a t'il des précautions à prendre pour passer d'un socket à un autre. Sachant que chaque socket est créer dans mon main() et après exploiter dans des thread auquel j'envoie l'id de la socket.

    Enfin je ne sais pas si le problème est clair mais je suis un peu perdu pour utiliser plusieurs socket.
    Merci d'avance pour votre aide.

  2. #2
    Invité
    Invité(e)
    Par défaut
    Bonjour,

    Sans voir ton code il est difficile de te dire pourquoi les autres sockets ne fonctionnent pas !

    Au lieu de faire des thread, tu peux aussi utiliser la fonction select qui te permettra de vérifier l'arrivée de nouvelles données sur les différentes socket.

  3. #3
    Expert confirmé Avatar de fregolo52
    Homme Profil pro
    Développeur C
    Inscrit en
    Août 2004
    Messages
    2 364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur C

    Informations forums :
    Inscription : Août 2004
    Messages : 2 364
    Points : 5 378
    Points
    5 378
    Par défaut
    Citation Envoyé par feydaykyn Voir le message
    Au lieu de faire des thread, tu peux aussi utiliser la fonction select qui te permettra de vérifier l'arrivée de nouvelles données sur les différentes socket.
    Qui est décrit dans ce cours ?

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 12
    Points : 5
    Points
    5
    Par défaut
    Merci à vous je vais aller voir le tutoriel en question en attendant si vous êtes motivé je vous met le code que j'ai fais :p encore merci d'avance:

    SERVEUR

    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
     
    #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
     
    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( PF_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{
    		fgets(msg, sizeof(msg), stdin);
    		search(msg);
    		send(client_id1, msg,sizeof(msg),0);
    	}
    	while(strcmp(msg,"exit")!=0);
    	return NULL;
    }
     
    /*****************************************/
    //	ENVOI RECEPTION SON	    //
    /*****************************************/
    void* flux_son_sent(void* arg){
     
    	int client_id_son_out=0;
    	struct thread_info *info=(struct thread_info *)arg;
    	int socket_id_son_out=info->socket_tmp;
    	char buf[128]="TEST envoi son serveur";
     
     
    	if((client_id_son_out = accept((int)socket_id_son_out,NULL,0)) == -1){
    		fprintf( stderr, "probleme avec accept son out \n" );
    		exit( EXIT_FAILURE );
    	}
    	else{
    		printf("Connexion serveur son OUT reussi sur socket %d\n",client_id_son_out);
    		search(buf);
    		send(client_id_son_out, buf,sizeof(buf),0);
    	}
     
    }
    void* flux_son_recu(void* arg){
     
    	int client_id_son_in=0;
    	struct thread_info *info=(struct thread_info *)arg;
    	int socket_id_son_in=info->socket_tmp;
    	char buf[128] = "";
     
     
    	if((client_id_son_in = accept((int)socket_id_son_in,NULL,0)) == -1){
    		fprintf( stderr, "problème avec accept son in\n" );
    		exit( EXIT_FAILURE );
    	}
    	else{
    		printf("Connexion serveur son IN reussi sur socket:%d\n",client_id_son_in);
    		recv(client_id_son_in, buf, sizeof(buf), 0);
    		//printf("SON IN recep reussi->%s\n",buf);
    	}
    }
     
    int main( int argc, char** argv ) {
    	int socket_id,socket_id_son_in,socket_id_son_out,client_id;
    	char buffer[128]= "";
    	pthread_t thread,thread_son_in,thread_son_out;
    	//permet le passage d'arguments aux threads
    	struct thread_info *info;
    	struct thread_info *info_in;
    	struct thread_info *info_out;
    	info = malloc(sizeof(struct thread_info));
    	info_in = malloc(sizeof(struct thread_info));
    	info_out = malloc(sizeof(struct thread_info));
     
    	//SERVEUR TEXTE
    	socket_id = create_tcp_server(2000,1);
    	info->socket_tmp=socket_id;
    	pthread_create(&thread, NULL, envoi,info);
     
    	//SERVEUR SON OUT
    	socket_id_son_out = create_tcp_server(2002,1);
    	info_out->socket_tmp=socket_id_son_out;
    	pthread_create(&thread_son_out, NULL, flux_son_sent,info_out);
     
    	//SERVEUR SON IN
    	socket_id_son_in = create_tcp_server(2001,1);
    	info_in->socket_tmp=socket_id_son_in;
    	pthread_create(&thread_son_in, NULL, flux_son_recu,info_in);
     
     
    	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_son_in, 2 );
    	close( socket_id_son_in );
    	shutdown( socket_id_son_out, 2 );
    	close( socket_id_son_out );
    	shutdown( socket_id, 2 );
    	close( socket_id );
    	shutdown( client_id, 2 );
    	close( client_id );
     
    	pthread_cancel(thread);
    	pthread_cancel(thread_son_in);
    	pthread_cancel(thread_son_out);
     
    	free(info);
    	free(info_in);
    	free(info_out);
     
    	exit( EXIT_SUCCESS );
     
    }
    CLIENT

    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
     
    #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
     
    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( PF_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; /* 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", 2000 );
     
    	while(recv(socket_id1,buffer, sizeof(buffer), 0))
    		printf("Stranger: %s\n", buffer);
    	return 0;
    }
     
    /*****************************************/
    //	  ENVOI RECEPTION SON  //
    /*****************************************/
    void* flux_son_sent(void* arg){
     
    	struct thread_info *info=(struct thread_info *)arg;
    	int socket_id_son_out=info->socket_tmp;
    	char buf[128] ="TEST envoi son client";
     
    	search(buf);
    	send(socket_id_son_out, buf,sizeof(buf),0);
     
    }
    void* flux_son_recu(void* arg){
     
    	struct thread_info *info=(struct thread_info *)arg;
    	int socket_id_son_in=info->socket_tmp;
    	char buf[128] = "";
     
    	recv(socket_id_son_in, buf, sizeof(buf), 0);
     
    }
     
    int main( int argc, char** argv ) {
    	int socket_id=0,socket_id_son_in=0,socket_id_son_out=0;
    	char msg[128]="";
    	pthread_t thread,thread_son_in,thread_son_out;
    	struct thread_info *info; //permet le passage d'arguments aux threads
    	struct thread_info *info_in;
    	struct thread_info *info_out;
    	info = malloc(sizeof(struct thread_info));
    	info_in = malloc(sizeof(struct thread_info));
    	info_out = 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);
     
    	//SOCKET SON IN
    	socket_id_son_in = create_tcp_client( "localhost", 2001);
    	info_in->socket_tmp=socket_id_son_in;
    	pthread_create(&thread_son_in, NULL, flux_son_recu, info_in);
     
    	//SOCKET SON OUT 
    	socket_id_son_out = create_tcp_client( "localhost", 2002);
    	info_out->socket_tmp = socket_id_son_out;
    	pthread_create(&thread_son_out, NULL, flux_son_sent, info_out);
     
    	printf("Socket texte [%d] || socket son in [%d] || socket son out [%d]\n",socket_id,socket_id_son_in,socket_id_son_out);
     
     
    	//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);
    	pthread_cancel(thread_son_in);
    	pthread_cancel(thread_son_out);
    	//On ferme les sockets
    	shutdown( socket_id, 2 );
    	shutdown( socket_id_son_in, 2 );
    	shutdown( socket_id_son_out, 2 );
    	close( socket_id );
    	close( socket_id_son_in );
    	close( socket_id_son_out);
     
    	free(info);
    	free(info_in);
    	free(info_out);
     
    	exit( EXIT_SUCCESS );
     
    }

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 12
    Points : 5
    Points
    5
    Par défaut
    Encore merci a vous mais j'ai trouvé d'ou venait mon soucis, mes sockets fonctionnent maintenant j'avais fais une erreur au niveau des ports et 2-3 autres petites broutilles.

Discussions similaires

  1. Socket pour chat
    Par jowelle dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 09/09/2013, 12h27
  2. [C#] Sockets ou WCF pour un chat ou mini jeux réseau
    Par darioo2 dans le forum Débuter
    Réponses: 5
    Dernier message: 20/08/2012, 17h57
  3. Réponses: 1
    Dernier message: 06/02/2012, 14h39
  4. Utilisation des Socket pour un mini chat
    Par megamario dans le forum C++Builder
    Réponses: 2
    Dernier message: 17/11/2009, 14h05
  5. Je Recherche des sources Turbo pascal pour piloter en TCP/IP
    Par mennix dans le forum Turbo Pascal
    Réponses: 18
    Dernier message: 23/08/2004, 01h31

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