简体   繁体   English

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

[英]Sending Ethernet (UDP) Frames to other PCs

I am trying to send UDP frames from my laptop to another PC as a client-server application using C++. 我正在尝试使用C ++将UDP帧从笔记本电脑发送到另一台PC,作为客户端服务器应用程序。 I am monitoring the ethernet port using WireShark and I do not see any information being sent from my laptop. 我正在使用WireShark监视以太网端口,但看不到笔记本电脑发送的任何信息。 Can someone help regarding this? 有人可以帮忙吗? Am I missing an important step? 我错过了重要的一步吗?

/*
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;
}

There's nothing wrong with that code - it runs fine here (apart from the busy loop). 该代码没有任何问题-它在这里运行正常(除了忙循环)。 Either: 要么:

  • the packets aren't going out on the wire, perhaps because there is no route to 10.222.14.229 (try pinging it), or 数据包没有通过网络发送出去,可能是因为没有到10.222.14.229路由(尝试对其进行ping操作),或者
  • WireShark isn't functioning properly (can it see other traffic from your laptop? - it might have a filter set or something) WireShark无法正常运行(它可以从您的笔记本电脑看到其他流量吗?-可能有过滤器设置或其他设置)

If you suspect WireShark, you could always try Microsoft Network Monitor . 如果您怀疑WireShark,可以随时尝试使用Microsoft Network Monitor

The solution to this thread: 该线程的解决方案:

WireShark was missing the Pincap library when it was installed on Windows 10. 在Windows 10上安装时,WireShark缺少Pincap库。

Installing the Pincap library solved the problem. 安装Pincap库可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM