添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register or Login before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. Hi.........
I wrote the following code to listen to UDP data sent at specified port by any machine using any application....
I used dialog box application....
m_DataReceived is variable of Edit Control box
m_PortNo is varible to get the port no.
When the connectme button is clicked the following code executes....
Code:
UpdateData( TRUE );
	if( !m_PortNo )
			MessageBox( "Please enter the Port No & then click \"Connect Me\"" );
			return;
WSADATA wsaData;
SOCKET RecvSocket;
sockaddr_in RecvAddr;
int Port = 6791;
char RecvBuf[1024];
int BufLen = 1024;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
char m_SocError[23];
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);
// Create a receiver socket to receive datagrams
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// Bind the socket to any address and the specified port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(m_PortNo);
RecvAddr.sin_addr.s_addr = inet_addr( "255.255.255.255" ); 
int BindCheck = bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
if( 0 != BindCheck )
AfxMessageBox("SocketBind");
if(recvfrom(RecvSocket,RecvBuf,BufLen,0,(SOCKADDR *)&SenderAddr,&SenderAddrSize)== SOCKET_ERROR) 
	MessageBox("ERROR");
	m_DataReceived = RecvBuf;
	UpdateData(FALSE);
closesocket(RecvSocket);
WSACleanup();
exit(1) ;
However though binding is successful I get the Error 10022 : WSAEINVAL
That means the parameter in the recvfrom() are invalid
Please suggest me How to remove this error? what changes to make in the recvfrom function
Please help me! I came to realise that socket binding is failed!!!
By mistake I used Bindchek == 0; instead of BindChek != 0 in the if loop....
Now the question is why binding is failed??? char RecvBuf[500]; int BufLen = 501; int SenderAddrSize = sizeof(SenderAddr); CString MultiLine = _T("\r\n"); // Initialize Winsock int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) MessageBox( "WSAStartup failed" ); return; // Create a receiver socket to receive datagrams RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Bind the socket to any address and the specified port. RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons( Port ); RecvAddr.sin_addr.s_addr = htonl( INADDR_ANY ); int BindCheck = bind( RecvSocket, ( SOCKADDR * ) &RecvAddr, sizeof( RecvAddr ) ); if( 0 != BindCheck ) wsprintf( m_SocError, "GetSockOpt failed to get SO_BROADCAST:%d", GetLastError()); MessageBox( "SocketBind Failed" ); return; int RecvChek = recvfrom( RecvSocket, RecvBuf, BufLen, 0, (SOCKADDR *)&SenderAddr, &SenderAddrSize ); if( RecvChek == SOCKET_ERROR ) m_DataReceived = MultiLine + "ERROR in Receiving" ; UpdateData( FALSE ); m_DataReceived = ""; RecvBuf[RecvChek] = '\0'; m_DataReceived = RecvBuf; UpdateData( FALSE ); //goto AGAIN; /*closesocket(RecvSocket); WSACleanup(); exit(1) ;*/ This code just works for one event...I mean accept packet once only.....
How to make it run for long.....
If I use while loop....it does not show any problem but the dialog box doesnot get closed even if I cilck on close button on extreme upper right corner.
I have to close it through task manager.
Please tell me how to overcome this problem Very strange...
Anyway, since I don't use UDP (our and many many other sysadmins don't allow broadcasting, and without broadcasting I don't see any advantage of using UDP) I cannot help you more.
However, you might consider change the socket type to SOCK_STREAM: * The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform. * Porting from Android to Windows 8: The Real Story
Do you have an Android application? How hard would it really be to port to Windows 8? * Guide to Porting Android Applications to Windows 8
If you've already built for Android, learn what do you really need to know to port your application to Windows Phone 8. * HTML5 Development Center
Our portal for articles, videos, and news on HTML5, CSS3, and JavaScript * Windows App Gallery
See the Windows 8.x apps we've spotlighted or submit your own app to the gallery! Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.