为什么我收到的字节多于发送C的字节数?

时间:2016-11-08 02:15:13

标签: c sockets

我收到的字节比我在服务器端发送的字节数多,而且我收到的文件在文件开头有一些乱码。

客户端代码

#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");
  }
}

附加屏幕截图

client send =&gt; enter image description here

[server recv] =&gt; enter image description here

1 个答案:

答案 0 :(得分: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,并且您的问题应该得到解决。