【问题标题】:Why am I receiving more bytes than I am sending C?为什么我收到的字节比我发送的 C 多?
【发布时间】:2016-11-08 10:42:13
【问题描述】:

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

客户端代码

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

附上截图

客户端发送 =>

[服务器接收] =>

【问题讨论】:

  • 发送尺寸时,应使用sizeof(size),而不是sizeof(stat_buf.st_size)

标签: c sockets


【解决方案1】:

在客户端你这样做:

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

但是在你做的服务器中:

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

size 是一个int,即 4 个字节。但是st_sizeoff_t,大概是8个字节。因此,您只读取大小的前 4 个字节,其余部分复制到文件中。这就是为什么您最终会在服务器上多出 4 个字节。

用与st_size相同的数据类型声明size,而不是int,你的问题应该会解决。

【讨论】:

    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多