简单的UDP监听,为什么经常需要打开WireShark才能接收到数据?不解求教

Cacar 2016-04-21 11:01:17
上位机程序在线程中进行网络监听,接收下位机上传的数据:
// 创建套接字
BOOL bRet = AfxSocketInit();

CSocket sockListen;
bRet = sockListen.Create(pModule->m_uPortRecv, SOCK_DGRAM, strIP);

BYTE *p = NULL;

while (pModule->m_bIsNetworkSet)
{
int iResult = sockListen.ReceiveFrom(buf, 1400, strIP, pModule->m_uPortRecv);
if (iResult == SOCKET_ERROR) // 如果接收失败,则继续监听并接收数据
continue;

跟踪返回值bRet都是成功的,接收不到数据的情况通常出现在重启计算机以后;当发现接收不到数据时,打开WireShark,可以看到是有数据的,此时软件也能成功接到数据;
有时候重启后,即使不开WireShark,也可以成功接收到数据;
想知道WireShark可能做了什么额外的操作,使得数据能够被上位机所接收?
开不开WireShark可能有什么样的区别呢?
请大家集思广益~多多指教!
...全文
5124 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cacar 2017-02-17
  • 打赏
  • 举报
回复
发现是因为wireshark会默认打开网卡的混杂模式,而我用代码来开启混杂模式,需要把接收SOCKET设置为SOCK_RAW,而这样做又会导致recvfrom接不到数据。 如何在SOCK_RAW类型的套接字上用recvfrom接收UDP数据包呢?
qq_34598917 2017-02-13
  • 打赏
  • 举报
回复
同样的问题没解决!求加qq953051394探讨
Cacar 2016-09-19
  • 打赏
  • 举报
回复 1
引用 9 楼 MiDEu 的回复:
[quote=引用 7 楼 Cacar 的回复:] [quote=引用 6 楼 MiDEu 的回复:] 我发现也是这样的 打开一个抓包软件的接口 程序才能继续接收 为什么呢 解决没有啊
没解决,原因不明。[/quote] 我的原因找到了 是因为下位机的端口在发送的时候MAC地址不对,网卡直接屏蔽了数据,当用wireshark工具打开监听的时候相当于屏蔽了网卡mac地址屏蔽的功能 数据就直接进来了[/quote] 怎么解决呢?修改下位机发送的目标mac?如果下位机目标mac无法更改的情况下,有没有什么办法可以“屏蔽网卡mac地址屏蔽功能”?@mideu
Anow_D 2016-09-05
  • 打赏
  • 举报
回复
原来如此,学到了
MiDEu 2016-09-04
  • 打赏
  • 举报
回复
引用 7 楼 Cacar 的回复:
[quote=引用 6 楼 MiDEu 的回复:] 我发现也是这样的 打开一个抓包软件的接口 程序才能继续接收 为什么呢 解决没有啊
没解决,原因不明。[/quote] 我的原因找到了 是因为下位机的端口在发送的时候MAC地址不对,网卡直接屏蔽了数据,当用wireshark工具打开监听的时候相当于屏蔽了网卡mac地址屏蔽的功能 数据就直接进来了
这不是鸭头 2016-09-03
  • 打赏
  • 举报
回复
找一个没装WireShark的电脑测试。有可能是winpcap的原因
Cacar 2016-09-02
  • 打赏
  • 举报
回复
引用 6 楼 MiDEu 的回复:
我发现也是这样的 打开一个抓包软件的接口 程序才能继续接收 为什么呢 解决没有啊
没解决,原因不明。
MiDEu 2016-08-10
  • 打赏
  • 举报
回复
我发现也是这样的 打开一个抓包软件的接口 程序才能继续接收 为什么呢 解决没有啊
赵4老师 2016-04-25
  • 打赏
  • 举报
回复 1
我只会粘贴MSDN原文。 sendto The Windows Sockets sendto function sends data to a specific destination. int sendto ( SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen ); Parameters s [in] A descriptor identifying a (possibly connected) socket. buf [in] A buffer containing the data to be transmitted. len [in] The length of the data in buf. flags [in] An indicator specifying the way in which the call is made. to [in] An optional pointer to the address of the target socket. tolen [in] The size of the address in to. Remarks The sendto function is used to write outgoing data on a socket. For message-oriented sockets, care must be taken not to exceed the maximum packet size of the underlying subnets, which can be obtained by using getsockopt to retrieve the value of socket option SO_MAX_MSG_SIZE. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned and no data is transmitted. The to parameter can be any valid address in the socket's address family, including a broadcast or any multicast address. To send to a broadcast address, an application must have used setsockopt with SO_BROADCAST enabled. Otherwise, sendto will fail with the error code WSAEACCES. For TCP/IP, an application can send to any multicast address (without becoming a group member). If the socket is unbound, unique values are assigned to the local association by the system, and the socket is then marked as bound. An application can use getsockname to determine the local socket name in this case. The successful completion of a sendto does not indicate that the data was successfully delivered. The sendto function is normally used on a connectionless socket to send a datagram to a specific peer socket identified by the to parameter. Even if the connectionless socket has been previously connected to a specific address, the to parameter overrides the destination address for that particular datagram only. On a connection-oriented socket, the to and tolen parameters are ignored, making sendto equivalent to send. For sockets using IP (version 4): To send a broadcast (on a SOCK_DGRAM only), the address in the to parameter should be constructed using the special IP address INADDR_BROADCAST (defined in WINSOCK2.H), together with the intended port number. It is generally inadvisable for a broadcast datagram to exceed the size at which fragmentation can occur, which implies that the data portion of the datagram (excluding headers) should not exceed 512 bytes. If no buffer space is available within the transport system to hold the data to be transmitted, sendto will block unless the socket has been placed in a nonblocking mode. On nonblocking, stream oriented sockets, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both the client and server systems. The select, WSAAsyncSelect or WSAEventSelect function can be used to determine when it is possible to send more data. Calling sendto with a len of zero is permissible and will return zero as a valid value. For message-oriented sockets, a zero-length transport datagram is sent. The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket. The semantics of this function are determined by the socket options and the flags parameter. The latter is constructed by or-ing the following values: Value Meaning MSG_DONTROUTE Specifies that the data should not be subject to routing. A Windows Sockets service provider can choose to ignore this flag. MSG_OOB Send out-of-band data (stream-style socket such as SOCK_STREAM only. Also see DECnet Out-Of-band data for a discussion of this topic.) Return Values If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, 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. WSAEACCES The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt 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 A blocking Windows Sockets 1.1 call was canceled through WSACancelBlockingCall. WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. WSAEFAULT The buf or to parameters are 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 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 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 as it is no longer usable. WSAECONNRESET The virtual circuit was reset by the remote side executing a "hard" or "abortive" close. For UPD 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 as 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. 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 recv, recvfrom, select, send, socket, WSAAsyncSelect, WSAEventSelect
kerwenfly 2016-04-23
  • 打赏
  • 举报
回复
WireShark应该不会对数据包修改的,时好时坏是不是你的接收程序的问题,端口被占用了?
Cacar 2016-04-23
  • 打赏
  • 举报
回复
引用 1楼赵4老师 的回复:
设置较小的发送缓冲区?
把接收缓冲设小一点?大概什么原因呢?求教~链接或其他参考资料都行,谢谢!
Cacar 2016-04-23
  • 打赏
  • 举报
回复
引用 2楼kerwenfly 的回复:
WireShark应该不会对数据包修改的,时好时坏是不是你的接收程序的问题,端口被占用了?
似乎也不像,如果是端口被占用的话,为什么一开wireshark就可以了呢?感觉讲不通啊
赵4老师 2016-04-22
  • 打赏
  • 举报
回复
设置较小的发送缓冲区?

18,363

社区成员

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

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