Socket收不到广播数据,但Sniffer可以抓到数据?

wenjiu5 2004-08-17 08:56:57
下面的代码是在PC端运行的接收程序。
// Module Name: Receiver.c
//
// Description:
// This sample receives UDP datagrams by binding to the specified
// interface and port number and then blocking on a recvfrom()
// call
//
// Compile:
// cl -o Receiver Receiver.c ws2_32.lib
//
// Command line options:
// sender [-p:int] [-i:IP][-n:x] [-b:x]
// -p:int Local port
// -i:IP Local IP address to listen on
// -n:x Number of times to send message
// -b:x Size of buffer to send
//
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>

#define DEFAULT_PORT 21024
#define DEFAULT_COUNT 25
#define DEFAULT_BUFFER_LENGTH 4096

int iPort = DEFAULT_PORT; // Port to receive on
DWORD dwCount = DEFAULT_COUNT, // Number of messages to read
dwLength = DEFAULT_BUFFER_LENGTH; // Length of receiving buffer
BOOL bInterface = FALSE; // Use an interface other than
// default
char szInterface[32]; // Interface to read datagrams from

//
// Function: usage:
//
// Description:
// Print usage information and exit
//
void usage()
{
printf("usage: sender [-p:int] [-i:IP][-n:x] [-b:x]\n\n");
printf(" -p:int Local port\n");
printf(" -i:IP Local IP address to listen on\n");
printf(" -n:x Number of times to send message\n");
printf(" -b:x Size of buffer to send\n\n");
ExitProcess(1);
}

//
// Function: ValidateArgs
//
// Description:
// Parse the command line arguments, and set some global flags to
// indicate what actions to perform
//
void ValidateArgs(int argc, char **argv)
{
int i;

for(i = 1; i < argc; i++)
{
if ((argv[i][0] == '-') || (argv[i][0] == '/'))
{
switch (tolower(argv[i][1]))
{
case 'p': // Local port
if (strlen(argv[i]) > 3)
iPort = atoi(&argv[i][3]);
break;
case 'n': // Number of times to receive message
if (strlen(argv[i]) > 3)
dwCount = atol(&argv[i][3]);
break;
case 'b': // Buffer size
if (strlen(argv[i]) > 3)
dwLength = atol(&argv[i][3]);
break;
case 'i': // Interface to receive datagrams on
if (strlen(argv[i]) > 3)
{
bInterface = TRUE;
strcpy(szInterface, &argv[i][3]);
}
break;
default:
usage();
break;
}
}
}
}

//
// Function: main
//
// Description:
// Main thread of execution. Initialize Winsock, parse the command
// line arguments, create a socket, bind it to a local interface
// and port, and then read datagrams.
//
int main(int argc, char **argv)
{
WSADATA wsd;
SOCKET s;
char *recvbuf = NULL;
int ret,
i;
DWORD dwSenderSize;
SOCKADDR_IN sender,
local;

// Parse arguments and load Winsock
//
ValidateArgs(argc, argv);

if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
// Create the socket and bind it to a local interface and port
//
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
{
printf("socket() failed; %d\n", WSAGetLastError());
return 1;
}
local.sin_family = AF_INET;
local.sin_port = htons((short)iPort);
// if (bInterface)
// local.sin_addr.s_addr = inet_addr(szInterface);
// else
local.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)
{
printf("bind() failed: %d\n", WSAGetLastError());
return 1;
}
// Allocate the receive buffer
//
recvbuf = GlobalAlloc(GMEM_FIXED, dwLength);
if (!recvbuf)
{
printf("GlobalAlloc() failed: %d\n", GetLastError());
return 1;
}
// Read the datagrams
//
for(i = 0; i < dwCount; i++)
{
dwSenderSize = sizeof(sender);
ret = recvfrom(s, recvbuf, dwLength, 0,
(SOCKADDR *)&sender, &dwSenderSize);
if (ret == SOCKET_ERROR)
{
printf("recvfrom() failed; %d\n", WSAGetLastError());
break;
}
else if (ret == 0)
break;
else
{
recvbuf[ret] = '\0';
printf("[%s] sent me: '%s'\n",
inet_ntoa(sender.sin_addr), recvbuf);
}
}
closesocket(s);

GlobalFree(recvbuf);
WSACleanup();
return 0;
}

问题是这样的,有一台嵌入式机顶盒(OS为VxWorks),有一个IP 192.168.100.99,他想用“255.255.255.255”作目标地址发广播,我的PC的IP为200.200.200.126,我无法收到数据。用Sniffer分析发现,他实际上是用"192.168.100.255"作目标地址发送的,由于不在同一网段,所以收不到。后来机顶盒把自己的IP改为 255.255.255.99,再次向“255.255.255.255”发广播,用Sniffer分析,确实是以255.255.255.255作目标地址发送的。但我上面的代码却收不到他发的数据,请高手指教(声明:端口号为21024,肯定不会错)。
...全文
210 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
howtotell 2004-08-17
  • 打赏
  • 举报
回复
255.255.255.99
这个以255打头的ip是保留的吧,换个试试看.
wenjiu5 2004-08-17
  • 打赏
  • 举报
回复
在同网段上发能收到,由于某些原因,我必须用广播。
铖邑 2004-08-17
  • 打赏
  • 举报
回复
或者不用广播发,直接对地址发,再看看能不能收到
铖邑 2004-08-17
  • 打赏
  • 举报
回复
你在同网段发,看看能收到吗?
wenjiu5 2004-08-17
  • 打赏
  • 举报
回复
xpdavis(咕嘟) :
不可能吧,在我的PC上用Sniffer可以看到他发的数据,如果路由将广播屏蔽掉了,Sniffer也应该抓不到数据才对呀
ydfok 2004-08-17
  • 打赏
  • 举报
回复
测试网络功能就要考虑交换机 路由器的 影响了。
在最开始的功能的可行性测试的时候
最好要避开这些东西。

要是你本来就避开了,
那就socket没写好
再自己分析一下代码了~~
铖邑 2004-08-17
  • 打赏
  • 举报
回复
可能是路由将广播屏蔽掉了

18,356

社区成员

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

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