Home > Software engineering >  (MFC network programming) TCP client add 2 UDP port _ receiving data has a problem
(MFC network programming) TCP client add 2 UDP port _ receiving data has a problem

Time:11-20

Recent want to use MFC to write a web application,
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 on

CodePudding 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 interface

You 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:

reference 2 floor kyosuke kanou response: 1989
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 interface

You should according to which the socket has data to judge if wParam is, won't, just three messages defined, each socket has a message, not chaos

Tried to UDP1 alone define a message, or a problem

CodePudding user response:

Receive code no problem, see whether to send the got mixed up on the socket

CodePudding user response:

WM_SOCKET WM_SOCKET + 1 + 2 don't multiple public a message id

CodePudding user response:

reference 5 floor smwhotjay reply:
WM_SOCKET WM_SOCKET + 1 + 2 don't multiple public a message id

It's no use, I added a message to UDP1 alone, one problem

CodePudding user response:

Send me the code, I look for you
  • Related