从udp套接字缓冲区中删除消息 [英] Remove message from udp socket buffer

查看:125
本文介绍了从udp套接字缓冲区中删除消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个简单的udp应用程序,我在两台不同的计算机上使用它们互相发送消息,问题是当用recvfrom读取第一条消息时,会再次读取相同的消息,并且再次,并且不会停止,看起来消息没有从套接字缓冲区中移除,并且在阅读之后仍然存在,我不是在偷看消息,这里是我如何使用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 ){
// 连接已关闭
this - >关闭();
throw std :: runtime_error(SocketException :: END_CONNECTION);
}

addr.fromOld(o_addr);

data [read] = 0 ;

return read;





有人可以帮我吗?感谢关注。

解决方案

UDP使用共享消息池,因此一旦发送消息,它就可供所有客户端使用(除非指定的端口连接到对等端口)并保持完整,直到下一个数据报



你最好实现一个(来自& to)机制来检查消息是否发送给你并回复是否发送给你你,所以你不会再收到同样的消息



例如



 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不适合这种类型的通信,它是对于信号传输或流式传输原始数据(如媒体流)非常有用

如果应用,您应该考虑使用TCP ë


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
    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.

解决方案

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


这篇关于从udp套接字缓冲区中删除消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆