将以太网 (UDP) 帧发送到其他 PC

Sending Ethernet (UDP) Frames to other PCs

本文关键字:其他 PC 以太网 UDP      更新时间:2023-10-16

我正在尝试使用 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, '', BUFLEN);   
puts(buf);
}
closesocket(s);
WSACleanup();
return 0;
}

该代码没有任何问题 - 它在这里运行良好(除了繁忙的循环(。 也:

数据包没有通过
  • 线路发出,可能是因为没有路由10.222.14.229(尝试 ping 它(,或者
  • WireShark无法正常运行(它可以看到笔记本电脑的其他流量吗?- 它可能设置了过滤器或其他东西(

如果您怀疑WireShark,您可以随时尝试Microsoft网络监视器。

此线程的解决方案:

WireShark在Windows 10上安装时缺少Pincap库。

安装针帽库解决了这个问题。