gcc:添加新变量后的错误行为(gcc: wrong behaviour after new variable was added)

编程入门 行业动态 更新时间:2024-10-26 10:29:37
gcc:添加新变量后的错误行为(gcc: wrong behaviour after new variable was added)

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

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

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

程序尽可能简单。 我不明白有什么不对。

任何提示都将不胜感激。

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; }

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; }

最满意答案

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

添加新变量时堆栈布局会发生变化 - 所以client_len的未初始化值恰好在之前工作,它不会在之后 - 很可能使您的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

更多推荐

本文发布于:2023-07-15 19:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   错误   gcc   wrong   added

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!