toto
Messages : 66 Points : 5680 Date de naissance : 24/07/1988 Date d'inscription : 19/02/2010 Age : 36 Localisation : casablanca Emploi/loisirs : hand ball
| Sujet: client serveur Lun 18 Oct - 13:25 | |
| saluut tout monde voila Un petite programme C Client / Serveur permet de communiquer entre eux sous lunix client.c - Code:
-
#include<stdio.h> #include<sys/un.h> #include<sys/socket.h> #include<sys/types.h> #define max 256 main(int argc,char *argv[]) { static struct sockaddr_un cli_addr,serv_addr; int i; int sockfd; int servlen; char sendline[256]; char recv[256]; int newsockfd; int clilen; if((sockfd=socket(AF_UNIX,SOCK_STREAM,0))<0) printf("erreur creation de socket\n"); serv_addr.sun_family=AF_UNIX; bcopy(argv[1],serv_addr.sun_path,strlen(argv[1])); servlen=strlen(serv_addr.sun_path)+sizeof(serv_addr.sun_family); //cote client if(connect(sockfd,(struct sockaddr *)&serv_addr,servlen)<0) printf("error de connexion \n"); while( strcmp(sendline,"bye")!=0) { //envoyer l'info gets(sendline); write(sockfd,sendline,sizeof(sendline)); //resevoire l'info2 read(sockfd,recv,max); printf("serveur dit: %s\n",recv); }
close(sockfd); exit(0);
}
server.c - Code:
-
#include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<sys/un.h> main(int argc,char* argv[]) { static struct sockaddr_un cli_addr,serv_addr; int i; int sockfd;// declaratoon des variable et construteur int newsockfd; int clilen; int servlen; int max=250; char recvline[256]; char c; char send[256]; if((sockfd=socket(AF_UNIX,SOCK_STREAM,0))<0) printf("error socket\n"); serv_addr.sun_family=AF_UNIX; printf("le chemin est %s\n",argv[1]);// bcopy(argv[1],serv_addr.sun_path,strlen(argv[1])); servlen=strlen(serv_addr.sun_path)+sizeof(serv_addr.sun_family); //cote serveur if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0) perror("bind"); listen(sockfd,1); clilen=sizeof(cli_addr); newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen); if(newsockfd<0) { printf("accept error\n"); exit(0); } printf("connexion acceptée\n"); while(strcmp(send,"bye")!=0) { //recevoire l'info read(newsockfd,recvline,max); printf("client dit:%s\n",recvline); //envoyer l'info2 gets(send); write(newsockfd,send,sizeof(send)); read(newsockfd,recvline,max); }
//fermer la connexion close(newsockfd); }
| |
|
TSDI2008
Messages : 34 Points : 5379 Date de naissance : 12/08/1985 Date d'inscription : 27/08/2010 Age : 39 Localisation : laayoune Emploi/loisirs : etudiant
| |