Home > OS >  Create two sockets under Windows, the second failure
Create two sockets under Windows, the second failure

Time:09-30

Under Windows UPD example, a sending a receiving function, the main call recv function first receive normal, debug tracking to send,
After
The SOCKET sockClient=SOCKET (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Directly out to the function, the sock create return the value of success,

If you send, send is normal, recv has been blocked to recfrom inside, each function call alone is normal, and that in Linux running normal,

 
//udptest. CPP: defines the entry point of the console application,
//

# include "stdafx. H"

#include
#include
#include

# pragma comment (lib, "ws2_32. Lib")
# define S_PORT 3702
# define R_PORT 3701
# define BUFLEN 2048
Int updrecv (void)
{

//initialize network environment
WSADATA wsa.
If (WSAStartup (MAKEWORD (2, 2), & amp; Wsa)!=0)
{
Printf (" WSAStartup failed \ n ");
return -1;
}


//create a UDP socket
SOCKET sock=SOCKET (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
If (the sock==SOCKET_ERROR)
{
Printf (" create socket failed \ n ");
return -1;
}
//binding address information
Sockaddr_in serverAddr;
ServerAddr. Sin_family=AF_INET;
ServerAddr. Sin_port=htons (R_PORT);
ServerAddr. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);

If (bind the sock, (sockaddr *) & amp; ServerAddr, sizeof (sockaddr))!=0)
{
Perror (" bind ");
}

Char buf [BUFLEN];
If (TRUE)
{
Memset (buf, 0, 512);
//network node information, used to store client network information
Sockaddr_in clientAddr;
Memset (& amp; ClientAddr, 0, sizeof (sockaddr_in));

Int clientAddrLen=sizeof (sockaddr);
//receive from the client data
Int ret=recvfrom (sock, buf, BUFLEN, 0, (sockaddr *) & amp; ClientAddr, & amp; ClientAddrLen);
//int ret=recvfrom (sock, buf, BUFLEN, 0, NULL, NULL);
Printf (" Recv MSG: % s from IP: Port: [% s] [% d] \ n ", buf, inet_ntoa (clientAddr. Sin_addr), ntohs (clientAddr. Sin_port));
//send a packet returned to the customer
Sendto (sock, "Hello World!" The strlen (" Hello World!" ), and 0, (sockaddr *) & amp; ClientAddr clientAddrLen);
Printf (" Send MSG back to IP: Port: [% s] [% d] \ n ", inet_ntoa (clientAddr. Sin_addr), ntohs (clientAddr. Sin_port));
}
Closesocket (sock);
WSACleanup();
return 0;
}

Int updsend (void)
{
//initialize network environment
WSADATA wsa.
If (WSAStartup (MAKEWORD (2, 2), & amp; Wsa)!=0)
{
Printf (" WSAStartup failed \ n ");
return -1;
}

//send a socket
//create a UDP socket
The SOCKET sockClient=SOCKET (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
If (sockClient==INVALID_SOCKET)
{
Printf (" create socket failed \ n ");
return -1;
}
//that the structure of a network address information, save the server address information
Sockaddr_in addr={0};
addr.sin_family=AF_INET;
Addr. Sin_port=htons (S_PORT);
Addr. Sin_addr. S_un. S_addr=inet_addr (" 255.255.255.255 ");
.//addr. Sin_addr. S_un S_addr=inet_addr (" 127.0.0.1 ");

Int opt=1;
Int nb=0;
Nb=setsockopt (sockClient, SOL_SOCKET, SO_BROADCAST, (char *) & amp; Opt, sizeof (opt));
If (nb==1)
{
Printf (" set the socket error... \n");
}

Char buf []="client test!" ;
//send data
Int dwSent=sendto (sockClient, buf, strlen (buf), 0, (SOCKADDR *) & amp; Addr, sizeof (SOCKADDR));
If (dwSent==0)
{
Printf (" failed send % s \ n ", buf);
return -1;
}
Closesocket (sockClient);
WSACleanup();
}

Int main (int arg c, char * argv [])
{
Updrecv ();
Updsend ();
System (" pause ");
return 0;
}

CodePudding user response:

Ah! Debug the problem for such a long time to find, client is the first message is sent, the server before running, missed the news, so when receiving has been blocked in recvfrom

CodePudding user response:

You can get the cause of the failure by calling WSAGetLastError
  • Related