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

📄 mp3client.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 <errno.h>#include <syslog.h>#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <arpa/inet.h>#include "mp3client.h"/********************************************************************** *  * ZhanYi_10617445_HomeWork04 *  * DESCRIPTION * * The purpose of this program is to run as a client, which can   *   send requirement for a mp3 file to the server, receive the mp3  *   stream, then decode and play the stream. * * The mp3 stream is decoded by functions in mymad.h and mymad.c,  *   using libmad library. And the PCM data is processed by functions *   in myalsa.h and myalsa.c, using asound library and alsa drivers * * The format of requirement is  *                     [<filename>\n\r] *  * USEAGE * * "usage: ./mp3client myfiles://<ip_address>:<port>/<filename> * * <ip_address> should be a IPv4 address in standard dot notation. *   IPv6 is not tested. A host and domain name will not be accepted, *   probably leading to the failure of connect function. * * <port> is specified by the server(print to the console), other *   values can't work. * * The prefixed "myfiles://" is needed in the argument passed to  *   this program.  * * A instance as follow *     ./mp3client myfiles://127.0.0.1:2024/ScarboboroughFair.mp3 **********************************************************************/intmain(int argc, char *argv[]){    int sockfd, ret_val;    struct sockaddr_in serveraddr;    ssize_t n;    char requirement[PATH_MAX+2];    unsigned char buf[CLI_BUF_SIZE];            if (argc != 2) {            /* check arguments */        fprintf(stderr, "usage: %s myfiles://<ip_address>:<port>/<filename>\n", argv[0]);        fprintf(stderr, "for a instance: %s myfiles://127.0.0.1:2024/ScarboboroughFair.mp3\n", argv[0]);        exit(0);    }        sockfd = socket(AF_INET, SOCK_STREAM, 0);    if (sockfd == -1)         err_quit("socket");    /* parse the arguments, init the serveraddr and requirement*/    parse_init(argv[1], &serveraddr, requirement);       if (connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {        perror("connect");        exit(1);    }        n = send(sockfd, requirement, strlen(requirement), 0);    if (n == -1)         err_quit("send");        mad(sockfd);} void                            /* parse the arguments pass to this program */ parse_init(char *arg, struct sockaddr_in *serveraddr, char *requirement) {    int ret_val;    unsigned int ip;    char *ip_str, *port_str, *filename;    ret_val = strncmp(arg, "myfiles://", strlen("myfiles://"));    if (ret_val == 0) {        ip_str = arg + strlen("myfiles://");        port_str = strchr(ip_str, ':');        if (port_str == NULL)            err_exit("<port> not specified");        *port_str++ = '\0';        filename = strchr(port_str, '/');        if (filename == NULL)            err_exit("<filename> not specified");        *filename++ = '\0';    } else         err_exit("unkown protocol");            /* requirement in this format: [<filename>\n\r] */    if (sprintf(requirement, "%s\n\r",filename) < 0)     err_quit("sprintf");        bzero(serveraddr, sizeof(*serveraddr));    serveraddr->sin_family = AF_INET;    if (inet_pton(AF_INET, ip_str, &ip) <= 0 ) {        perror("inet_pton");        exit(1);    }    serveraddr->sin_addr.s_addr = ip;    serveraddr->sin_port = htons(atoi(port_str));}

⌨️ 快捷键说明

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