32c9271a
skhinach
modif
|
1
2
3
4
5
6
|
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
|
3e25c332
pifou
Big upload
|
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
|
#define BUFF_SIZE 100000
struct PageWeb
{
int size;
char* html_contents;
};
void getWeb(char* pageName, struct PageWeb** page)
{
printf("Test\n");
*page=malloc(sizeof(struct PageWeb));
printf("Test ouh\n");
FILE* pageFile=NULL;
printf("Test 1");
free((*page)->html_contents);
if((pageFile=fopen(pageName,"r"))!=NULL)
{
fseek(pageFile, 0L, SEEK_END);
long nbr_char=ftell(pageFile);
fseek(pageFile, 0L, SEEK_SET);
printf("Test 2");
(*page)->html_contents=malloc(nbr_char*sizeof(char));
fread(((*page)->html_contents),1,nbr_char,pageFile);
(*page)->size=strlen(((*page)->html_contents));
}
else
{
printf("Test 3");
char erreur[]="La page n'a pas été trouvée\n";
(*page)->html_contents=malloc(strlen(erreur)*sizeof(char));
strcat((*page)->html_contents, "La page n'a pas été trouvée\n");
}
printf("Test 4");
fclose(pageFile);
}
|
32c9271a
skhinach
modif
|
46
47
48
49
50
|
void* reponseConnexion(void* sock)
{
printf("Connected\n");
|
3e25c332
pifou
Big upload
|
51
52
53
54
55
|
struct PageWeb* page=malloc(sizeof(struct PageWeb*));
getWeb("../Sidoux/Page.html", &page);
|
fff6f037
skhinach
modifs
|
56
|
char buff_rec[BUFF_SIZE];
|
3e25c332
pifou
Big upload
|
57
58
59
|
char buffer[BUFF_SIZE];
strcat(buffer,"HTTP/1.1 200 OK\r\nServer: Serveur fait maison\r\nContent-Type: text/html; charset=UTF-8\r\nConnection: Keep-alive\r\n\r\n");
|
fff6f037
skhinach
modifs
|
60
|
|
3e25c332
pifou
Big upload
|
61
62
63
64
65
66
|
printf("Test10\n");
printf("%s", page->html_contents);
strcat(buffer, page->html_contents);
free(page);
|
fff6f037
skhinach
modifs
|
67
|
|
32c9271a
skhinach
modif
|
68
69
70
71
|
FILE* sockdial_stream = fdopen(*((int*)(sock)), "a+");
printf("Sock : %d // Stream : %p \n", *((int*)(sock)), sockdial_stream);
|
fff6f037
skhinach
modifs
|
72
|
fgets(buff_rec, BUFF_SIZE, sockdial_stream);
|
32c9271a
skhinach
modif
|
73
74
|
fprintf(sockdial_stream, "%s", buffer);
|
fff6f037
skhinach
modifs
|
75
76
|
//while(strcmp(buffer, "EXIT\n")!=0)
//{
|
32c9271a
skhinach
modif
|
77
|
printf("WAIT\n");
|
fff6f037
skhinach
modifs
|
78
|
// fgets(buff_rec, BUFF_SIZE, sockdial_stream);
|
32c9271a
skhinach
modif
|
79
|
|
fff6f037
skhinach
modifs
|
80
|
printf("Le message reçu : %s", buff_rec);
|
32c9271a
skhinach
modif
|
81
82
83
|
fprintf(sockdial_stream, "Ceci est une réponse du serveur TCP.\n");
|
fff6f037
skhinach
modifs
|
84
|
//}
|
c35daaf6
Souheib Khinache
Organisation, cré...
|
85
|
|
32c9271a
skhinach
modif
|
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
strcpy(buffer, "START");
printf("Fin de la connexion\n");
fclose(sockdial_stream);
pthread_exit(NULL);
}
int lanceThread(void(* fonction) (void *), void* arg, int size)
{
pthread_t thr_id;
printf("%d \n",size);
if(pthread_create(&thr_id, NULL, (void*)fonction, arg )!=0) { fprintf(stderr, "Le thread n'a pas pu être créé.\n"); return -1; }
pthread_detach(thr_id);
return 0;
}
|