How to distinguish the broad packets or normal packets?
-
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 !
-
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 !
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.
-
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 !
-
Thanks !