接收UDP消息但recv返回0

时间:2017-10-05 18:39:56

标签: c sockets udp

问题:我有两个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

如您所见,我在服务器上收到的消息被更改为&#34;您的消息是:&#34;在开始时,我在客户端看到了这一点,但长度的返回值为0。这是为什么?

此代码只是略微修改了示例UDP服务器客户端的代码示例,因此如果指出代码质量不好,我将不会被冒犯!

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

2 个答案:

答案 0 :(得分:1)

通常你应该使用recvfrom:

来自recv(2)man-page:

  

recv()调用通常仅用于连接的套接字(请参阅connect(2)),并且与recvfrom()相同          NULL src_addr参数。

和socket(2)man-page:

  

SOCK_DGRAM和SOCK_RAW套接字允许向sendto(2)调用中指定的通信方发送数据报。数据报          通常接收recvfrom(2),它返回下一个数据报及其发送者的地址。

使用recv(2)read(2)

要使用recv(2)read(2),因为您要交换大量数据报,您必须先致电connect(2)

if (connect(fd,remote_addr,sizeof(remote_addr))==-1) {
    die("%s",strerror(errno));
}

现在,您可以选择recvwrite

答案 1 :(得分:0)

我能够解决这个问题,似乎我忘了用括号中的recv包装我的if语句,所以我给msglen分配了条件的bool值

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

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

修复是

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

感谢那些试图寻求帮助的人