Bonjour,

Je souhaite créer un chat entre plusieurs clients avec des terminaux, j'ai réussi à en faire un mais avec des clients et un serveur.
Je ne sais pas comment synchroniser l'envoi de messages et la réception en fonction des utilisateurs je sais qu'il faut utiliser des signaux mais j'ai pas compris comment m'en servir.

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
main(int argc,char *argv[])
{
#include<unistd.h>
#include <errno.h>
#include <ctype.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include<fcntl.h>
#include<string.h>
#define TAILLE 256
#define	nomTube "essai"
	int fd,fifo;
	char buf[TAILLE];
	scanf("%s",pseudo);
 
	int pid;
 
	fifo = mknod(nomTube, S_IFIFO|0666, 0);
  	if ((fifo== -1)) {
   		 perror("Error creating the	named pipe");
    		exit(1);
  	}
 
	/*Ouverture du pipe*/
	fd=open(nomTube,O_WRONLY);
	if(fd==-1)
	{
 
		perror("Impossible d'ouvrir l'entrée du tube nommé\n");
		exit(1);
	}
 
	pid=fork();
	/*Poster Message*/
	if(pid==0){
		int n=1;
		while (n>0) {
		n=read(STDIN_FILENO, buf, TAILLE);
			if (n<0) {
			      perror ("Error reading from stdin");
			      exit (1);
			}	
			if (write(fd, buf, n)<0) {
	     			perror ("Error writing into pipe");
	      			exit (1);
	    		}
		}
	}
 
	if(pid>0){
	/*Lire le message*/
		printf("Je suis le père");
		int numread=1;	
		while (numread!=0) {
			numread = read(fd, buf, TAILLE);
			//printf();
	    		if (numread>0)
	      			write(STDOUT_FILENO, buf, numread);
	    		if (numread<0) {
	      			perror("Error reading from pipe");
	      			exit(1);
	    		}	
	  	}
	}
 
	else{
 
		perror("Erreur pid");
	}
	//return EXIT_SUCCESS;
}