ayo
ayo

Reputation: 57

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++. I am monitoring the ethernet port using WireShark and I do not see any information being sent from my laptop. 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;
}

Upvotes: 0

Views: 901

Answers (2)

ayo
ayo

Reputation: 57

The solution to this thread:

WireShark was missing the Pincap library when it was installed on Windows 10.

Installing the Pincap library solved the problem.

Upvotes: 1

catnip
catnip

Reputation: 25388

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
  • WireShark isn't functioning properly (can it see other traffic from your laptop? - it might have a filter set or something)

If you suspect WireShark, you could always try Microsoft Network Monitor.

Upvotes: 1

Related Questions