Click here to Skip to main content
14,265,815 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello, i have a simple udp application that i'm using on two different computers to send messages to each other, the problem is that when the first message is read with recvfrom, the same message is read again, and again, and again, and wont stop, looks like the message wasn't removed from the socket buffer and still there after reading it, i'm not peeking the message, here is how i'm using the recvfrom:

struct sockaddr_in o_addr;
uint32 read;
int l = sizeof(struct sockaddr);

if ((read = recvfrom(sock_fd, data, length, 0, (struct sockaddr*)&o_addr, &l)) == SOCKET_ERROR){
    throw std::runtime_error(Exception::getSystemError());

if (read == 0){
    //Connection closed
    throw std::runtime_error(SocketException::END_CONNECTION);


data[read] = 0;

return read;

can anyone help me here? thanks for attention.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

UDP uses a shared message pool , therefore once a message was sent out it is available to all clients (unless specified port is connected to a peer) and stays intact until next datagram

you would better implement a (from & to ) mechanism to check whether message was sent to you and reply if it was sent to you , so you would not get same message again

for example



UDP is not ideal for this type of communication , it is useful for signaling or streaming raw data such as media streaming
you should consider TCP if applicable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100