0

I saw something that was a red flag for me when I ran this:

nc -l -u -p 1500

This netcat command makes it listen for packets from UDP port 1500. Once i ran this on the windows command line, I immediately got a Windows Firewall has disabled features blah blah blah popup message. Sadly I realized that my own UDP code did not trigger this, so I must be doing something wrong. Here is the current iteration of my winsock code in case it is relevant:

#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif
#include <sys/types.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <TinyThread++/tinythread.h>
#include <iostream>

#ifdef WIN32
class WSA {
public:
    WSA() {
        WSADATA wsaData;
        WORD wVersionRequested = MAKEWORD(2,0);
        int err = WSAStartup(wVersionRequested, &wsaData);
        if (err != 0) {
            PRINT("WSAStartup failed with an error: "); PRINT_INT(err);
            exit(-1);
        }
    }
    ~WSA() {
        WSACleanup();
    }
};
static WSA wsa_placeholder;
static int UDPSocket = -1;
static int listen_port = -1;

int initUDPSocket(int port) {
    listen_port = port;
    UDPSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    ASSERT(UDPSocket != -1);
    sockaddr_in myaddr; 
    memset(&myaddr,0,sizeof(myaddr)); // clear it
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    ASSERT(listen_port > 0);
    myaddr.sin_port = htons(listen_port);
    ASSERT(bind(UDPSocket, (sockaddr *)&myaddr, sizeof(myaddr))!=-1);
    return UDPSocket;
}


void closeUDPSocket() {
    closesocket(UDPSocket);
    UDPSocket = -1;
}

using namespace std;
using namespace tthread;
void UDPListenThread (void *arg) {
    cout << "UDP Listening thread ID: " << this_thread::get_id() << endl;
    while (1) {
        sockaddr_in from;
        int fromlen = sizeof(from);
        size_t len;
        char buf[2048];
        len = recvfrom(UDPSocket, buf, 2048, 0, (sockaddr*)&from, &fromlen);
        printf("Received packet from %s:%d, length %u\n",inet_ntoa(from.sin_addr),
                ntohs(from.sin_port),len);
        hexdump(buf,len);
        if (len == 2 && buf[0] == 'q' && buf[1] == 0x0a) {
            break;
        }
    }
    cout << "Received a quit packet. exiting routine (thread)." << endl;
}
thread startUDPListenThread() {
    cout << "Main thread ID: " << this_thread::get_id() << endl;
    return thread(UDPListenThread,0);
}

If I use netcat to send UDP packets from the same machine to my program, it receives them. But it does not receive packets from the internet. Also, why is it that Windows Firewall didn't seem to mind? Is there something else that I need to do similar to WSAStartup?

1 Answer 1

1

You are binding to localhost, Windows firewall has separate configurations for Private, Public, and Domain, which presumably map onto intranet & localhost, the Internet, and Kerberos authenticated domain.

Bind to 0.0.0.0 (INADDR_ANY) to listen on all interfaces.

Sign up to request clarification or add additional context in comments.

1 Comment

You got the exact thing that I was missing. Thank you sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.