Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problem: UDP broadcast senden und empfangen

Empfohlene Antworten

Veröffentlicht

Hallo,

folgendes Problem:

Ich möchte den unter Finding a Lantronix Device on a Subnet gezeigten Java-Ansatz mittels Visual C++ realisieren:

1) Ich erstelle einen Socket der Port 30718 abhört

2) Ich sende einen Broadcast ins Netz für Port 30718 mit meiner Anfrage

3) Ich warte auf Antworten

Hosts die den verwendeten Port abhören schicken mir auf meine gesendete Nachricht eine UDP Antwort an den Port 30718.

In einem Netzwerksniffer sehe ich das mein Broadcast funktioniert. Weiterhin kann ich auch die Antwort eines Hosts sehen.

Nur leider kann ich diese nicht empfangen. In der while-Schleife zum Empfangen erhalte ich nur dreimal meine eigene gesendete Anfrage.

Woran liegt das? Habe ich etwas falsch verstanden?

Vielen Dank für eine Antwort.

Gruß,

Tom

Im Beispielcode habe ich die Fehlerprüfungen auf Grund der besseren Übersicht herausgenommen.


  WSADATA wsaData;

  WSAStartup(0x0101, &wsaData);


  // Create the socket to listen for broadcast response

  SOCKET sockRecv;

  SOCKADDR_IN sinRecv;


  sockRecv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);


  //Fill out the information needed to initialize a socket

  sinRecv.sin_family = AF_INET; // address family Internet

  sinRecv.sin_port = htons (30718); //Port to connect on

  sinRecv.sin_addr.s_addr = htonl (INADDR_ANY); 


  bind(sockRecv, (LPSOCKADDR)&sinRecv, sizeof(sinRecv));




  // Create the socket for broadcasting

  SOCKET sock;

  SOCKADDR_IN sin;


  sin.sin_family = AF_INET;

  sin.sin_addr.s_addr = INADDR_BROADCAST;

  sin.sin_port = htons (30718); //Port to connect on


  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);



  int i = 1;

  setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&i, 1);


  connect(sock, (SOCKADDR *)&sin, sizeof(sin));



  // send the broadcast

  const int nSendBufferLen = 4;

  BYTE sendBuffer[nSendBufferLen] = { 0x00, 0x00, 0x00, 0xF6};

  long lBytesSend = send(sock, (const char*) sendBuffer, nSendBufferLen, 0);



  int nBytesRecv;

  char recvBuffer[80];

  memset(recvBuffer, 0, sizeof(recvBuffer)); //Clear the buffer



  // Try to receive data on an extra socket for port 30718 only receives my own broadcast!

  i = 0;

  while(true)

  {

    nBytesRecv = recv(sockRecv, recvBuffer, sizeof(recvBuffer)-1, 0);


    CString str;  

    str.Format(_T("Receive %d, Bytes %d\n"), ++i, nBytesRecv);

    TRACE(str);

  }    



  WSACleanup();

  closesocket(sock);


  closesocket(sockRecv);

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.