C中的字符串中的空字符



美好的一天,

我试图发送一个"对象"槽插座。结构看起来像这样:

typedef struct header_file
{
    char chunk_id[4];
    int chunk_size;
    char format[4];
    char subchunk1_id[4];
    int subchunk1_size;
    short int audio_format;
    short int num_channels;
    int sample_rate;
    int byte_rate;
    short int block_align;
    short int bits_per_sample;
    char subchunk2_id[4];
    int subchunk2_size;     
} header;

这是WAV声音文件的标题结构。要读取文件的那部分,我有:

FILE* infile = fopen("Sound.wav", "rb");
header *meta = (header*)malloc(sizeof(header));
if(infile){
    fread(meta, 1, sizeof(header), infile);
}

到目前为止,如果我尝试显示结构中包含的所有信息,那就没问题了!但是,我应该通过插座发送此部分:

n = write(socket_fd, meta, sizeof(header));

也有效,但是在服务器端,它仅接收到:

RIFF*_

前4个字符是结构的数组chunk_id中的字符,chunk size352042,在HEXA中给出0x00055F2A

我认为(至少我是这样认为(,由于零件的零,服务器将其读为角色,并猜猜0x00的含义是什么?无效的!字符串的结尾!我该如何解决?

事先感谢您的帮助

完成客户端代码

void* stream_audio(void *arg);
typedef struct header_file
{
    char        chunk_id[4];
    int         chunk_size;
    char        format[4];
    char        subchunk1_id[4];
    int         subchunk1_size;
    short int   audio_format;
    short int   num_channels;
    int         sample_rate;
    int         byte_rate;
    short int   block_align;
    short int   bits_per_sample;
    char        samples_id[4];
    int         samples_size;
} header;
typedef struct header_file* header_p;
int         KEEP_ALIVE     = 1;
int         PORT_NO        = 5002;
char        SERVER_IP[20]  = "127.0.0.1";
pthread_t   t_id;
int main(int argc, char * argv[]) {
    int terr;
    terr        = pthread_create(&t_id, NULL, &stream_audio, NULL);
    pthread_join(t_id, NULL);
}

void* stream_audio(void *arg){
    FILE *              infile = fopen("../files/man1_nb.wav","rb");    // Source WAV audio file
    int                 sockfd, portno, n;
    struct sockaddr_in  serv_addr;
    struct hostent      *server;    
    int                 count   = 0;    // For counting number of frames in wave file.
    char                buff16[2];          // short int used for 16 bit as input data format is 16 bit PCM audio
    header_p            meta    = (header_p)malloc(sizeof(header)); // WAV file metadata fields
    int nb;
    portno = PORT_NO;                           // read port number from arguments
    sockfd = socket(AF_INET, SOCK_STREAM, 0);   // create socket
    if (sockfd < 0) {
        error("ERROR opening socket");
        return ;
    }
    server = gethostbyname(SERVER_IP);            // get host address from argument
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such hostn");
        return ;
    }
    // Create socket and connect to server
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,  server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
    // send header WAV file trhough socket
    if (infile){
        fread(meta, 1, sizeof(header), infile);
        meta->chunk_size = 0;
        n = write(sockfd,meta,sizeof(meta));
        if (n < 0) 
            error("ERROR writing to socket");
    } else return;
    int counter = 0;
    while (!feof(infile))
    {
        bzero(buff16, sizeof(buff16));
        nb = fread(buff16,1,1,infile);      // Reading data in chunks of 16 bits (2 bytes)
        n  = write(sockfd,buff16,sizeof(buff16));
        if(!KEEP_ALIVE){
            fclose(infile);
            close(sockfd);
            return;
        }
    }
    fclose(infile);
    close(sockfd);
    return;
}

完成服务器代码

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
    perror(msg);
    exit(1);
}
int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno, clilen;
     char buffer[401];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port providedn");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,401);
     char temper[100];
     sprintf(temper, "/home/user/Documents/manif/bin/receiver%d.wav", portno);
     FILE *receiver = fopen(temper,"wb+");
     int empty = 0;
     while(1){
         memset(buffer,0,400);
         n = read(newsockfd,buffer,400);
        if (n < 0){
            error("ERROR reading from socket");
            break;
        } 
        else if(n>0){
            fwrite(buffer,1, n, receiver);
        } else  break;
        printf("%s   %dn",buffer, n);
     }
     fclose(receiver);
     close(sockfd);
     return 0; 
}

您通过网络发送的内容是包含多个元素的struct。但是,您正在尝试打印您所读的字符串的内容,但不是字符串。

您需要知道您收到的数据的格式并以这种格式阅读。

如果您仅用于调试目的打印,请循环循环读取并单独打印每个字节作为数字。您可能想在发件人侧进行相同的操作以比较它们。

此外,在客户端,您对feof的使用是不正确的。相反,您应该检查fread的返回值,以查看您是否到达文件端。这很可能是不匹配的原因。

printf将打印ascii,non ascii(将在屏幕上看起来垃圾(字符,直到遇到 0,这就是为什么您在屏幕上只能在屏幕上获得很少的字节,因为旁边的字节必须已经 0,如果您需要查看您需要单独显示结构的每个成员的数据,或者只需将接收到的套接字转换为在显示

时将接收到的缓冲区转换为十六进制

相关内容

  • 没有找到相关文章

最新更新