Implementation under the same IP TCP client and 2 UDP port
to send and receive data at the same timeEncountered a problem:
UDP1 received data sometimes ran to the side UDP2, excuse me, is what reason, I posted some code? ( now and two opposite the UDP and TCP server port, a total of three port connection, both sides each data will trigger this problem )
//to establish a SOCKET
Socket_udp=socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Socket_udp2=socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Socket_tcp_client=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);
Addr_tcp_target. Sin_family=AF_INET;
Addr_udp_target. Sin_family=AF_INET;
Addr_udp_target2. Sin_family=AF_INET;
This - & gt; GetDlgItem (IDC_IPADDRESS1) - & gt; GetWindowText (str1);
This - & gt; GetDlgItem (IDC_PORT1_EDIT) - & gt; GetWindowText (str2);
This - & gt; GetDlgItem (IDC_PORT2_EDIT) - & gt; GetWindowText (str3);
This - & gt; GetDlgItem (IDC_PORT3_EDIT) - & gt; GetWindowText (str3);
M_port1=atoi (str2 GetBuffer (1));
M_port2=atoi (str3 GetBuffer (1));
M_port3=atoi (str4 GetBuffer (1));
//TCP
Addr_tcp_target. Sin_port=ntohs (m_port1);//htons (3000);//m_port
Addr_tcp_target. Sin_addr. S_un. S_addr=inet_addr (str1. GetBuffer (1));//m_ip
//udp
Addr_udp_target. Sin_port=ntohs (m_port2);//htons (3001);//m_port
Addr_udp_target. Sin_addr. S_un. S_addr=inet_addr (str1. GetBuffer (1));//m_ip
Addr_udp_target2. Sin_port=ntohs (m_port3);//htons (3002);//m_port
Addr_udp_target2. Sin_addr. S_un. S_addr=inet_addr (str1. GetBuffer (1));//m_ip
//local ip& The port
Gethostname (hostname, sizeof (hostname));
Hn hostent *;
Hn=gethostbyname (hostname);//according to the machine host name to get the machine IP
StrIPAddr=inet_ntoa (* (struct in_addr *) hn - & gt; H_addr_list [0]);//change the IP to string
//TCP
Addr_tcp. Sin_family=AF_INET;
Addr_tcp. Sin_addr. S_un. S_addr=inet_addr (strIPAddr);//the client address
Addr_tcp. Sin_port=htons (3000);//TCP client for port 3000
Bind (socket_tcp_client, (SOCKADDR *) & amp; Addr_tcp, sizeof (SOCKADDR));//client binding
//udp
Addr_udp. Sin_family=AF_INET;
Addr_udp. Sin_addr. S_un. S_addr=inet_addr (strIPAddr);//the client address
Addr_udp. Sin_port=htons (3001);//UDP as the local port 3001
Bind (socket_udp, (SOCKADDR *) & amp; Addr_udp, sizeof (SOCKADDR));//client binding
Addr_udp2. Sin_family=AF_INET;
Addr_udp2. Sin_addr. S_un. S_addr=inet_addr (strIPAddr);//the client address
Addr_udp2. Sin_port=htons (3002);//UDP as the local port 3001
Bind (socket_udp2, (SOCKADDR *) & amp; Addr_udp2, sizeof (SOCKADDR));//client binding
//
If (WSAAsyncSelect (socket_tcp_client, this - & gt; M_hWnd, WM_SOCKET FD_READ))
{
MessageBox (_T (" TCP asynchronous setting error "), _T (" "));
}
If (WSAAsyncSelect (socket_udp, this - & gt; M_hWnd, WM_SOCKET FD_READ))
{
MessageBox (_T (" setup error udp asynchronous "), _T (" "));
}
If (WSAAsyncSelect (socket_udp2, this - & gt; M_hWnd, WM_SOCKET FD_READ))
{
MessageBox (_T (" setup error udp2 asynchronous "), _T (" "));
}
//I customize a message OnSocket (WPARAM WPARAM, LPARAM LPARAM) receive three port data
Ret1=recv (socket_tcp_client buff_tcp, 146000, NULL);
Int n=sizeof (addr_udp_target);
Ret2=recvfrom (socket_udp, buff_udp, 146000, 0, (sockaddr *) & amp; Addr_udp_target, & amp; N);
Int n2=sizeof (addr_udp_target2);
Ret3=recvfrom (socket_udp2, buff_udp2, 146000, 0, (sockaddr *) & amp; Addr_udp_target2, & amp; N2);
CodePudding user response:
Sending and receiving end of the IP address and port output logs, contrast, see is the problem of receiving, sending, or deal with the problem onCodePudding user response:
When a set of interface s a named network event happened, the application window hWnd wMsg will receive the news. The wParam parameter identifies the network event set interfaceYou should according to which the socket has data to judge if wParam is, won't, just three messages defined, each socket corresponding to a message, not chaos
CodePudding user response: