udp-UDP绑定本地端口和IP,用sendto向指定端口发广播数据失败

问题描述

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()的返回值是什么?

时间: 2024-09-14 17:13:42

udp-UDP绑定本地端口和IP,用sendto向指定端口发广播数据失败的相关文章

udp-qt UDP 如何绑定目的ip

问题描述 qt UDP 如何绑定目的ip lineEdit = new QLineEdit(this);QString ip_dest;udpSocket=new QUdpSocket(this);port=69; //tftpip_dest =lineEdit->text();udpSocket->bind(ip_destport); 报错 :G:qtprojectstesttest125mainwindow.cpp:24: error: C2664: "bool QAbstrac

udp-QT中 UDP 怎么绑定发送端口

问题描述 QT中 UDP 怎么绑定发送端口 int Sender::Send(QString ipaddr,quint16 port) { QHostAddress serverAddress = QHostAddress(ipaddr); return udpSocket->writeDatagram(TX_data.data(), TX_data.size(),serverAddress, port); } 发送函数可以发送数据到指定的对方的地址和端口,但是发送所使用的端口是系统随机分配的,

LinuxC下获取UDP包中的路由目的IP地址和头标识目的地址

在接受到UDP包后,有时候我们需要根据所接收到得UDP包,获取它的路由目的IP地址和头标识目的地址. (一)主要的步骤: 在setsockopt中设置IP_PKTINFO,然后通过recvmsg来获取struct in_pktinfo(struct in_pktinfo是struct msghdr中msg_control的成员).in_pktinfo 结构体(如下所示),我们可以从in_pktinfo中获取路由目的地址(destination address of the packet).头标识

iOS UDP组播服务端收不到客户端发的数据

问题描述 iOS UDP组播服务端收不到客户端发的数据 客户端可以收到服务器发的数据,反过来不行.我想实现双向多播,理论上是可行的吧? 多播绑定的端口是别人向自己发数据的端口吧?加入的多播组应该是向别人发送数据的地址吧? 使用GCDAsyncUdpSocket,为什么只绑定一个端口就行?不是还要绑定IP吗? 本地Socket的IP.端口与发送的目的IP.端口,有点绕晕了,请高手指教 解决方案 bind绑定的是本地的ip地址和端口, 加入多播组的调用中使用对端向外发送的多播ip(通常是224.x.

UDP 收/发 广播包

网络通信基础 如果网络中两个主机上的应用程序要相互通信,其一要知道彼此的IP,其二要知道程序可监听的端口.因为同一主机上的程序使用网络是通过端口号来区分的. UDP Socket的使用过程: 1. 初始化网络库 2. 创建SOCK_DGRAM类型的Socket. 3. 绑定套接字. 4. 发送.接收数据. 5. 销毁套接字. 6. 释放网络库. 广播数据包的原理: 专门用于同时向网络中所有工作站进行发送的一个地址叫做广播地址.在使用TCP/IP 协议的网络中,主机标识段host ID 为全1 的

我在C#里面使用UdpClient这个类侦听端口:11000时,如何得到向我发信息人的IP与端口呢

问题描述 我的机器是处在公网,用UdpClient类的Receive()方法来接收信息,但这个方法只能得到信息内容,不能得到信息从哪个IP.端口而来.两台机器都在公网还好做,就把自已的IP打包成数据发送过去就行了.如果有一台机器在内网,内网的机器使用Dns.GetHostEntry(Dns.GetHostName())也只能得到内网IP,打包内网IP发过去也没用了.只要接收方能够得到发信息方的IP和端口就好办了.很急,知道的朋友帮帮忙啊. 解决方案 解决方案二:Receive需要一下IPEndP

Python扫描IP段查看指定端口是否开放的方法

  本文实例讲述了Python扫描IP段查看指定端口是否开放的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 6

SAS112如何修改数据端口的IP地址

1. 在IE中输入管理端口的IP,再输入用户名,密码(manage,!manage)进入管理界面; 2. 在左侧菜单栏,SAS112上面右键,选择configuration----system settings------host interfaces; 3. 进入配置各个数据端口的ip的界面,根据需要进行配置: 4. 配置完成后,点击下方的apply,确认配置.

WINDOWS 2003 防火墙设置 只允许 指定IP 访问指定端口

前不久在阿里云买的一个服务器,由于没有设置防火墙,前几天突然提示外地登录,立即关闭了服务器. 第二天修改密码,重新开启不一会又提示在别处登录,当时很是无语啊~~~ 于是就寻找只允许指定IP访问指定端口的方法~~好点的是,终于找到了~哈哈~~ 1.打开控制面板   2.打开防火墙 ,点例外 3.点添加端口   4.输入要控制的端口   5.点更改范围   6.选择自定义列表   7.输入允许访问的IP(格式为:192.168.3.100,192.168.3.101/255.255.255.255)