Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
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
    this->close();
    throw std::runtime_error(SocketException::END_CONNECTION);
}

addr.fromOld(o_addr);

data[read] = 0;

return read;


can anyone help me here? thanks for attention.
Posted

1 solution

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

server@client-x:dosomething
client-x@server:ok


client-x@server:createfile(xyz.txt)
@server@client-x:filecreated(xyz.txt)
client-x@server:writefile(xyz.txt,somebytes)
@server@client-x:byteswritten(xyz.txt)


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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900