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) ;
} |
Partager