2016-11-08 105 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,並且應解決您的問題。

相關問題