erin c
erin c

Reputation: 1345

poll() with non blocking udp socket

I am trying to make a c++ program work which is written by somebody else. I am having hard time understanding it. I am not even %100 sure that we can use poll() with a UDP socket but the code I am refactoring, is using poll() to read from udp socket as follows:

fd.fd = m_bsocket;
fd.events = POLLIN;


iPollResult = poll(&fd, 1, iTimeout);

if(iPollResult > 0)
{
    int iReceivedByteCount = recv(m_bsocket, p_pBuffer, p_iBufferSize, 0);
    if(iReceivedByteCount > 0)
    {
        *p_pReadSize = iReceivedByteCount;
    }
    else
    {
        eReturnValue = UDP_READ_ERROR;
    }
}

return eReturnValue;

I tried sending udp packets to this program using command line:

echo "123" | nc -u 127.0.0.1 25

It looks like poll() always times out and returns 0, therefore I can not read anything.

I also wrote a small c# program that sends udp datagram, but I can not receive the message. I am wondering what I am doing wrong...

Upvotes: 1

Views: 9827

Answers (2)

huysentruitw
huysentruitw

Reputation: 28151

You don't need to call connect() as UDP is connectionless. You need to bind() the socket to the IP of the interface you are listening on or 0.0.0.0 (INADDR_ANY) for all interfaces. And when sending to a destination, use sendto().

For completeness: if you call connect() on a UDP socket, you are just setting a default destination for the send() function (then you can use send instead of sendto).

If you want to receive data, you always have to bind() the socket to the interface, or all interfaces. Beware that you will have to verify the source address from the messages you are receiving. So you might want to filter the sender by using recvfrom() and checking the source address.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409442

While UDP sockets can be used to connect to another host, they are mostly used "connectionless". Reading your question and comments it makes no sense that you have a connected socket. Instead it should be connectionless as suggested by WouterH in his comment.

int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

struct sockaddr_in sin = { 0 };
sin.sin_family = AF_INET;
sin.sin_port = htons(25);
sin.sin_addr.s_addr = INADDR_ANY;
bind(sockfd, (struct sockaddr *) &sin, sizeof(sin));

// Make socket non-blocking if needed

With the above code, whenever someone sends UDP packets to port 25 on any address of your host, your socket will intercept it. Use e.g. poll or select to know when data is available.

Upvotes: 2

Related Questions