⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3server.c

📁 The purpose of this program is to run as a server, which can receive requirement from clients and se
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <syslog.h>#include <sys/types.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <arpa/inet.h>#include "mp3server.h"/********************************************************************** *  * ZhanYi_10617445_HomeWork04 *  * DESCRIPTION * * The purpose of this program is to run as a server, which can   *   receive requirement from clients and send datas to clients as *   requested. * * The data to be sended is specified as mp3 files. * * The format of requirement is  *                     [<filename>\n\r] * * The server listens on a random port choose by kernel. The port *   will be printed to the standard out when the relationship between *   the program and the port is established. *   The port should be used as part of the argument of the client  *   end program. * * USEAGE * * ./mp3server <directory_name> * * If no argument is present, a default argument '.' (the current *   directory) is assumed. **********************************************************************/intmain(int argc, char *argv[]){    int sockfd, ret_val;    struct sockaddr_in servaddr;    if (argc > 2) {             /* check the arguments */        fprintf(stderr, "usage: ./mp3server <directory_name>");        exit(0);    }    if (argc == 2) {        /* change the current directory if directory name is specified*/        ret_val = chdir(argv[1]);               if (ret_val < 0)             err_quit("chdir");    }                sockfd = server_init(&servaddr); /* initialise the server */    serve(sockfd);                   /* get ready to accept requirements */}int                             /* initialise the server */server_init(struct sockaddr_in *servaddr){    int sockfd, ret_val;        struct sockaddr_in tempaddr;    socklen_t templen;    sockfd = socket(AF_INET, SOCK_STREAM, 0);     if (sockfd == -1)         err_quit("socket");            bzero(servaddr, sizeof(*servaddr));    servaddr->sin_family = AF_INET;    servaddr->sin_addr.s_addr = htonl(INADDR_ANY);    servaddr->sin_port = 0;    ret_val = bind(sockfd, (struct sockaddr *)servaddr, sizeof(*servaddr));    if (ret_val == -1)         err_quit("bind");        ret_val = listen(sockfd, QLEN);    if (ret_val == -1)         err_quit("listen");        templen = sizeof(struct sockaddr);    ret_val = getsockname(sockfd,(struct sockaddr*)&tempaddr,&templen);    if(ret_val == -1){        perror("getsockname");        exit(1);    }        fprintf(stdout, "Mp3Server is listening on port %hu\n", ntohs(tempaddr.sin_port));            return sockfd;}    void                            /* get ready to accept requirements */serve(int sockfd){    int mp3fd, clfd;    ssize_t n;    char buf[SERV_BUF_SIZE];    struct sockaddr_in claddr;    socklen_t clilen = sizeof(claddr);            while (1) {        clfd = accept(sockfd, (struct sockaddr *)&claddr, &clilen);        if (clfd < 0)             err_quit("accept");                fprintf(stdout, "A client form the address %s connected.\n", inet_ntoa(claddr.sin_addr));                n = recv(clfd, buf, SERV_BUF_SIZE, 0);        if (n < 0)             err_quit("recv");                buf[n-2] = '\0';        fprintf(stdout, "The client requires the mp3 file: %s\n", buf);                if ((mp3fd = open(buf, O_RDONLY)) < 0) {            sprintf(buf, "the mp3 file %s do not exist\n", buf);            err_cont("buf");        } else {            while ((n = read(mp3fd, buf, SERV_BUF_SIZE)) > 0) {                send(clfd, buf, n, 0);            }            close(mp3fd);        }        close(clfd);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -