gcc:添加新变量后的错误行为

发布于 2024-10-31 10:23:45 字数 2500 浏览 0 评论 0原文

我正在编写简单的程序:服务器和客户端。你知道,我只是在学习所有这些东西。

我添加了新变量(server.c 中的 fileUp),客户端崩溃了。我用gdb调试了一下。客户端无法从套接字读取任何内容。没有这个变量就可以正常工作。

我确实使用 gcc 和 g++ 以及 -Wall 编译了这些程序。没有错误,没有警告。

程序尽可能简单。我不明白出了什么问题。

任何提示将不胜感激。

服务器.c

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
  struct sockaddr_in address, client;
  int s = socket(AF_INET, SOCK_STREAM, 0);

  memset(&address, 0, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = htonl(INADDR_ANY);
#define PORT 54321
  address.sin_port = htons(PORT);


  if(bind(s, (struct sockaddr *)&address, sizeof(address))<0) {
    perror("nie udał się bind");
    exit(-1);
  }

  if(listen(s, 5)<0) {
    perror("nie udał się listen");
    exit(-1);
  }

  socklen_t client_len;
  int c = accept(s, (struct sockaddr *)&client, &client_len);

  int file = open("../data", O_RDONLY);
  if(file<0) {
    perror("nie udało się otworzyć pliku");
    exit(-1);
  }

#define MAX 1024
  char buf[MAX];
  int n = read(file, buf, MAX);
  int fileUp = n;

  do {
    write(c, buf, MAX);
    buf[n-1] = '\0';
    printf("%d: %s\n", n, buf);
    /*fileUp += n;
      printf("pobrano: %d\n", fileUp);*/
    n = read(file, buf, MAX);
    getchar();
  } while(n != 0);

  close(c);
  close(s);

  return 0;
}

客户端.c

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
  struct sockaddr_in address;
  int s = socket(PF_INET, SOCK_STREAM, 0);

  memset(&address, 0, sizeof(address));
  address.sin_family = AF_INET;
#define PORT 54321
  address.sin_port = htons(PORT);

  if(inet_pton(AF_INET, argv[1], &address.sin_addr) <=0) {
    perror("podano nieprawidłowy adres");
    exit(-1);
  }

  if(connect(s, (struct sockaddr *)&address, sizeof(address))<0) {
    perror("nie można się połączyć");
    exit(-1);
  }

#define MAX 1024
  char buf[MAX];
  int n = read(s, buf, MAX);
  int fileDown = n;

  do {
    buf[n-1] = '\0';
    printf("%d: %s\n", n, buf);
    n = read(s, buf, MAX);
    fileDown += n;
    printf("pobrano: %d\n", fileDown);
  } while(n != 0);

  close(s);
  return 0;
}

I am writing simple programs: server and client. You know, I am just learning all these stuff.

I added new variable (fileUp in server.c) and the client just crashed. I debugged it with gdb. The client can't read anything from the socket. Without that one variable works fine.

I did compile these programs with both gcc and g++ with -Wall. No errors, no warnings.

Programs are as simple as they can be. I don't understand what is wrong.

Any hint'll be appreciated.

server.c

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
  struct sockaddr_in address, client;
  int s = socket(AF_INET, SOCK_STREAM, 0);

  memset(&address, 0, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = htonl(INADDR_ANY);
#define PORT 54321
  address.sin_port = htons(PORT);


  if(bind(s, (struct sockaddr *)&address, sizeof(address))<0) {
    perror("nie udał się bind");
    exit(-1);
  }

  if(listen(s, 5)<0) {
    perror("nie udał się listen");
    exit(-1);
  }

  socklen_t client_len;
  int c = accept(s, (struct sockaddr *)&client, &client_len);

  int file = open("../data", O_RDONLY);
  if(file<0) {
    perror("nie udało się otworzyć pliku");
    exit(-1);
  }

#define MAX 1024
  char buf[MAX];
  int n = read(file, buf, MAX);
  int fileUp = n;

  do {
    write(c, buf, MAX);
    buf[n-1] = '\0';
    printf("%d: %s\n", n, buf);
    /*fileUp += n;
      printf("pobrano: %d\n", fileUp);*/
    n = read(file, buf, MAX);
    getchar();
  } while(n != 0);

  close(c);
  close(s);

  return 0;
}

client.c

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
  struct sockaddr_in address;
  int s = socket(PF_INET, SOCK_STREAM, 0);

  memset(&address, 0, sizeof(address));
  address.sin_family = AF_INET;
#define PORT 54321
  address.sin_port = htons(PORT);

  if(inet_pton(AF_INET, argv[1], &address.sin_addr) <=0) {
    perror("podano nieprawidłowy adres");
    exit(-1);
  }

  if(connect(s, (struct sockaddr *)&address, sizeof(address))<0) {
    perror("nie można się połączyć");
    exit(-1);
  }

#define MAX 1024
  char buf[MAX];
  int n = read(s, buf, MAX);
  int fileDown = n;

  do {
    buf[n-1] = '\0';
    printf("%d: %s\n", n, buf);
    n = read(s, buf, MAX);
    fileDown += n;
    printf("pobrano: %d\n", fileDown);
  } while(n != 0);

  close(s);
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2024-11-07 10:23:45

socklen_t client_len; 应该是 socklen_t client_len = sizeof(client);

当您添加新变量时,堆栈布局将会改变 - 因此 client_len 中的未初始化值code> 之前恰好可以工作,之后就不行了 - 最有可能使您的 accept 调用失败,然后您尝试写入无效的 FD。

您当然还应该检查 accept 的返回值

socklen_t client_len; should be socklen_t client_len = sizeof(client);

The stack layout will change when you add your new variable - so the uninitialized value in client_len just happened to work before, it doesn't after - most likely making your accept call fail, and then you're trying to write to an invalid FD.

You should of course also check the return value of accept

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文