Answer the question
In order to leave comments, you need to log in
How to broadcast messages over UDP using Socket?
Good day to all!
The task, it seems to me, is simply incredibly simple, but I didn’t read something somewhere, apparently.
I create a USP server
Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
public void Send(byte[] data) {
socket.Send( data , data.Length, SocketFlags.Broadcast);
}
SocketException: Предпринятая операция не поддерживается для выбранного типа объекта.
Answer the question
In order to leave comments, you need to log in
He asked, he answered.
It turns out that it was necessary to read the documentation a little more carefully, although everything is not so obvious there.
Quote from Microsoft website:
If you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. If you want to send data to a broadcast address, you must first call the SetSocketOption method and set the socket option to SocketOptionName.Broadcast . In addition, you must ensure that the number of bytes sent does not exceed the maximum packet size for the underlying service provider. If so, the datagram will not be sent and SendTo will throw a SocketException .
socket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Broadcast, true );
IPEndPoint targetEndPoint = new IPEndPoint( IPAddress.Broadcast, 27000 );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question