짧게 설명드리자면

클라이언트는 웹브라우저가 해줄거라 따로 필요없고

서버는 소켓생성 -> bind -> listen -> accept의 과정을 거치며

accepting loop에서 클라이언트의 요청이 들어오면 그에 맞게 response를 보냅니다.

 

몇년 전에 학교 과제용으로 만든거라 그냥 참고용으로만 봐주세요,,

우분투(64bit)에서 만들고 실행했었습니다.

따로 프로세스나 쓰레드를 생성하진 않았어요.

각 부분별로 설명을 드리자면..

헤더파일 선언 및 listen함수에 들어갈 BACKLOG값, 버퍼사이즈, 파일 이름 크기 정의입니다.

 

클라이언트가 파일을 요청하면 서버는 파일의 내용을 읽어와 보내주는데, 웹 브라우저가 이를 띄우기 위해선 앞에 html형식의 헤더를 붙여야합니다. 헤더가 없으면 제대로 사진 등을 출력하지 않아요.

어차피 앞부분은 다 똑같으니 image/jpeg , image/gif, audio/mp3, application/pdf, text/html만 따로 구분해서 만들고

앞부분도 따로 하면 메모리를 조금 덜 먹을 수 있겠습니다...

defaulthtml은 서버에 없는 파일을 요청했을 경우 보내는 html 메세지입니다.

 

실행파일 이름이 server인 경우 $./server [PORT번호] 로 실행하게 되는데, 포트번호를 입력하지 않은 경우 프로그램을 종료합니다.

소켓 파일디스크립터를 만듭니다. 

서버의 IP, 포트번호를 설정하고 

그를 이용해 소켓을 bind해줍니다.

 

listen함수입니다. BACKLOG값은 대기중인 큐에 넣을 최대 연결 수 라고 하는데 자세한건 더 찾아보시면 좋을것 같네요

 

이제 아래는 accept loop입니다.

클라이언트의 주소를 받고

콘솔창에 클라이언트의 주소를 띄워줍니다.

클라이언트가 보낸 값을 읽어옵니다.

클라이언트가 보낸 값중 GET / 뒷부분의 위치를 받고(5는 "GET /"의 length입니다)

띄어쓰기 전까지의 파일이름을 받고 뒷부분을 \0으로 채웁니다.

현재 작업중인 디렉토리에서 파일을 찾고 없으면 404 not found 메세지를 보냅니다.(defaulthtml)

 

파일이 있으면 확장자별로 헤더를 덧붙이고 전송합니다.

 

지금보니까 코딩 겁나 드럽게했네요..

암튼 서버 실행화면은 이렇게 생겼구요

포트번호 9999번으로 했을 경우

http://127.0.0.1:9999/testgif.gif 를 주소창에 치면

대충 요런식으로 파일을 읽어옵니다.

mp3,pdf,jpeg 등 파일을 열 수 있습니다.

 

봐주셔서 감사합니다.

다시 말씀드리지만 하도 오래된 코드라 참고용으로만 봐주셨으면 좋겠습니다..

아래는 복사가능한 전체코드입니다.

더보기

#include <stdio.h>
#include <arpa/inet.h> // GET IP ADDRESS IN 64 BIT MACHINE
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#define BACKLOG 10
#define BUF_SIZE 1024
#define FILE_NAME 100
//FILE HEADER(before sending file, server send this header to client)
char imageheader[] = "HTTP/1.1 200 Ok\r\n" "Content-Type: image/jpeg\r\n\r\n";
char gifheader[] = "HTTP/1.1 200 Ok\r\n" "Content-Type: image/gif\r\n\r\n";
char mp3header[] = "HTTP/1.1 200 Ok\r\n" "Content-Type: audio/mp3\r\n\r\n";
char pdfheader[] = "HTTP/1.1 200 Ok\r\n" "Content-Type: application/pdf\r\n\r\n";
char htmlheader[] = "HTTP/1.1 200 Ok\r\n" "Content-Type: text/html\r\n\r\n";
char defaulthtml[] =  "HTTP/1.1 404 Not Found\r\n"
"Content-Type : text/html; charset=UTF-8\r\n\r\n"
"<!DOCTYPE html>\r\n"
"<html><head><title>WELCOM TO MY SERVER</title>\r\n"
"<body>I've got wrong file name. check it please.</body>"
"</html>";
int main(int argc, char *argv[])
{
int sockfd, new_fd; // sockfd : server socket fd, new_fd : client socket fd
struct sockaddr_in my_addr; // server addr
struct sockaddr_in their_addr; // client addr
char buffer[BUF_SIZE];
char *strptr1;
int count=0;
int n; // reading variable
int sin_size; // client addr size
int filesize;
int PORT = atoi(argv[1]);
if(argc<2){
printf("error (no port)\n");
exit(0);
} // if there is no port #, terminate program.

///////////////////MAKE SOCKET////////////////////
if((sockfd=socket(PF_INET, SOCK_STREAM, 0))==-1)
{
perror("socket error");
exit(0);
}
///////////////////BIND SOCKET////////////////////
memset(&my_addr,0,sizeof(my_addr)); // clear by 0
my_addr.sin_family = AF_INET; // AF_INET in IPv4
my_addr.sin_port = htons(PORT); // port # , short type data(PORT)
my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 32bit IPv4 addr , long type data(IP)
if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))==-1)
{
perror("bind error");
exit(0);
}
//////////////////////LISTEN//////////////////////
if(listen(sockfd, BACKLOG)==-1)
{
perror("listen error");
exit(0);
}
//////////////////////////////////////////////////
sin_size = sizeof(struct sockaddr_in);
////////////////////ACCEPTING/////////////////////

while(1){ // accepting loop
if((new_fd=accept(sockfd, (struct sockaddr*)&their_addr, &sin_size))==-1)
{
perror("fail to accepting");
continue;
}
//show client's ip address
printf("server : got connection from %s\n", inet_ntoa(their_addr.sin_addr));
//read request message and print it
bzero(buffer,BUF_SIZE);
n=read(new_fd,buffer,BUF_SIZE);
if(n<0) perror("READING ERROR");
printf("%s", buffer);
///////FIND FILENAME///////
strptr1 = strstr(buffer,"GET /");
strptr1 += 5;
char filename[100];
char pwd[BUF_SIZE];
memset(filename,0,100);
int i=0;
int fd;
while(1)
{
if(strptr1[i]==' ')
{
filename[i] = '\0'; break;
}
filename[i]=strptr1[i];
i++;
}//get filename until meet the ' ' character.
/////FIND FILE IN WORKING DIRECTORY/////
getcwd(pwd,BUF_SIZE);
strcat(pwd,"/");
strcat(pwd,filename);
char fbuf[BUF_SIZE];
/////FILE is not in working directory/////
if((fd=open(pwd,O_RDONLY))==-1)
{
        write(new_fd,defaulthtml,sizeof(defaulthtml)-1);
printf("Server sent '404 not found message' to client\n");
}
/////FILE is in working directory/////
////////SENDING FILE TO CLIENT////////
else
{ //SEND FILE HEADER//
if(strstr(filename,".jpg")!=NULL||strstr(filename,".jpeg")!=NULL)
write(new_fd,imageheader,sizeof(imageheader)-1);
else if(strstr(filename,".gif")!=NULL)
write(new_fd,gifheader,sizeof(gifheader)-1);
else if(strstr(filename,".pdf")!=NULL)
write(new_fd,pdfheader,sizeof(pdfheader)-1);
else if(strstr(filename,".mp3")!=NULL)
write(new_fd,mp3header,sizeof(mp3header)-1);
else if(strstr(filename,".html")!=NULL)
write(new_fd,htmlheader,sizeof(htmlheader)-1);
        //SEND FILE//
while((n=read(fd,fbuf,BUF_SIZE))>0)
         write(new_fd,fbuf,n);
        printf("Server sent file completly : %s\n\n", filename);
}
close(new_fd);
close(fd);
}
}

+ Recent posts