I'm trying to process some UDP packages. Opening sockets and so on works well, I do get packages and I get the source IP and port.The port is set in m_port and the IP in m_ipfilter. However recvfrom always returns 1. My code is basically the example code and according to wireshark there are packages with a length of 998 incoming. Does somebody know what I'm doing wrong? The buffer is at the moment 2k long.
struct sockaddr_in server, from;
int sock, flen = sizeof(from);
char buf[BUFLEN];
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
std::cerr << "Couldn't open socket: " << strerror(errno) << ", errno: " << errno << std::endl;
return;
}
memset((char *)&server, 0, sizeof(server)); //zero the structure
server.sin_family = AF_INET;
server.sin_port = htons(m_port);
server.sin_addr.s_addr = htonl(m_ipfilter); //zero is recive on any adrress, INADDR_ANY=0
if (bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1)
{
std::cerr << "Couldn't bind socket on port: " << m_port << " : " << strerror(errno) << ", errno: " << errno << std::endl;
return;
}
while (1)
{
ssize_t recv_len = 0;
if (recv_len = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&from, (socklen_t *)&flen) != -1) //there is data
{
processOutput(buf, recv_len, from, (socklen_t)flen);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…