Salut les C réseau,
J'essaye d'implémenter un humble serveur HTTP qui renvoie une page HTML contenant une image seulement le problème est le suivant:
Le navigateur affiche la page HTML mais pas l'image contenus dans la balise <img> de celle-ci et ne fait pas de requête afin d'obtenir celle-ci comme indiquer dans le bouquin d'oû je sort l'idée:
Le code est le suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
 
#include <sys/types.h> 
#include <sys/socket.h>
#include <sys/stat.h> 
 
#include <netinet/in.h>
#include <arpa/inet.h>
 
void handle_connection(int sock_client_id,struct sockaddr_in *client_addr) ;
int get_file_length(int fd) ;
 
main(int argc, char *argv[]) {
  int sock_server_id,sock_client_id ;
  int  optval=1 ;
 
  struct sockaddr_in sockname, client_addr ;
  struct in_addr server_addr ;
 
 
  if ((sock_server_id=socket(AF_INET,SOCK_STREAM,0)) == -1) {
    printf("error creating server socket\n") ;
    return ;
 
  }
 
  setsockopt(sock_server_id, SOL_SOCKET, SO_REUSEADDR,&optval, sizeof(int)) ;
 
  inet_aton("127.0.0.1",&server_addr) ; //catch the address in a in_addr structure                     
  // Memory settings:
  memset((char *) &sockname,0,sizeof(struct sockaddr_in)) ;
  sockname.sin_family=AF_INET ;
  sockname.sin_port=htons(80) ;
  sockname.sin_addr.s_addr=server_addr.s_addr ;
 
  //Bind the server and make it listen
  bind(sock_server_id,(struct sockaddr *) &sockname,sizeof(struct sockaddr_in));
  printf("listen on address: %s on port:  %d\n",inet_ntoa(sockname.sin_addr),ntohs(sockname.sin_port)) ;
  listen(sock_server_id, -1) ;
  printf("waiting connection entry...\n") ;
 
  socklen_t sin_size=sizeof(struct sockaddr_in) ;
  while ( 1 == 1) {
    // eternal loop in waiting for connections
    sock_client_id=accept(sock_server_id,(struct sockaddr *)&client_addr, &sin_size) ;
    handle_connection(sock_client_id,&client_addr) ; // Function to handle request
  }
 
  // Shut down process
  shutdown(sock_server_id,SHUT_RDWR) ;
  close(sock_server_id) ;
  shutdown(sock_client_id,SHUT_RDWR) ;
  close(sock_client_id) ;
 
  }
 
void handle_connection(int sock_client_id,struct sockaddr_in *client_addr) {
  unsigned char *ptr, request[512],ressource[512],*req_file ; ;
  int length,fd ;
  char *root_dir="/home/nom/C/" ;
 
  length=recv(sock_client_id,request,512,0) ;
  printf("Get request from: %s from port: %d\n%s",inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port),request) ;
  if (strncmp("GET / HTTP/1.1",request,14) == 0) {
    req_file="index.html" ;
    /* Content of index.html:
     * 
      <!DOCTYPE html>
      <html lang='fr'>
      <title>server test</title>
      <meta charset='utf-8'>
      <head>
      <style></style>
      </head>
      <body>Hello client <br>
      I show you the image like this:
      <br>
      <img src="http://127.0.0.1:80/image.jpg">
      </body>
      </html>
    *
    */
  } 
  else if (strncmp("GET /image.jpg HTTP/1.1",request,23)) {
    req_file="image.jpg" ;
  }
  strcpy(ressource,root_dir) ;
  strcat(ressource,req_file) ;
  fd=open(ressource,O_RDONLY,0) ;
  // Send the headers
  send(sock_client_id,"HTTP/1.1 200 OK\r\n",strlen("HTTP/1.1 200 OK\r\n"),0) ;
  send(sock_client_id,"Server: The test server\r\n\r\n",strlen("Server: The test server\r\n\r\n"),0)  ;
 
  while ((read(fd,ptr,get_file_length(fd))) > 0) { 
    // Send the requested file
    send(sock_client_id,ptr,get_file_length(fd),0) ;
  }
  free(ptr) ;
  close(fd) ;
}  
 
 
int get_file_length(int fd) {
  struct stat stat_file ;
  fstat(fd,&stat_file) ;
  return (int) (stat_file.st_size) ;
}
En faite le serveur, lancer en tant que root étant donnée la valeur du port écoutant, renvoie la page index.html puis quitte au lieu de continuer dans la boucle ou il est censé être prisonnier.
j'ai la sortie suivante:
nom@nom-desktop:~$ sudo ~/C/server
listen on address: 127.0.0.1 on port: 80
waiting connection entry...
Get request from: 127.0.0.1 from port: 35679
GET / HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
nom@nom-desktop:~$
Puis retour au prompt.

Si vous avez une idée du pourquoi du comment du comportement du serveur ou si vous avez un conseil, un avis, une critique merci de poster.