Re: System.Net.Sockets.SocketException -UDP

Tech-Archive recommends: Speed Up your PC by fixing your registry



Torben Schulz wrote:
Hallo Thomas,


gib beim UdpClient-Konstruktor die (Empfangs-) Port-Nummer an, etwas wie:

 // hier zB 11000 als lokalen Port binden
 UdpClient udpClient = new UdpClient( 11000,
AddressFamily.InterNetwork );
 IPEndPoint remoteIPEndPoint = new IPEndPoint( IPAddress.Any, 0 );
 byte[] receivedBytes = udpClient.Receive( ref remoteIPEndPoint );



Das hat in meinem Falle auch nicht so recht geholfen, auch wenn der Fehler woanders liegen mag. Ich möchte folgendes erreichen: Wir haben im Netz mehrere Gateways, die auf Port 19000 auf ein UDP lauschen und nach Erhalt ein Protokoll mit z.B. Speicherdaten zurücksenden. Die IPs der Gateways sowie deren Anzahl kann variieren. Mein Ansatz war, den Request erst einmal pauschal an alle Teilnehmer im Netz zu schicken:

Byte[] sendBytes = Encoding.ASCII.GetBytes("SEARCH_REQUEST\r\n\r\n");
UdpClient udpClient = new UdpClient();
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Broadcast , 0);
udpClient.Send(sendBytes, sendBytes.Length, remoteIpEndPoint);

Du möchtest also Broadcasts senden. Dafür muss der Socket vorbereitet werden, ungefähr so:

socket.SetSocketOption(SocketOptionLevel.Socket,
    SocketOptionName.Broadcast, 1);

Da jedoch UdpClient keinen Zugriff auf den Socket anbietet,
ist er schlicht nicht brauchbar für das was du vorhast.
Du musst auf Sockets zurückgreifen.

ciao
Robert
.



Relevant Pages

  • Re: UDP broadcast
    ... Ich hab jetzt fast was ich wollte (siehe Code unten). ... Allerdings ist der UDPClient im blocking mode, ... udpClient.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint); ... Next by Date: ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: UDP broadcast
    ... UdpClient udpClient = new UdpClient; ... // set address to broadcast address and port to 8001 ... udpClient.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint); ... > There is a udp client available in the .NET framework but how can I use it ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: UDP broadcast
    ... > receiving data. ... > UdpClient udpClient = new UdpClient; ... > udpClient.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint); ... > // Blocks until a message returns on this socket from a remote host. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: UDP broadcast
    ... > receiving data. ... > UdpClient udpClient = new UdpClient; ... > udpClient.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint); ... > // Blocks until a message returns on this socket from a remote host. ...
    (microsoft.public.dotnet.languages.csharp)