Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to distinguish the broad packets or normal packets?

How to distinguish the broad packets or normal packets?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wangningyu
    wrote on last edited by
    #1

    The following is a fragment of code:

    sockaddr\_in		from,a;
    WSADATA			wsdata;
    BOOL			optval = TRUE;
    u\_short			uPort  = m\_nPort;
    
    if(WSAStartup(0x0202,&wsdata) != 0)
    {
    	CString strErr;
    	strErr.Format(\_T("WSAStartup Init failed! Error code:%d"),WSAGetLastError());
    	AddNote(strErr);
    	\_endthreadex(0);
    	return FALSE;
    }
    
    a.sin\_family		= AF\_INET;
    a.sin\_addr.s\_addr	= 0;
    a.sin\_port			= htons(uPort);
    
    from.sin\_family			= AF\_INET;
    from.sin\_addr.s\_addr	= INADDR\_BROADCAST;
    from.sin\_port			= htons(uPort);
    
    int fromlength=sizeof(SOCKADDR);
    pThread->m\_socket = socket(AF\_INET,SOCK\_DGRAM,0);
    setsockopt(pThread->m\_socket,SOL\_SOCKET,SO\_BROADCAST,(char FAR \*)&optval,sizeof(optval));
    bind(pThread->m\_socket,(sockaddr \*)&a,sizeof(sockaddr\_in));
    
    char		buf\[BUF\_MAX\_LEN\] = {0x00};
    int			nLen = 0;
    char		\*pszIPClient = NULL;
    u\_short		uPortClient  = 0;
    THREADBOARD	\*pThreadBoard = NULL;
    CString		strInit = \_T("");
    
    strInit.Format(\_T("Port: %d Now waiting for clients..."),uPort);
    AddNote(strInit);
    
    while(true)
    {
    	nLen = recvfrom(pThread->m\_socket,buf,BUF\_MAX\_LEN,0,(struct sockaddr FAR \*)&from,(int FAR \*)&fromlength);
    	if(nLen > 0)
    	{
    		// How to distinguish is send broadcast , or normal send ?
    	}
    }
    

    Thanks for your reply !

    P S 2 Replies Last reply
    0
    • W wangningyu

      The following is a fragment of code:

      sockaddr\_in		from,a;
      WSADATA			wsdata;
      BOOL			optval = TRUE;
      u\_short			uPort  = m\_nPort;
      
      if(WSAStartup(0x0202,&wsdata) != 0)
      {
      	CString strErr;
      	strErr.Format(\_T("WSAStartup Init failed! Error code:%d"),WSAGetLastError());
      	AddNote(strErr);
      	\_endthreadex(0);
      	return FALSE;
      }
      
      a.sin\_family		= AF\_INET;
      a.sin\_addr.s\_addr	= 0;
      a.sin\_port			= htons(uPort);
      
      from.sin\_family			= AF\_INET;
      from.sin\_addr.s\_addr	= INADDR\_BROADCAST;
      from.sin\_port			= htons(uPort);
      
      int fromlength=sizeof(SOCKADDR);
      pThread->m\_socket = socket(AF\_INET,SOCK\_DGRAM,0);
      setsockopt(pThread->m\_socket,SOL\_SOCKET,SO\_BROADCAST,(char FAR \*)&optval,sizeof(optval));
      bind(pThread->m\_socket,(sockaddr \*)&a,sizeof(sockaddr\_in));
      
      char		buf\[BUF\_MAX\_LEN\] = {0x00};
      int			nLen = 0;
      char		\*pszIPClient = NULL;
      u\_short		uPortClient  = 0;
      THREADBOARD	\*pThreadBoard = NULL;
      CString		strInit = \_T("");
      
      strInit.Format(\_T("Port: %d Now waiting for clients..."),uPort);
      AddNote(strInit);
      
      while(true)
      {
      	nLen = recvfrom(pThread->m\_socket,buf,BUF\_MAX\_LEN,0,(struct sockaddr FAR \*)&from,(int FAR \*)&fromlength);
      	if(nLen > 0)
      	{
      		// How to distinguish is send broadcast , or normal send ?
      	}
      }
      

      Thanks for your reply !

      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      You can do this by using raw sockets but I was raw-socketing only on linux, dont know how it works on windows (if it works at all...). I searched for an example and found this discussion: http://bytes.com/topic/c/answers/900770-how-determine-origin-incoming-message-uincast-multicast-broadcast[^] One of the answers contains source code, read along that thread. Note: I havent tested their code. EDIT: The trick is simple: the sendto address is in the header of the datagram you received, but your recvfrom returns just the payload from that and the address of the sender. You need a method to reach other fields of the received UDP packet header, one way is using raw sockets.

      1 Reply Last reply
      0
      • W wangningyu

        The following is a fragment of code:

        sockaddr\_in		from,a;
        WSADATA			wsdata;
        BOOL			optval = TRUE;
        u\_short			uPort  = m\_nPort;
        
        if(WSAStartup(0x0202,&wsdata) != 0)
        {
        	CString strErr;
        	strErr.Format(\_T("WSAStartup Init failed! Error code:%d"),WSAGetLastError());
        	AddNote(strErr);
        	\_endthreadex(0);
        	return FALSE;
        }
        
        a.sin\_family		= AF\_INET;
        a.sin\_addr.s\_addr	= 0;
        a.sin\_port			= htons(uPort);
        
        from.sin\_family			= AF\_INET;
        from.sin\_addr.s\_addr	= INADDR\_BROADCAST;
        from.sin\_port			= htons(uPort);
        
        int fromlength=sizeof(SOCKADDR);
        pThread->m\_socket = socket(AF\_INET,SOCK\_DGRAM,0);
        setsockopt(pThread->m\_socket,SOL\_SOCKET,SO\_BROADCAST,(char FAR \*)&optval,sizeof(optval));
        bind(pThread->m\_socket,(sockaddr \*)&a,sizeof(sockaddr\_in));
        
        char		buf\[BUF\_MAX\_LEN\] = {0x00};
        int			nLen = 0;
        char		\*pszIPClient = NULL;
        u\_short		uPortClient  = 0;
        THREADBOARD	\*pThreadBoard = NULL;
        CString		strInit = \_T("");
        
        strInit.Format(\_T("Port: %d Now waiting for clients..."),uPort);
        AddNote(strInit);
        
        while(true)
        {
        	nLen = recvfrom(pThread->m\_socket,buf,BUF\_MAX\_LEN,0,(struct sockaddr FAR \*)&from,(int FAR \*)&fromlength);
        	if(nLen > 0)
        	{
        		// How to distinguish is send broadcast , or normal send ?
        	}
        }
        

        Thanks for your reply !

        S Offline
        S Offline
        Software_Developer
        wrote on last edited by
        #3

        First [this]. Then, Packet Filtering , found [here]. Also [this].

        W 1 Reply Last reply
        0
        • S Software_Developer

          First [this]. Then, Packet Filtering , found [here]. Also [this].

          W Offline
          W Offline
          wangningyu
          wrote on last edited by
          #4

          Thanks !

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups