无法使用udp协议从网络接收数据

时间:2013-07-18 21:01:28

标签: c++ linux udp

我的目标是编写C ++程序,使用udp协议从网络接收数据。 OS - linux。我几乎没有问题。

  1. 我想确保我可以使用linux C库来编写C ++程序。
  2. 是否有任何存在的完整教程,如何在linux中使用套接字编程一步一步地进行操作? (我知道网络中有很多教程,但我正在寻找能帮助我从INTERNET接收udp数据的东西,而不是同一设备上的其他程序。)。
  3. 我在终端(使用tcpdump)中看到我的计算机收到发往指定端口的udp包。我的程序有什么问题,看起来像这样

    class Connection
    {
    private:
        char * buffer;
        int bufferSize;
        int sock;
        struct sockaddr_in server;
        struct sockaddr_in other;
        socklen_t addressLength;
    
    public:
        Connection(string address, int port, int bufferSize);
        ~Connection();
        int reciveData();
    };
    
    Connection::Connection(string address, int port, int bufferSize)
    {
        this->addressLength = sizeof(this->server);
    
        this->sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
        if(this->sock == -1)
        {
            cout << "Socket error!" << endl;
            exit(1);
        }
    
        memset(&(this->server), 0, sizeof(this->server));
        this->server.sin_family = AF_INET;
        this->server.sin_port = htons(port);
        this->server.sin_addr.s_addr = htonl(INADDR_ANY);
    
        if( bind(this->sock, (sockaddr *) &(this->server), this->addressLength) == -1 )
        {
            cout << "Bind socket error!" << endl;
            exit(1);
        }
    
        this->bufferSize = bufferSize;
        this->buffer = new char[bufferSize];
    
        cout << "Socket created!" << endl;
    }
    
    int Connection::receiveData()
    {
        int returned;
        fflush(stdout);
    
        if( (returned =recvfrom(this->sock, this->buffer, this->bufferSize, 0, (sockaddr *) &(this->other), &this->addressLength)) == -1)
        {
            cout << "Receiving error!" << endl;
            exit(1);
        }
    
        return returned;
    }
    

    当我打电话给receiveData()时没有发生任何事情 - 我认为程序会等待一些udp包(我不知道为什么他没有收到它)。

  4. 有人能解释一下udp服务器和客户端程序之间的区别吗?

  5. 非常感谢。

1 个答案:

答案 0 :(得分:1)

调用recvfrom()时,您将向其传递指向this->addressLength的指针。在输入时,长度需要是地址缓冲区的完整大小。输出时,返回实际的地址长度。因此recvfrom()可能会改变您的addressLength成员,从而影响后续代码。改为使用局部变量:

int Connection::receiveData()
{
    int returned;
    int addrlen = sizeof(this->other);

    fflush(stdout);

    returned = recvfrom(this->sock, this->buffer, this->bufferSize, 0, (sockaddr *) &(this->other), &addrLen);
    if( returned == -1 )
    {
        cout << "Receiving error!" << endl;
        exit(1);
    }

    return returned;
}

或者,改为使用单独的成员变量:

class Connection
{
private:
    char * buffer;
    int bufferSize;
    int sock;
    struct sockaddr_in server;
    struct sockaddr_in other;
    socklen_t serverAddressLength;
    socklen_t otherAddressLength;

public:
    Connection(string address, int port, int bufferSize);
    ~Connection();
    int reciveData();
};

Connection::Connection(string address, int port, int bufferSize)
{
    this->serverAddressLength = sizeof(this->server);

    this->sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if(this->sock == -1)
    {
        cout << "Socket error!" << endl;
        exit(1);
    }

    if(this->sock == -1)
    {
        cout << "Socket error!" << endl;
        exit(1);
    }

    memset(&(this->server), 0, sizeof(this->server));
    this->server.sin_family = AF_INET;
    this->server.sin_port = htons(port);
    this->server.sin_addr.s_addr = htonl(INADDR_ANY);

    if( bind(this->sock, (sockaddr *) &(this->server), this->serverAddressLength) == -1 )
    {
        cout << "Bind socket error!" << endl;
        exit(1);
    }

    this->bufferSize = bufferSize;
    this->buffer = new char[bufferSize];

    cout << "Socket created!" << endl;
}

int Connection::receiveData()
{
    int returned;
    fflush(stdout);

    this->otherAddressLength = sizeof(this->other);
    if( (returned =recvfrom(this->sock, this->buffer, this->bufferSize, 0, (sockaddr *) &(this->other), &this->otherAddressLength)) == -1)
    {
        cout << "Receiving error!" << endl;
        exit(1);
    }

    return returned;
}