Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //+++++++++
- //serveur.c
- //+++++++++
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <errno.h>
- #include <string.h>
- #define p 8000
- struct spectacle
- {
- int num_spect;
- char *nom_spect;
- int date_spect;
- int nb_place;
- int tarif;
- };
- typedef struct spectacle spectacle;
- struct reservation
- {
- int num_res;
- int num_sp;
- int nb_p;
- };
- typedef struct reservation reservation;
- spectacle t_spectacle[100];
- int nom_client(int sock);
- int nom_client(int sock)
- {
- struct sockaddr_in client;
- struct hostent* clt;
- int longueur = sizeof(client);
- if(getpeername(sock, (struct sockaddr*)&client, &longueur)<0)
- return(-1);
- longueur = sizeof(client.sin_addr);
- if((clt = gethostbyaddr((char*)&client.sin_addr, longueur, AF_INET)) == NULL)
- return(-1);
- // noths : convertit un entier court dans l'ordre des octets réseau
- // inet_ntoa convertit une adresse internet dans l'ordre des octets
- // réseau en une chaîne de caractères.
- printf("Client de nom %s et l'adresse %d sur socket %d\n", clt->h_name, inet_ntoa(client.sin_addr), ntohs(client.sin_port));
- }
- int main()
- {
- char inscris;
- int sock, sock_com;
- struct sockaddr_in adresse;
- char *message[1024];
- u_short port = p;
- int options = -1;
- //+++++++++++++++++++++++
- // Ouverture d'une socket
- //+++++++++++++++++++++++
- sock=socket(AF_INET, SOCK_STREAM, 0);
- if(sock<0)
- {
- perror("Erreur d'ouverture de la socket");
- return(-1);
- }
- //+++++++++++++++++++++++++++++++++++++++++++++++++
- // Constituer l'adresse conformément à la structure
- //+++++++++++++++++++++++++++++++++++++++++++++++++
- adresse.sin_family = AF_INET;
- adresse.sin_port = htons((u_short)port);
- adresse.sin_addr.s_addr = INADDR_ANY;
- //+++++++++++++++++++++++++++++++++++++++++++++++++++
- // Permettre la réuitilisation d'une socket existante
- //+++++++++++++++++++++++++++++++++++++++++++++++++++
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(int));
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
- // Phase d'association de la socket à l'adresse locale
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
- if(bind(sock, (struct sockaddr*)&adresse, sizeof(adresse))<0)
- {
- perror("Erreur d'association");
- return(-1);
- }
- //++++++++++++++++++++++++++++++++
- // Attente de la connexions client
- //++++++++++++++++++++++++++++++++
- listen(sock, 5);
- //++++++++++++++++++++++++++++
- // Acceptation de la connexion
- //++++++++++++++++++++++++++++
- sock_com=accept(sock, (struct sockaddr*)0, (int*)0);
- if(sock_com == -1)
- {
- perror("ERREUR ACCEPT");
- return(-1);
- }
- else
- perror("Bravo: connexion établie !");
- nom_client(sock_com);
- //++++++++++++++++
- // Phase d'échange
- //++++++++++++++++
- /*
- if(read(sock_com, message, 1024)==-1)
- {
- perror("Il y a un probleme dans la lecture de la socket\n");
- }
- */
- FILE *fichier = NULL;
- //+++++++++++++++++++++++++++++++++++++
- // Lecture du fichier spectacle.txt
- //+++++++++++++++++++++++++++++++++++++
- fichier = fopen ("spectacle.txt", "r+");
- if (fichier == NULL)
- {
- perror("Erreur lors de l'ouverture du fichier !");
- }
- int i=-1;
- int num, date, places, prix;
- char nom, crew;
- while (fgetc(fichier) != EOF)
- {
- i++;
- fscanf(fichier,"%d %s %d %d %d", &num,&nom,&date,&places,&prix);
- t_spectacle[i].num_spect=num;
- t_spectacle[i].nom_spect=&nom;
- t_spectacle[i].date_spect=date;
- t_spectacle[i].nb_place=places;
- t_spectacle[i].tarif=prix;
- }
- while(i>=0)
- {
- printf("%d,%s,%d,%d,%d\n", t_spectacle[i].num_spect,t_spectacle[i].nom_spect,t_spectacle[i].date_spect,t_spectacle[i].nb_place,t_spectacle[i].tarif);
- i--;
- }
- read(sock_com, inscris, sizeof (inscris));
- if (inscris=='o')
- {
- message[1024]="Loguez vous !";
- }
- else
- {
- message[1024]="Inscrivez vous !";
- }
- if(write(sock, message, strlen(message)) == -1)
- {
- perror("Erreur d'émission ou d'écriture dans la socket");
- return(-1);
- }
- /*else
- {
- fprintf(stderr, "Message reçu : %s\n", message);
- fprintf(stderr, "Message à envoyer : \n");
- scanf("%s", message);char testcar,mot;
- if(write(sock_com, message, strlen(message))==-1)
- {
- perror("Il y a un problème dans l'envoi du message\n");
- return(-1);
- }
- else
- {
- fprintf(stderr, "\nLe message %s est correctement envoyé\n", message);
- }
- close(sock_com);
- }*/
- close(sock);
- }
Add Comment
Please, Sign In to add comment