int send_data(int fd,char* data, sizet size) { ssizet bytessize; while(size>0) {…
- User
int send_data(int fd,char* data, size_t size)
{
codessize_t bytes_size; while(size>0) { bytes_size=write(fd,data,size); if(bytes_size>=0) { size-=bytes_size; data+=bytes_size; } else return -1; } return 0;}
int send_file(int fd, int file)
{
codeconst int buf_size = 4096; char buf[buf_size]; ssize_t bytes_read=read(file, buf, buf_size); int status=0; while (bytes_read>0) { status=send_data(fd, buf, bytes_read); if (status<0) return status; bytes_read=read(file, buf, buf_size); } return status;}
int send_string(int fd, char* data)
{
codereturn send_data(fd,data,strlen(data));}
char* join_string(char *str1, char *str2, size_t *size)
{
codesize_t len1 = strlen(str1); size_t len2 = strlen(str2); char *buffer = (char *) malloc(len1 + len2 + 1); memcpy(buffer,str1,len1); memcpy(buffer+len1,str2,len2+1); if (size != NULL) *size=(len1+len2)*sizeof(char); return buffer;}
int get_file_size(int fd)
{
codestruct stat st; if (fstat(fd,&st)==-1) { return -1; } return st.st_size;}
void serve_file(int fd, char* path) {
/* TODO: PART 2 */
/* PART 2 BEGIN */
int file=open(path,O_RDONLY);
char* content_length=(char*) malloc(sizeof(char)*100);
snprintf(content_length, 100,"%d",get_file_size(file));
http_start_response(fd, 200);
http_send_header(fd, "Content-Type", http_get_mime_type(path));
http_send_header(fd, "Content-Length", content_length); // TODO: change this line too
http_end_headers(fd);
send_file(fd,file);
- Assistant
void serve_directory(int fd, char* path) {
http_start_response(fd, 200);
http_send_header(fd, "Content-Type", http_get_mime_type(".html"));
http_end_headers(fd);
/* TODO: PART 3 */
/* PART 3 BEGIN */
DIR *directory=opendir(path);
struct dirent* entry;
- User
#elif FORKSERVER
code/* * TODO: PART 5 * * When a client connection has been accepted, a new * process is spawned. This child process will send * a response to the client. Afterwards, the child * process should exit. During this time, the parent * process should continue listening and accepting * connections. */ /* PART 5 BEGIN */ /* PART 5 END */为了实现基于Fork的服务器,你无需编写太多新代码。
子进程应使用客户端套接字 fd 调用 request_handler。提供响应后,子进程将终止。 父进程将继续监听并接受传入连接。它不会等待子进程。 请记住,父进程和子进程都要适当关闭套接字。
- Assistant
#elif FORKSERVER
code/* * TODO: PART 5 * * When a client connection has been accepted, a new * process is spawned. This child process will send * a response to the client. Afterwards, the child * process should exit. During this time, the parent * process should continue listening and accepting * connections. */ /* PART 5 BEGIN */ pid_t pid = fork(); if (pid == 0) { close(server_fd); handle_files_request(fd); close(fd); exit(0); } else if (pid < 0) { perror("fork"); } else { close(fd); } /* PART 5 END */ - User
#elif FORKSERVER /* * TODO: PART 5 * * When a client connection has been accepted, a new * process is spawned. This child process will send * a response to the client. Afterwards, the child * process should exit. During this time, the parent * process should continue listening and accepting * connections. / / PART 5 BEGIN / pid_t pid = fork(); if (pid == 0) { close(server_fd); handle_files_request(fd); close(fd); exit(0); } else if (pid < 0) { perror(“fork”); } else { close(fd); } / PART 5 END */解释代码
- Assistant
在基于Fork的服务器中,当一个客户端连接被接受时,会创建一个新的进程。这个子进程负责发送响应给客户端,完成后子进程将终止。在此期间,父进程将继续监听并接受传入连接。
在代码中的PART 5部分,我们使用fork()系统调用创建一个子进程。如果pid等于0,说明当前执行的是子进程代码。子进程会关闭父进程的监听套接字server_fd,然后调用handle_files_request处理客户端的请求并发送响应。最后,子进程关闭客户端套接字fd,并调用exit(0)终止子进程。
如果pid小于0,说明fork()调用失败,会打印错误信息。
如果pid大于0,说明当前执行的是父进程代码。父进程会关闭客户端套接字fd,并继续监听和接受传入连接。
通过这种方式,基于Fork的服务器可以并行处理多个客户端请求,每个请求在独立的子进程中处理,实现了并发性。
- User
- Assistant
void serve_forever(int* socket_number, void (*request_handler)(int)) {
struct sockaddr_in server_address, client_address;
size_t client_address_length = sizeof(client_address);
int client_socket_number;
// Creates a socket for IPv4 and TCP.
*socket_number = socket(PF_INET, SOCK_STREAM, 0);
if (*socket_number == -1) {
codeperror("Failed to create a new socket"); exit(errno);}
int socket_option = 1;
if (setsockopt(*socket_number, SOL_SOCKET, SO_REUSEADDR, &socket_option, sizeof(socket_option)) ==
code-1) { perror("Failed to set socket options"); exit(errno);}
// Setup arguments for bind()
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;