gpt4 book ai didi

c - 收到UDP报文但recv返回0

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:45 25 4
gpt4 key购买 nike

问题: 我有两个 c 程序,一个发送 UDP 消息,然后得到响应并打印它。另一个接收 UDP 消息,对其进行修改,然后发回。我正确收到消息,正确修改(打印正常)并发送。我在另一端收到消息,但 recv 返回值为 0。

问题:为什么我得到了正确的消息,但没有返回消息的长度?这很难检查我是否收到了正确长度的消息,因为我的目标是访问状态消息的第 320 个字节

我找到了 this但我没有使用 strlen(),加上缓冲区实际上包含正确的消息。我也没有断开套接字,因为我使用的是 UDP。

这是客户端的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#define SERVICE_PORT 9090

#define BUFLEN 2048
#define MSGS 5

int main(void)
{
struct sockaddr_in myaddr, remaddr;
int fd, i, slen=sizeof(remaddr);
char *server = "127.0.0.1";
char buf[BUFLEN];
ssize_t msglen;


if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1)
printf("socket not created\n");


memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(7096);

if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed");
return 0;
}

memset((char *) &remaddr, 0, sizeof(remaddr));
remaddr.sin_family = AF_INET;
remaddr.sin_port = htons(SERVICE_PORT);
if (inet_aton(server, &remaddr.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}

char msg[4096];
memset(msg, 0, 4096);
for (i=0; i < MSGS; i++) {
printf("Sending packet %d to %s port %d\n", i, server, SERVICE_PORT);
sprintf(buf, "This is packet %d", i);
if (sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&remaddr, slen)==-1)
perror("sendto");
memset(msg, 0, 4096);
if(msglen = recv(fd, msg, sizeof(msg), MSG_WAITALL) == -1){
printf("Bad recv\n");
}
printf("recieved %d bytes: %s\n", msglen, msg);
}
close(fd);
return 0;
}

这是服务器的代码:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#include <linux/ip.h> /* for ipv4 header */
#include <linux/udp.h> /* for upd header */

#define ADDR_TO_BIND "127.0.0.1"
#define PORT_TO_BIND 9090
#define SERVICE_PORT 7096
#define MSG_SIZE 256
#define HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))

int main(void) {
int raw_socket, fd;
struct sockaddr_in sockstr, remaddr;
socklen_t socklen;

int retval = 0;

char msg[MSG_SIZE];
ssize_t msglen;


if ((raw_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return 1;
}
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return 1;
}
sockstr.sin_family = AF_INET;
sockstr.sin_port = htons(PORT_TO_BIND);
sockstr.sin_addr.s_addr = inet_addr(ADDR_TO_BIND);
socklen = (socklen_t) sizeof(sockstr);
if (bind(raw_socket, (struct sockaddr*) &sockstr, socklen) == -1) {
perror("bind");
retval = 1;
goto _go_close_socket;
}
if(bind(fd, (struct sockaddr*) &remaddr, socklen) == -1){
perror("bind");
}
memset(msg, 0, MSG_SIZE);
while(1){
if ((msglen = recv(raw_socket, msg, MSG_SIZE, 0)) == -1) {
perror("recv");
retval = 1;
goto _go_close_socket;
}
memset((char *) &remaddr, 0, sizeof(remaddr));
remaddr.sin_family = AF_INET;
remaddr.sin_port = htons(SERVICE_PORT);
if (inet_aton(ADDR_TO_BIND, &remaddr.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
unsigned char buf[4096];
msg[msglen] = '\0';
printf("recieve %d bytes: %s\n", msglen, msg);
sprintf(buf, "Your msg is: %s\n",msg );
int slen=sizeof(remaddr);
if (sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&remaddr, slen)==-1){
printf("send failed");
}
printf("sent: %s\n", buf);
}
_go_close_socket:
close(raw_socket);

return retval;
}

运行客户端的输出是:

Sending packet 0 to 127.0.0.1 port 9090
recieved 0 bytes: Your msg is: This is packet 0

Sending packet 1 to 127.0.0.1 port 9090
recieved 0 bytes: Your msg is: This is packet 1

Sending packet 2 to 127.0.0.1 port 9090
recieved 0 bytes: Your msg is: This is packet 2

Sending packet 3 to 127.0.0.1 port 9090
recieved 0 bytes: Your msg is: This is packet 3

Sending packet 4 to 127.0.0.1 port 9090
recieved 0 bytes: Your msg is: This is packet 4

运行服务器的输出是:

recieve 256 bytes: This is packet 0
sent: Your msg is: This is packet 0

recieve 256 bytes: This is packet 1
sent: Your msg is: This is packet 1

recieve 256 bytes: This is packet 2
sent: Your msg is: This is packet 2

recieve 256 bytes: This is packet 3
sent: Your msg is: This is packet 3

recieve 256 bytes: This is packet 4
sent: Your msg is: This is packet 4

如您所见,我在服务器上收到的消息更改为开头为“your msg is:”,我在客户端看到了这一点,但返回值的长度为 0。这是为什么?

这段代码只是对示例 UDP 服务器客户端的代码示例稍作修改,因此如果指出代码质量不好,我不会被冒犯!

编辑 这已解决,这是将 recv() 调用包装在括号中的问题,因此条件存储在 msglen 中而不是实际返回值中。请参阅下面的答案

最佳答案

我能够解决这个问题,似乎我忘记了将 recv 的 if 语句括在括号中,所以我为 msglen 分配了条件的 bool 值

recv(fd, msg, sizeof(msg), 0) == -1

当然是 0,因为返回值不等于 -1。

修复是

if((msglen = recv(fd, msg, sizeof(msg), 0)) = -1)

感谢那些试图帮助他们的人

关于c - 收到UDP报文但recv返回0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46592649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com