Home > Software engineering >  UDP broadcast program can send the information to the different ports on a host of the client
UDP broadcast program can send the information to the different ports on a host of the client

Time:09-18

I have a peer-to-peer (P2P) version of the UDP broadcast program, messages can be sent to the other host to run this program in the local area network (LAN), but I want to test in the unit, sends a message to the host on several different copies (each copy of the binding of different port), can be achieved, the attached code is as follows:
 void CUDPDlg: : OnBroad () 
{
The UpdateData (true);//the input data to control by the control variables
Struct sockaddr_in toaddr;//store the structure of the target address variable

Memset ((void *) & amp; Toaddr, 0, sizeof (addr));//toaddr each field values all s
Toaddr. Sin_family=AF_INET;//specifies the protocol family to aAF_INET
Toaddr. Sin_addr. S_addr=INADDR_BROADCAST;//specify the sending address is broadcast address
Toaddr. Sin_port=htons (port 1);
M_List. AddString (" I said: "+ m_Edit);//will send content added to the ListBox control
Sendto (brdsock, m_Edit, m_Edit GetLength (), 0, (sockaddr *) & amp; Toaddr, sizeof (toaddr));//send data to the communication to end

}

Void CUDPDlg: : OnSend ()
{
The UpdateData (true);//the input data to control by the control variables
Struct sockaddr_in toaddr;//store the structure of the target address variable
DWORD bwaddr;//store destination IP address of the variable

M_IPCtrl. GetAddress (bwaddr);//by IP address space variable m_IP get target IP address

Memset ((void *) & amp; Toaddr, 0, sizeof (addr));//toaddr each field values all s
Toaddr. Sin_family=AF_INET;//specifies the protocol family to aAF_INET
Toaddr. Sin_addr. S_addr=htonl (bwaddr);
Toaddr. Sin_port=htons (port);
M_List. AddString (" native: & gt;" + m_Edit);//will send content added to the ListBox control
Sendto (sock, m_Edit, m_Edit GetLength (), 0, (sockaddr *) & amp; Toaddr, sizeof (toaddr));//send data to the communication to end
}

Afx_msg LRESULT CUDPDlg: : OnRecvmessage (WPARAM WPARAM, LPARAM LPARAM)
{
Char recvBuffer [1000].
Cstrings STR.
Int len=sizeof (fromaddr); The last parameter in//recvfrom function, must be initialise
Int size=recvfrom (sock, recvBuffer, sizeof (recvBuffer), 0, (sockaddr *) & amp; Fromaddr, & amp; Len);//receiving data
If (size> 0)
{
RecvBuffer [size]='\ 0';//add string terminator at the end of the string '\ 0'
STR. The Format (" messages from % s: % d: % s ", inet_ntoa (fromaddr. Sin_addr), ntohs (fromaddr. Sin_port), recvBuffer);
M_List. AddString (STR);//added to the ListBox control
}
return 0;
}

Void CUDPDlg: : OnCreate ()
{
If ((the sock=socket (AF_INET, SOCK_DGRAM, 0)) & lt; 0)
{
MessageBox (" failed to create a socket!" );
//return true;
}
If ((brdsock=socket (AF_INET, SOCK_DGRAM, 0)) & lt; 0)
{
MessageBox (" failed to create a socket!" );
//return true;
}
//to a datagram socket binding address, in order to receive broadcast information
Memset ((void *) & amp; Addr, 0, sizeof (addr));//addr each field values all s
Addr. Sin_family=AF_INET;//specifies the protocol family to AF_INET
Addr. Sin_port=htons (port);//the specified UDP port number
Addr. Sin_addr. S_addr=htonl (INADDR_ANY);//specify the IP address
While (bind the sock, (LPSOCKADDR) & amp; Addr, sizeof (addr))!=0)
{
Port++;
Addr. Sin_port=htons (port);//the specified UDP port number
//MessageBox (" binding failed!" );
//closesocket (sock);
//return true;
}
//set the datagram socket m_DGramSocket radio properties, make it can broadcast information
BOOL yes=TRUE;
Int ret=setsockopt (brdsock, SOL_SOCKET, SO_BROADCAST, (char *) & amp; Yes, sizeof (BOOL));
/* * * for m_DGramSocket registered FD_READ events, events will send WM_RECVMESSAGE news * * */
If (WSAAsyncSelect (sock, m_hWnd, WM_RECVMESSAGE FD_READ)!=0)
{
MessageBox (" socket asynchronous event registration failed!" );
Closesocket (sock);
}


}


 class CUDPDlg: public CDialog 
{
//Construction
Public:
PParent CUDPDlg (CWnd *=NULL);//standard constructor
The SOCKET sock, brdsock;
Struct sockaddr_in addr, fromaddr;
//Dialog Data
//{{AFX_DATA (CUDPDlg)
Enum {IDD=IDD_UDP_DIALOG};
CListBox m_List;
CIPAddressCtrl m_IPCtrl;
Cstrings m_Edit;
Cstrings m_Port;
//}} AFX_DATA

CodePudding user response:

This is not very easy to test, the next udo tools can try

CodePudding user response:

I asked the WinSock UDP programming can sends a message to the host several different copies of the program (each copy of the binding of different port), how to write program code,

CodePudding user response:

UDP broadcast programming can sends a message to the host on several different copies of the program
  • Related