Listening to broadcasts when the server and client are on the same machine

After spending most of the night, I could not get it to work. That's what I'm doing:

  • This is a network game that is hosted by one participant and other players join it. The owner himself also acts as a player.
  • The user clicks the Host button to start advertising in UDP.
  • Other users can view a list of all available hosts and select one of them.

As a starter, I downloaded the NetworkHelper library to learn how networking works in UWP. This library provides the UDPManagerand classes UDPParticipantthat I used for classes Hostand Playerrespectively.

The library also includes a small trial version of the question / answer. The only major change between my architecture and the game is that one of my players must act as a Host and a Player, while a sample game can act as a host or player at a time. Therefore, unlike them, I need to have two objects at the same time DatagramSocket.

Everything works fine, except (grrr ... why, except always around the corner) that a client running on the main machine cannot listen to advertising messages. DatagramSockettells me that I cannot have multiple use methods for the same network address (protocol / host / port). If I use different ports for the server and client ( AdvertiserPortand ListenerPortbelow), there are no exceptions, but the client never receives an advertising message.

( ):

AdvertiserSocket = new DatagramSocket();
AdvertiserSocket.MessageReceived += MessageToConnectReceivedFromParticipantAsync;
await AdvertiserSocket.BindServiceNameAsync(AdvertiserPort);

_timer = new Timer(async state => await SendMessageAsync(), null, 0, AdvertiserInterval);

:

private async Task SendMessageAsync()
{
  Stream outStream = (await AdvertiserSocket.GetOutputStreamAsync(AdvertiserGroupHost, AdvertiserPort)).AsStreamForWrite();

  using (var writer = new StreamWriter(outStream))
  {
    await writer.WriteLineAsync(AdvertiserMessage);
    await writer.FlushAsync();
  }
}

:

_listenerSocket = new DatagramSocket();
_listenerSocket.MessageReceived += AdvertisementMessageReceivedFromManagerAsync;
await _listenerSocket.BindServiceNameAsync(ListenerPort);
_listenerSocket.JoinMulticastGroup(ListenerGroupHost);

? , UDP ? , ?

237.1.3.37 UDP_MULTICAST_IP. ? - , 255.255.255.255 . ?

+4
1

MS. , DatagramSocket. , . . SO post ( ).

, :

  • /, Control.MulticastOnly true .
  • BindServiceNameAsync(), .
  • 237.1.3.37 . 255.255.255.255 .

, - .

+1

Source: https://habr.com/ru/post/1655801/


All Articles