UDP recvfrom无法正常工作

时间:2015-10-01 06:09:16

标签: c++ windows sockets udp

我有UDP服务器程序,它从端口7888接收数据。服务器代​​码如下。

//UDPIPV4Server.h
#pragma once

#include <iostream>
#include <string>
#include <winsock2.h>
#include <windows.h>
#include <Ws2tcpip.h>

using std::string;
using std::cout;
using std::endl;
using std::cerr;

class UDPIPV4Server
{
public:
    UDPIPV4Server();
    ~UDPIPV4Server(void);
    UINT type;
    string mac_address;
    UINT port;
    int socket_var;
    struct sockaddr_in si_server, si_client;


    int Config(void);
    int RecvData(char* recv_buffer, int buf_size,string *ip_addr);
};

//UDPIPV4Server.cpp
int UDPIPV4Server::Config(void) {
    WSADATA wsadata;
    int error = WSAStartup(0X0202, &wsadata);

    if(error) {
        cerr<<"UdpIPV4Server.cpp:- WSAStartup failed"<<endl;
        return -1;
    }
    if ((socket_var = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
        cerr<<"UdpIPV4Server.cpp:- socket function failed"<<endl;
        return -1;
    }
    memset((char *) &si_server, 0, sizeof(si_server));
    si_server.sin_family = AF_INET;
    si_server.sin_port = htons(7888);
    char host[NI_MAXHOST] = "10.8.0.2";
    if(inet_pton(AF_INET, host, &si_server.sin_addr) != 1) {
        cerr<<"UdpIPV4Server.cpp: inet_pton() failed\n";
        return -1;
    }
    if(bind(socket_var,(struct sockaddr *)&si_server,sizeof(si_server)) == -1) {
            cerr<<"UdpIPV4Server.cpp:- bind failed: "<<endl;
            return -1;
    }
    return 0;
}

//recv data from the UDP client
//recv_buffer - [out] receive buffer
//buf_size - [in] size of receive buffer in bytes
//ip_addr - [out] ip address of the remote client
int UDPIPV4Server::RecvData(char* recv_buffer, int buf_size, string *ip_addr) {
    int recv_len;
    cout<<"waiting for data\n";
    memset((char*)&si_client, 0, sizeof(struct sockaddr_in));
    int si_client_len = sizeof(si_client);
    if((recv_len = recvfrom(socket_var, recv_buffer, buf_size, 0, (struct sockaddr *)&si_client, &si_client_len)) == -1) {
        cerr<<"udpipv4server.cpp:- recvfrom failed"<<endl;
        return recv_len;
    }
    char client_addr[INET_ADDRSTRLEN];
    cout<<"Received packets from "<<inet_ntoa(si_client.sin_addr)<<":"<<ntohs(si_client.sin_port)<<endl;
    *ip_addr = inet_ntoa(si_client.sin_addr);
    return recv_len;
}
//main.cpp
#include "UDPIPV4Server.h"
int main() {
UDPIPV4Server udp_server;
udp_server.Config();
char recv_frame_buffer[65534] = "";
memset(recv_frame_buffer,0,sizeof(recv_frame_buffer));
int retval;
string ip_addr;
if((retval = udp_server.RecvData(recv_frame_buffer,sizeof(recv_frame_buffer),&ip_addr)) ==  -1) {
    cerr<<"ReceiverCommModule:- Error in receving data"<<endl;
    continue;
}
}

上述程序在10.8.0.2:7888上接收数据。但是,当收到数据时,此代码无效。我已经查询过wireshark,数据是在10.8.0.2:7888收到的。但是socket无法从端口读取数据。UDPIPV4Server.config()函数成功传递。但是UDPIPV4Server.RecvData()没有返回。 UDP recvfrom正在等待,因为没有收到数据。代码有什么问题吗?谢谢。

1 个答案:

答案 0 :(得分:2)

我有同样的问题。在UDP中,recvfrom表现得就像没有可用的数据一样,dispite wireshark确认有效的UDP数据包到达了正确的端口。它发生在我的compagny的计算机上,但它在我的个人计算机上工作正常。我想这与windows fire wall或antivir有关,如果你的软件不允许相关的端口,它会过滤传入的数据包。我注意到recvfrom工作一次,就在通过同一个端口发送数据包之后。可能是这与我的电脑配置有关。