我的目的是编写用UDP协议从网络接收数据的C++程序。操作系统-linux。我没什么问题。
我想确保我可以使用Linux C库来编写C++程序。
在任何地方都有一个完整的教程,一步一步地介绍如何在Linux中使用套接字编程吗?(我知道在网络上有很多教程,但我正在寻找一些东西来帮助我从INTERNET接收udp数据,而不是从同一设备上的另一个程序。)。
我在终端(使用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包(我不知道他为什么没有收到它)。有人能解释一下udp服务器和客户端程序的区别吗?
谢谢。
最佳答案
调用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;
}
关于c++ - 无法使用udp协议(protocol)从网络接收数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17733794/