向其他 PC 发送以太网 (UDP) 帧
Sending Ethernet (UDP) Frames to other PCs
我正在尝试使用 C++ 将 UDP 帧作为客户端-服务器应用程序从我的笔记本电脑发送到另一台 PC。我正在使用 WireShark 监控以太网端口,但我没有看到从我的笔记本电脑发送的任何信息。有人可以帮忙吗?我错过了重要的一步吗?
/*
Simple udp client
*/
#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define SERVER "10.222.14.229"
#define BUFLEN 512
#define PORT 8888
int main(void)
{
struct sockaddr_in si_other;
int s, slen = sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];
char message1[] = "Hello";
WSADATA wsa;
//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//create socket
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
{
printf("socket() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
memset((char *)&si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
while (1)
{
if (sendto(s, message1, strlen(message1), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
memset(buf, '[=10=]', BUFLEN);
puts(buf);
}
closesocket(s);
WSACleanup();
return 0;
}
该代码没有任何问题 - 它在这里运行良好(除了繁忙的循环)。或者:
- 数据包没有通过网络发送出去,可能是因为没有到
10.222.14.229
的路由(尝试 ping 它),或者
- WireShark 运行不正常(它能看到来自您笔记本电脑的其他流量吗?-它可能设置了过滤器或其他东西)
如果您怀疑 WireShark,您可以随时尝试 Microsoft Network Monitor。
本帖的解决方案:
WireShark 在 Windows 10 上安装时缺少 Pincap 库。
安装 Pincap 库解决了问题。
我正在尝试使用 C++ 将 UDP 帧作为客户端-服务器应用程序从我的笔记本电脑发送到另一台 PC。我正在使用 WireShark 监控以太网端口,但我没有看到从我的笔记本电脑发送的任何信息。有人可以帮忙吗?我错过了重要的一步吗?
/*
Simple udp client
*/
#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define SERVER "10.222.14.229"
#define BUFLEN 512
#define PORT 8888
int main(void)
{
struct sockaddr_in si_other;
int s, slen = sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];
char message1[] = "Hello";
WSADATA wsa;
//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//create socket
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
{
printf("socket() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
memset((char *)&si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
while (1)
{
if (sendto(s, message1, strlen(message1), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
memset(buf, '[=10=]', BUFLEN);
puts(buf);
}
closesocket(s);
WSACleanup();
return 0;
}
该代码没有任何问题 - 它在这里运行良好(除了繁忙的循环)。或者:
- 数据包没有通过网络发送出去,可能是因为没有到
10.222.14.229
的路由(尝试 ping 它),或者 - WireShark 运行不正常(它能看到来自您笔记本电脑的其他流量吗?-它可能设置了过滤器或其他东西)
如果您怀疑 WireShark,您可以随时尝试 Microsoft Network Monitor。
本帖的解决方案:
WireShark 在 Windows 10 上安装时缺少 Pincap 库。
安装 Pincap 库解决了问题。