Test UDP port

* UdpTest.cpp

cpp 复制代码
/* 
* On client:
* gcc udp_test_windows.c -o udp_test_windows -lws2_32
* ./udp_tset_windows x.x.x.x 4665 # Replace x.x.x.x with your server IP address
* On server:
* sudo tcpdump -i any udp port 4665 -nn -XX
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "Ws2_32.lib")

int sendUdp(const char *ip, unsigned short port, const char *message) {
    WSADATA wsaData;
    SOCKET sockfd;
    struct sockaddr_in servaddr;
    int ret_code;

    /* Initialize Winsock */
    if ((ret_code = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) {
        fprintf(stderr, "WSAStartup failed with error: %d\n", WSAGetLastError());
        return ret_code;
    }

    /* Create socket */
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
        ret_code = WSAGetLastError();
        fprintf(stderr, "Socket creation failed with error: %d\n", ret_code);
        WSACleanup();
        return ret_code;
    }
    memset(&servaddr, 0, sizeof(servaddr));
    /* Set server address and port */
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(port);
    if (inet_pton(AF_INET, ip, &servaddr.sin_addr) <= 0) {
        fprintf(stderr, "Invalid address/ Address not supported\n");
        closesocket(sockfd);
        WSACleanup();
        return 2;  /* Custom error code for invalid address */
    }
    /* Send message to server */
    int n = sendto(sockfd, message, strlen(message), 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
    if (n == SOCKET_ERROR) {
        ret_code = WSAGetLastError();
        fprintf(stderr, "Send failed with error: %d\n", ret_code);
        closesocket(sockfd);
        WSACleanup();
        return ret_code;
    }
    closesocket(sockfd);
    WSACleanup();
    return 0;
}

int main(int argc, char* argv[]) 
{
    /* E:\Projects\VS_Projects\UdpTest\Debug\UdpTest.exe x.x.x.x 4665 */
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <IP> <port>\n", argv[0]);
        return 1;
    }

    const char* ip = argv[1];
    unsigned short port = atoi(argv[2]);
    const char* message = "Hello, aMule!";
    sendUdp(ip, port, message);
    printf("Message sent to %s:%d\n", ip, port);

    return 0;
}

Add port 4665/udp to the firewall configuration:

bash 复制代码
default_zone=$(firewall-cmd --get-default-zone)
firewall-cmd --permanent --zone=${default_zone} --add-port=4665/udp
firewall-cmd --reload
firewall-cmd --state
firewall-cmd --list-all --zone=${default_zone}

Test output:

相关推荐
lwprain16 分钟前
安装支持ssl的harbor 2.1.4 docker 19.03.8 docker-compose 1.24.0
网络协议·ssl·harbor
软件技术员17 分钟前
Let‘s Encrypt SSL证书:acmessl.cn申请免费3个月证书
服务器·网络协议·ssl
耗同学一米八30 分钟前
2024 年河北省职业院校技能大赛网络建设与运维赛项样题四
运维·网络
速盾cdn1 小时前
速盾:CDN缓存的工作原理是什么?
网络·安全·web安全
网络安全-杰克2 小时前
网络安全概论
网络·web安全·php
怀澈1222 小时前
高性能服务器模型之Reactor(单线程版本)
linux·服务器·网络·c++
耗同学一米八2 小时前
2024 年河北省职业院校技能大赛网络建设与运维赛项样题二
运维·网络·mariadb
skywalk81633 小时前
树莓派2 安装raspberry os 并修改成固定ip
linux·服务器·网络·debian·树莓派·raspberry
_不会dp不改名_3 小时前
HCIA笔记3--TCP-UDP-交换机工作原理
笔记·tcp/ip·udp