问题描述
- UDP绑定本地端口和IP,用sendto向指定端口发广播数据失败
-
MFC下,上位机采用UDP协议,绑定本地端口60000,向目标端口50000发送广播数据,然后单片机应答。但是上位机调用sendto函数发送失败!
解决方案
代码如下:
int ret = -1;
CString str;
char buf[10] = {0,1,2,3,4,5,6,7,8,9};
/* 加载套接字库 */
if (!AfxSocketInit())
MessageBox("加载套接字库失败!", "错误信息");
/* 创建套接字 */
int sockfd = -1;
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd < 0)
MessageBox("创建套接字失败!", "错误信息");
else
{
str.Format("创建套接字成功 %d", sockfd);
MessageBox(str, "提示信息");
}
/* 本地端口和地址 */
SOCKADDR_IN src_addr;
memset(&src_addr, 0, sizeof(src_addr));
src_addr.sin_family = AF_INET;
src_addr.sin_port = htons(60000);
src_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// inet_pton(AF_INET, "192.168.1.102", &src_addr.sin_addr.s_addr);
memset(src_addr.sin_zero, 0, sizeof(src_addr.sin_zero));
/* 目标端口和地址 */
SOCKADDR_IN dst_addr;
dst_addr.sin_family = AF_INET;
dst_addr.sin_port = htons(50000);
dst_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
memset(dst_addr.sin_zero, 0, sizeof(dst_addr.sin_zero));
/* 绑定 */
ret = bind(sockfd, (SOCKADDR *)&src_addr, sizeof(SOCKADDR));
if (ret < 0)
{
str.Format("绑定失败 %d", ret);
MessageBox(str, "错误信息");
}
else
{
str.Format("绑定成功 %d", ret);
MessageBox(str, "提示信息");
}
/* 发送数据 */
ret = sendto(sockfd, buf, sizeof(buf), 0, (SOCKADDR*)&dst_addr, sizeof(dst_addr));
if (ret < 0)
{
str.Format("发送失败 %d", ret);
MessageBox(str, "错误信息");
}
解决方案二:
看一下 sendto 的返回值,根据返回的失败原因再分析吧。
Error code Description
WSANOTINITIALISED A successful WSAStartup call must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAEACCES The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt (Windows Sockets) with the SO_BROADCAST parameter to allow the use of the broadcast address.
WSAEINVAL An unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled.
WSAEINTR The socket was closed.
WSAEINPROGRESS A blocking Winsock call is in progress, or the service provider is still processing a callback function.
WSAEFAULT The buf or to parameter is not part of the user address space, or the tolen parameter is too small.
WSAENETRESET The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.
WSAENOBUFS No buffer space is available.
WSAENOTCONN The socket is not connected (connection-oriented sockets only).
WSAENOTSOCK The descriptor is not a socket.
WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not stream style such as type SOCK_STREAM, out of band (OOB) data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only receive operations.
WSAESHUTDOWN The socket has been shut down. It is not possible to call sendto on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH.
WSAEWOULDBLOCK The socket is marked as nonblocking and the requested operation would block.
WSAEMSGSIZE The socket is message-oriented, and the message is larger than the maximum supported by the underlying transport.
WSAEHOSTUNREACH The remote host cannot be reached from this host at this time.
WSAECONNABORTED The virtual circuit was terminated due to a time-out or other failure. The application should close the socket because it is no longer usable.
WSAECONNRESET The virtual circuit was reset by the remote side executing a hard or abortive close. For UDP sockets, the remote host was unable to deliver a previously sent UDP datagram and responded with a "Port Unreachable" ICMP packet. The application should close the socket because it is no longer usable.
WSAEADDRNOTAVAIL The remote address is not a valid address, for example, ADDR_ANY.
WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket.
WSAEDESTADDRREQ A destination address is required.
WSAENETUNREACH The network cannot be reached from this host at this time.
WSAETIMEDOUT The connection has been dropped because of a network failure or because the system on the other end went down without notice.
解决方案三:
WSAGetLastError()的返回值是什么?