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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include "../../include/reseau/file.h"
#define port 30
#define max_file 30
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
struct StructThread
{
pthread_t thread;
SOCKADDR_IN sin;
int recsize;
SOCKET sock;
int err;
char data[500];
};
void *envoie(void *p_data,FLdonnee *File_envoie)
{
struct StructThread *structThread = p_data;
int retour;
retour= send(structThread->sock,File_envoie->donnee->donnee,sizeof File_envoie->donnee->donnee - 1,0);
supprimerEnTete(File_envoie);
return retour;
}
void *reception (void *p_data,FLdonnee *File_reception)
{
struct StructThread *structThread = p_data;
int fin = 0;
while(!fin)
{
//reception de donnees
unsigned char data[1024];
int sock_err = recv (structThread->sock, data, (sizeof data - 1), 0);
size_t nb_rec = sock_err;
data[nb_rec] = 0;
printf ("on recoit:\n'%s' sur socket:%d\n", data,structThread->sock);
Ldonnee* ladonnee=(Ldonnee*)malloc(sizeof(Ldonnee));;
ladonnee->taille=1024;
ladonnee->socket=structThread->sock;
strcpy(data,ladonnee->donnee);
insererEnQueue(File_reception,*ladonnee );
// on peut passer data les données au autres fonction,
// et l'on reconnait le client a l'aide du numero de socket structThread->sock
if (data[0] == 48) // equivaut a un '0' recu
{
fin = 1;
}
bzero(data,sizeof data-1);
fflush (stdout);
}
shutdown (structThread->sock, 2);
printf ("client deconnecter sur socket %d \n", structThread->sock);
close (structThread->sock);
free (structThread), structThread = NULL;
return NULL;
}
void attente_client(int socket,FLdonnee *File_reception,FLdonnee *File_envoie)
{
int error = 0;
int fin = 0;
while (!fin)
{
printf ("en attente de connexion cliente sur le port %d...\n", port);
// creation du thread
struct StructThread *structThread = malloc (sizeof *structThread);
structThread->recsize = (int) sizeof structThread->sin;
structThread->sock = accept (socket, (SOCKADDR *) &structThread->sin, &structThread->recsize);
//nouveau trhread pour le client
printf
("client connecte sur le socket %d avec comme adresse%s:%d\n",
structThread->sock, inet_ntoa (structThread->sin.sin_addr),
htons (structThread->sin.sin_port));
strcpy(structThread->data,"test");
// on lance le thread
//pthread_create (&structThread->thread, NULL, reception, structThread);
//pthread_create (&structThread->thread, NULL, envoie, structThread);
//on détruit quand il est fini
structThread = NULL;
}
return error;
}
int app(void)
{
int sock;
sock = socket (AF_INET, SOCK_STREAM, 0);
int sock_err;
SOCKADDR_IN sin = { 0 };
sin.sin_addr.s_addr = htonl (INADDR_ANY);
sin.sin_family = AF_INET;
sin.sin_port = htons (port);
sock_err = bind (sock, (SOCKADDR *) &sin, sizeof sin);
sock_err = listen (sock, 5);
FLdonnee *File_reception;
creer(File_reception,max_file);
printf("\nla4\n");
FLdonnee *File_envoie;
creer(File_envoie,max_file);
printf("\nla5\n");
printf("ecoute sur le port n %d ...",port);
printf("\nla6\n");
attente_client(sock,File_reception,File_envoie);
/* ferme le socket. */
sock_err = close (sock);
return 0;
}
int main(void)
{
app();
return 0;
} |
Partager