将以太网 (UDP) 帧发送到其他 PC
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, ' ', BUFLEN);
puts(buf);
}
closesocket(s);
WSACleanup();
return 0;
}
该代码没有任何问题 - 它在这里运行良好(除了繁忙的循环(。 也:
数据包没有通过- 线路发出,可能是因为没有路由
10.222.14.229
(尝试 ping 它(,或者 - WireShark无法正常运行(它可以看到笔记本电脑的其他流量吗?- 它可能设置了过滤器或其他东西(
如果您怀疑WireShark,您可以随时尝试Microsoft网络监视器。
此线程的解决方案:
WireShark在Windows 10上安装时缺少Pincap库。
安装针帽库解决了这个问题。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 在其他文件中创建类时在 c++ 项目中不起作用
- 类与私有变量的其他类之间的线程安全性
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- GlobalAlloc而不是其他分配方法
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- 在Visual Studio中包括用于其他PC的库
- 将以太网 (UDP) 帧发送到其他 PC
- 编译的程序在其他窗口PC上停止响应
- 使用 mfc 的 EXE(调试)不能在其他 PC 上运行
- 如何使C 项目在其他PC上运行
- 在C++程序中引用C#库,但不能引用其他PC
- 当您下载其他库并将其功能添加到C ++程序中时,您是否能够在另一台PC上运行您的软件(没有库)?
- 通过蓝牙将文件从PC传输到其他设备的代码示例c++