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
|
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#include <time.h>
#define MAX_CARACT 100
#define PORT 2007
#define LISTE_ATTENTE 5
/*Variables globales*/
int mon_address_longueur, lg=512;
struct sockaddr_in mon_address, client_address;
socklen_t longueur;
int client_socket=-1;
int ma_socket;
char *ip_client;
char *buffer;
sigset_t ens , ens_old;
struct sigaction action;
int ret;
void init_socket()
{
buffer=malloc(MAX_CARACT+1);
printf("INITIALISATION\n");
bzero(&mon_address,sizeof(mon_address));
mon_address.sin_port = htons(PORT);
mon_address.sin_family = AF_INET;
mon_address.sin_addr.s_addr = htonl(INADDR_ANY);
/* creation de socket */
if ((ma_socket = socket(AF_INET,SOCK_STREAM,0))== -1)
{
printf("Problème avec la creation\n");
exit(0);
}
sigfillset(&ens);
sigdelset(&ens , SIGINT);
sigdelset(&ens , SIGCHLD);
sigprocmask(SIG_SETMASK , &ens , &ens_old ) ;
action.sa_handler = SIGCHLD_handler;
action.sa_mask = ens;
action.sa_flags = SA_RESTART;
if ( sigaction(SIGCHLD , &action , NULL) == -1 )
{
perror("sigaction");
exit(1);
}
setbuf(stdout, NULL);
setbuf(stdin, NULL);
signal(SIGINT, SIGINT_handler);
signal(SIGCHLD , SIGCHLD_handler);
ret = bind(ma_socket,(struct sockaddr *)&mon_address, sizeof(mon_address));
if (ret!=0)
{
perror("bind()");
exit(1);
}
/* ecoute sur la socket */
ret = listen(ma_socket,LISTE_ATTENTE);
if (ret!=0)
{
perror("listen()");
exit(1);
}
}
char *ecoute(void)
{
mon_address_longueur = sizeof(client_address);
longueur = sizeof(client_address);
client_socket = accept(ma_socket,
(struct sockaddr *)&client_address,
&longueur);
ip_client=inet_ntoa(client_address.sin_addr);
lg = read(client_socket,buffer, MAX_CARACT);
write(client_socket,buffer, MAX_CARACT);
return buffer;
} |
Partager