Home > other >  The Windows sockets udp question
The Windows sockets udp question

Time:10-14

Udp socket communication, whether must first open the receiver recvfrom function, the call to the sender sendto? First open the sender receiver wouldn't receive it,
The fourth parameter and recvfrom function source address (from) is the output source address or need to specify the source address, instructions from the source address receives packets/?
Today we had a day, fill the from address wouldn't return 1,


 
//receiver
Int waitsec (int SEC)
{
While (sec> 0 {
Cout & lt; The SEC -;
Sleep (1000);
}
Return 0;
}

Int main (int arg c, char * argv [])
{
Char buf [10]={0};
Int ret.
Int len.
WSADATA data;
WSAStartup (MAKEWORD (2, 2), & amp; The data);
The SOCKET sock;
The sock=socket (AF_INET, SOCK_DGRAM, 0);
Cout & lt; SOCKADDR_IN locaddr;
SOCKADDR_IN sendaddr={0};
Len=sizeof (SOCKADDR_IN);
Locaddr. Sin_port=htons (4000);
Locaddr. Sin_family=AF_INET;
Locaddr. Sin_addr. S_un. S_addr=inet_addr (" 192.168.4.35 ");
Sendaddr. Sin_port=htons (4000);
Sendaddr. Sin_family=AF_INET;
Sendaddr. Sin_addr. S_un. S_addr=inet_addr (" 192.168.4.35 ");
The setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) & amp; Locaddr, TRUE);
Ret=bind (sock, (SOCKADDR *) & amp; Locaddr, sizeof (SOCKADDR_IN));
Cout & lt; While (1) {
Cout & lt; <"The receive star" & lt; Ret=recvfrom (sock, buf, 9, 0, (SOCKADDR *) & amp; Sendaddr, & amp; Len);
Cout & lt; <"The receive end" & lt; Cout & lt; Cout & lt; <"IP Address:" & lt; Cout & lt; <"Recv:" & lt; Waitsec (1);
}
Closesocket (sock);
WSACleanup();
Return 0;
}



 
//the sender
Int waitsec (int SEC)
{
While (sec> 0 {
Cout & lt; The SEC -;
Sleep (1000);
}
Return 0;
}

Int main (int arg c, char * argv [])
{
Char TMP [100]={0};
Int ret.
Int len.
WSADATA data;
WSAStartup (MAKEWORD (2, 2), & amp; The data);
The SOCKET sock;
The sock=socket (AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN locaddr recvaddr;
Locaddr. Sin_port=htons (4000);
Locaddr. Sin_family=AF_INET;
Locaddr. Sin_addr. S_un. S_addr=inet_addr (" 192.168.4.35 ");
Recvaddr. Sin_port=htons (4000);
Recvaddr. Sin_family=AF_INET;
Recvaddr. Sin_addr. S_un. S_addr=inet_addr (" 192.168.4.35 ");
The setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) & amp; Locaddr, TRUE);
Bind (sock, (SOCKADDR *) & amp; Locaddr, sizeof (SOCKADDR_IN));
Len=sizeof (SOCKADDR_IN);
While (1) {
Cout & lt; <"Sendto star" & lt; Ret=sendto (sock, "sender", 6, 0, (SOCKADDR *) & amp; Recvaddr, sizeof (SOCKADDR_IN));
Cout & lt; <"Sendto end" & lt; Cout & lt; <"IP Address:" & lt; Cout & lt; <"Send." & lt; Waitsec (5);
/* cout & lt; <"The receive star" & lt; Ret=recvfrom (sock, TMP, 100, 0, (SOCKADDR *) & amp; Recvaddr, & amp; Len);
Cout & lt; <"The receive end" & lt; Cout & lt; <"LEN=" & lt; }
Closesocket (sock);
WSACleanup();
Return 0;
}

CodePudding user response:

Not must first open the receiver recvfrom function, the call to the sender sendto
And recvfrom function source address (from) the fourth parameter is a function to fill, it receives what IP data, just fill in the IP
  • Related