2016-11-08 86 views
1

我收到的字节数超过了我在服务器端发送的数量,并且我收到的文件在我的文件开始处有一些垃圾字符。为什么我收到比我发送C更多的字节?

客户机代码

#include <arpa/inet.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <sys/sendfile.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <sys/stat.h> 
#include <netinet/in.h> 
#include <dirent.h> 
#include <time.h> 

int main() { 
    int serverPort, clientPort, clientSock, serverSock; 
    scanf("%d", &clientPort); 
    struct sockaddr_in cadd, sadd1, sadd2, clen, slen; 
    cadd.sin_family = AF_INET; 
    cadd.sin_addr.s_addr = inet_addr("127.0.0.1"); 
    cadd.sin_port = htons(clientPort); 
    clientSock = socket(AF_INET, SOCK_STREAM, 0); 
    if(clientSock == -1) 
    fprintf(stderr, "unable to create socket: %s\n", strerror(errno)); 
    int result = connect(clientSock, (struct sockaddr *)&cadd, sizeof(cadd)); 
    if(result == -1) { 
    fprintf(stderr, "unable to create socket: %s\n", strerror(errno)); 
    return -1; 
    } 
    while(1) { 
    struct stat stat_buf; 
    off_t offset = 0;; 
    int choice = 1; 
    int fd = open("myList.txt", O_RDONLY); 
    if (fd == -1) { 
     fprintf(stderr, "unable to open %s", strerror(errno)); 
     exit(1); 
    } 
    fstat(fd, &stat_buf); 
    offset = 0; 
    int size = (int)stat_buf.st_size; 
    printf("File size: %d\n", size); 
    send(clientSock, &size, sizeof(stat_buf.st_size), 0); 
    int sent = sendfile(clientSock, fd, &offset, stat_buf.st_size); 
    if(sent == -1) { 
     fprintf(stderr, "error sendfile: %s\n", strerror(errno)); 
     exit(1); 
    } 
    if(sent != stat_buf.st_size) { 
     fprintf(stderr, "error sendfile %d of %d bytes\n", sent, (int)stat_buf.st_size); 
     exit(1); 
    } 
    printf("sendfile succesfull %d\n", sent); 
    break; 
    } 
} 

服务器代码

#include <arpa/inet.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <sys/sendfile.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <sys/stat.h> 
#include <netinet/in.h> 
#include <dirent.h> 
#include <time.h> 

int main() { 
    int serverPort, sock; 
    printf("Enter port number: "); 
    scanf("%d", &serverPort); 
    struct sockaddr_in server1, server2; 
    int addrlen = sizeof(server2); 
    sock = socket(AF_INET, SOCK_STREAM, 0); 
    if(sock == -1) { 
    fprintf(stderr, "unable to create socket: %s\n", strerror(errno)); 
    exit(1); 
    } 
    server1.sin_family = AF_INET; 
    server1.sin_port = htons(serverPort); 
    int rc = bind(sock, (struct sockaddr *) &server1, sizeof(server1)); 
    if(rc == -1) { 
    fprintf(stderr, "bind error: %s\n", strerror(errno)); 
    close(rc); 
    exit(1); 
    } 
    rc = listen(sock, 1); 
    if(rc == -1) { 
    fprintf(stderr, "listen failed: %s\n", strerror(errno)); 
    exit(1); 
    } 
    while(1) { 
    int con = accept(sock, (struct sockaddr *) &server2, &addrlen); 
    int crt = creat("please.txt", S_IRWXU), size, count = 0; 
    recv(con, &size, sizeof(size), 0); 
    while(1) { 
     char mssg[100]; 
     memset(mssg, '\0', sizeof(mssg)); 
     int n = recv(con, mssg, sizeof(mssg), 0); 
     int wrt = write(crt, mssg, n); 
     count = count + wrt; 
     printf("Count: %d\n", count); 
     if(count >= size) 
     break; 
    } 
    printf("Write successful\n"); 
    } 
} 

附加的屏幕截图

客户端发送=> enter image description here

[服务器的recv] => enter image description here

+0

发送大小时,应该使用'sizeof(size)',而不是'sizeof(stat_buf.st_size)'。 – Barmar

回答

0

在客户端你这样做:

send(clientSock, &size, sizeof(stat_buf.st_size), 0); 

但是在服务器这样做:

recv(con, &size, sizeof(size), 0); 

sizeint,也就是4个字节。但是st_sizeoff_t,推测是8字节。所以你只能读取大小的前4个字节,而剩下的则被复制到文件中。这就是为什么你最终在服务器上增加4个字节的原因。

声明size具有与st_size相同的数据类型,而不是int,并且应解决您的问题。

相关问题