UDP Broadcast - Empfange keine Antwort

florian0

Grünschnabel
Hiho,

ich bin inzwischen etwas verzweifelt. Ich habe anscheinden immer die Probleme die sonst keiner hat.

Um mit Sockets in .NET etwas vertrauter zu werden, habe ich mir ein kleines Projekt gesucht.
Meine Wahl viel auf einen LAN Server Browser für Battlefield 2 (Ein Programm das alle Battlefield 2 Server im lokalen Netzwerk auflistet)

Nach ein bischen Pakete fishen in Wireshark, hab ich die Client-Server-Kommunikation relativ gut im Griff.

Alles beginnt mit einem Broadcast auf Port 29900.
Auf diesen Broadcast antworten alle BF2-Server im Netzwerk.

Beispiel aus Wireshark:
Code:
Nr	Time		SRC-IP		DST-IP			Protocol	Information
5	0.722120	7.8.34.153	255.255.255.255		UDP	Source port: 55858  Destination port: 29900
6	0.784682	7.3.254.47	7.8.34.153		UDP	Source port: 29900  Destination port: 55858
9	0.804871	7.7.149.23	7.8.34.153		UDP	Source port: 29900  Destination port: 55858
10	0.819204	7.1.227.34	7.8.34.153		UDP	Source port: 29900  Destination port: 55858
11	0.822317	7.0.36.68	7.8.34.153		UDP	Source port: 29900  Destination port: 55858
12	0.824445	7.8.24.206	7.8.34.153		UDP	Source port: 29900  Destination port: 55858
13	0.834174	7.6.107.2	7.8.34.153		UDP	Source port: 29900  Destination port: 55858
14	0.932352	7.5.204.23	7.8.34.153		UDP	Source port: 29900  Destination port: 55858
Wundert euch nicht über die IPs. Die kommen aus dem Programm Tunngle (gute Hamachi VPN alternative).

Nun zum Problem:
Ich sende das erste Broadcast Paket.
Die Server antworten (sichtbar in Wireshark), aber im Programm kommt nichts an.

Aktueller Code
Code:
private void GetServers(int port)
        {

            // Setup a Broadcast Socket
            
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sock.Bind(new IPEndPoint(IPAddress.Parse("7.8.34.153"), 55858));
            //sock.Bind(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345));


            sock.Connect(IPAddress.Broadcast, port);

            // We need a Timeout
            sock.ReceiveTimeout = 1000;

            //Socket ready !

            // #########################################
            // First Packet
            // Send to all Interfaces (Broadcast)
            // FE FD 02 00 00 00 00 00 <- Any Servers around ******

            byte[] firstpacket = { 0xFE, 0xFD, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            sock.Send(firstpacket);

            // #########################################
            // Second Packet
            // HERE! Im a Server!
            // 05 00 00 00 00 00 00

            // There may be multiple Servers on the same Port
            // Just wait a bit to collect them all

            EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 55858);
            
            while (true)
            {
                try
                {
                    
                    byte[] secondpacket = new byte[7];
                    // Wait until a packet is received
                    
                    sock.ReceiveFrom(secondpacket, ref remoteEndPoint);
                    //sock.Receive(secondpacket);
                    // Check if this answer is valid

                    if (secondpacket[0] == 0x05)
                    {
                        // Open a new Thread for each valid answer
                        //Thread tmp = new Thread(new ParameterizedThreadStart(GetServerInfo));
                        // tmp.Start(sock.RemoteEndPoint, port);
                        MessageBox.Show(sock.RemoteEndPoint.ToString());
                    }
                    // Else : Just drop this answer, it might be useless

                }
                catch (SocketException se)
                {
                    // Maybe Timeout
                    // Whatever it is -> break the loop
                    MessageBox.Show(se.Message);
                    break;
                }
            }
            
            sock.Close();

        }

Ich habe es mit Receive und ReceiveFrom versucht. Erfolglos.
Wenn jemand eine Idee hat ... nur her damit.

Gruß
florian0
 
Zurück