Reputation: 3508
I have a program that recevice message in udp in visual studio.
sometime I miss a message.I want to use select to see udp buffer used size if udp rec buffer is full.
How I can use select for this purpose?
where can I see a example?
Upvotes: 2
Views: 2147
Reputation: 84169
Post-fact discovery that your receive buffer is full will not do you any good - packets have already been dropped. Even more - you need to set your buffer sizes before connect()
or bind()
, they will not change after that.
What you should do is pre-set socket receive buffer to some large value that would accommodate your traffic spikes, and try to process your network input faster. That usually means doing select()
on non-blocking sockets (or even better - some advanced API like Linux epoll(7)
in edge-triggered mode), and draining socket input until you get EWOULDBLOCK
.
You cannot discover that you missed a UDP packet by using select()
or any other socket API. This has to be done in the application-level protocol, i.e. one layer up from the transport. Common practice is including sequence numbers in application message header.
Upvotes: 4
Reputation: 409196
You can use getsockopt
to get socket options, including receive buffer size. Use setsockopt
to set the size.
Example of getting the size:
int size;
getsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF, &size);
std::cout << "Buffer size is currently " << size << " bytes\n";
Upvotes: 3
Reputation: 87416
I think one of the main properties of UDP (as opposed to TCP) is that you will sometimes lose messages. You have to design your protocol to account for that. I'm not an expert in UDP, but I don't understand how seeing the size of the buffer will help you, and I don't understand why you want to use select
to do it.
Upvotes: 2