电脑2个网卡,如何分别发送广播出去

这是一个账号007 2018-07-12 09:44:09
电脑2个网卡,分别用网线直连2个设备。

2个网卡的IP分别是192.168.1.100和192.168.1.101
子网掩码255.255.255.0
网关都是192.168.1.1
2个设备的IP分别是192.168.1.215和192.168.1.216

我发送广播的构造函数这样的
CreateBroadcastSocket()
{
if (m_udpSockfd != INVALID_SOCKET)
{
TRACE("av matrix log : CreateBroadcastSocket() socet is live\n");
CloseSocket();
}

m_udpSockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

if (m_udpSockfd == INVALID_SOCKET)
{
WSACleanup();//清理库
TRACE("av matrix log : CreateBroadcastSocket() create socket err\n");
return FALSE;
}

m_serverAddr.sin_family = AF_INET;
m_serverAddr.sin_port = htons(UDP_PORT);
m_serverAddr.sin_addr.s_addr = INADDR_BROADCAST;

bool bOpt = true;
setsockopt(m_udpSockfd, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));

SOCKADDR_IN localAddr;
localAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(UDP_PORT);

if(bind(m_udpSockfd,(SOCKADDR*)&localAddr,sizeof(SOCKADDR))<0)
{
TRACE("av matrix log : CreateBroadcastSocket() bind err\n");
return FALSE;
}

int mode = 1; // noblocking
ioctlsocket(m_udpSockfd, FIONBIO, (ULONG*)&mode);//非阻塞

return TRUE;
}


用cmd去ping的话2个设备都能ping得通,但是使用上面这个构造的广播socket,来发送广播,只能收到一个设备的返回信息。
请问应该怎么处理这种情况呢?
...全文
654 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
GongKiro 2018-07-20
  • 打赏
  • 举报
回复
bind指定IP的socket就可以了,这样就可以往电脑的每个IP中发送数据了,而且这个是独立的socket了,两个电脑IP不会项目干扰
仍在发呆的~ 2018-07-18
  • 打赏
  • 举报
回复
你发送的时候得发送两次广播, 你是不是想通过发送一次广播, 让两个设备都收到? 一次广播,你用其中一个网卡发送, 但是和另一个网卡的设备不通。 你之所以ping能通, 是因为ping有默认的网卡ping的
赵4老师 2018-07-12
  • 打赏
  • 举报
回复
bind
The Windows Sockets bind function associates a local address with a socket.

int bind (
SOCKET s,
const struct sockaddr FAR* name,
int namelen
);

Parameters
s
[in] A descriptor identifying an unbound socket.
name
[in] The address to assign to the socket from the SOCKADDR structure.
namelen
[in] The length of the name.
Remarks
The bind function is used on an unconnected socket before subsequent calls to the connect or listen functions. It is used to bind to either connection-oriented (stream) or connectionless (datagram) sockets. When a socket is created with a call to the socket function, it exists in a name space (address family), but it has no name assigned to it. Use bind to establish the local association of the socket by assigning a local name to an unnamed socket.

A name consists of three parts when using the Internet address family: the address family, a host address, and a port number that identifies the application. In Windows Sockets 2, the name parameter is not strictly interpreted as a pointer to a SOCKADDR structure. It is cast this way for Windows Sockets 1.1 compatibility. Service Providers are free to regard it as a pointer to a block of memory of size namelen. The first two bytes in this block (corresponding to the sa_family member of the SOCKADDR structure) must contain the address family that was used to create the socket. Otherwise, an error WSAEFAULT will occur.

If an application does not care what local address is assigned, specify the manifest constant value ADDR_ANY for the sa_data member of the name parameter. This allows the underlying service provider to use any appropriate network address, potentially simplifying application programming in the presence of multihomed hosts (that is, hosts that have more than one network interface and address).

For TCP/IP, if the port is specified as zero, the service provider will assign a unique port to the application with a value between 1024 and 5000. The application can use getsockname after bind to learn the address and the port that has been assigned to it. If the Internet address is equal to INADDR_ANY, getsockname will not necessarily be able to supply the address until the socket is connected, since several addresses can be valid if the host is multihomed. Binding to a specific port number other than port 0 is discouraged for client applications, since there is a danger of conflicting with another socket already using that port number.

Windows CE: For IrSocket implementation of this function:

The Af_irda.h must be explicitly included.
The SOCKADDR_IRDA_wcesdk_SOCKADDR_IRDA structure is used in the addr parameter.
The WSAENETDOWN error value is not supported.
There is no wildcard address equivalent to INADDR_ANY.
IrSockets clients must call bind before using a connect function. If the service name is of the form "LSAP-SELxxx" where xxx is a decimal integer in the range 0-255, the address indicates a specific LSAP-SEL xxx rather than a service name. LSAP-SELxxx service names will cause no IAS calls. The socket will be bound directly to the LSAP-SEL specified bypassing IAS.

Return Values
If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

Error Codes
WSANOTINITIALISED A successful WSAStartup must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAEADDRINUSE A process on the machine is already bound to the same fully-qualified address and the socket has not been marked to allow address re-use with SO_REUSEADDR. For example, IP address and port are bound in the af_inet case) . (See the SO_REUSEADDR socket option under setsockopt.)
WSAEADDRNOTAVAIL The specified address is not a valid address for this machine
WSAEFAULT The name or the namelen parameter is not a valid part of the user address space, the namelen parameter is too small, the name parameter contains incorrect address format for the associated address family, or the first two bytes of the memory block specified by name does not match the address family associated with the socket descriptor s.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEINVAL The socket is already bound to an address.
WSAENOBUFS Not enough buffers available, too many connections.
WSAENOTSOCK The descriptor is not a socket.


QuickInfo
Windows NT: Yes
Windows: Yes
Windows CE: Use version 1.0 and later.
Header: Declared in winsock2.h.
Import Library: Link with ws2_32.lib.

See Also
connect, getsockname, listen, setsockopt, socket, WSACancelBlockingCall


worldy 2018-07-12
  • 打赏
  • 举报
回复
简单点,向

创建一个sock,确定端口,和一个网卡的ip关联,向
192.168.1.255或者向255.255.255.255 发送数据


18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧