【问题标题】:Sending Ethernet (UDP) Frames to other PCs向其他 PC 发送以太网 (UDP) 帧
【发布时间】:2018-07-05 14:46:05
【问题描述】:

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

【问题讨论】:

    标签: c++ sockets networking udp ethernet


    【解决方案1】:

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

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

    如果您怀疑 WireShark,您可以随时尝试Microsoft Network Monitor

    【讨论】:

    • 我创建了繁忙的循环以便在 WireShark 上监控它。我将首先尝试 MNM,并会进行相应的更新。
    • @PaulSanders 您没有提出正确的问题。投了反对票。
    • 好的,LMK。我现在需要投票,因为@EJP 认为他需要get back at me :)
    • @PaulSanders 我只是想鼓励你解释你拒绝投票的奇怪行为,就好像它们是问题一样。
    • @EJP 好吧,这不是这样做的方法。你可以问我。我评论了other thread
    【解决方案2】:

    本帖的解决方法:

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

    安装 Pincap 库解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多