Reset TCP connections on Windows

发布于:2025-07-21 ⋅ 阅读:(17) ⋅ 点赞:(0)

Reset a pair of TCP connections on Windows

TcpReset.exe <local_ip> <local_port> <remote_ip> <remote_port>

🛠 Full CLI Utility Code

#include <winsock2.h>
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <ws2tcpip.h>

#pragma comment(lib, "iphlpapi.lib")  // IP Helper API
#pragma comment(lib, "ws2_32.lib")    // Winsock

#define _WINSOCK_DEPRECATED_NO_WARNINGS

// netstat -ano | findstr 10831
// TCP    192.168.1.9:10831      192.168.1.10 : 10831     FIN_WAIT_2      17312
void printUsage(const char* exeName) {
    printf("Usage: %s <local_ip> <local_port> <remote_ip> <remote_port>\n", exeName);
    printf("Example: %s 192.168.1.9 10831 192.168.1.10 10831\n", exeName);
}

int main(int argc, char* argv[]) {
    if (argc != 5) {
        printUsage(argv[0]);
        return 1;
    }

    const char* localIP = argv[1];
    int localPort = atoi(argv[2]);
    const char* remoteIP = argv[3];
    int remotePort = atoi(argv[4]);

    // Initialize Winsock
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        fprintf(stderr, "WSAStartup failed.\n");
        return 1;
    }

    MIB_TCPROW row;
    ZeroMemory(&row, sizeof(row));

    row.dwState = MIB_TCP_STATE_DELETE_TCB; // Force reset

    if (inet_pton(AF_INET, localIP, &row.dwLocalAddr) != 1) {
        fprintf(stderr, "Invalid local IP address: %s\n", localIP);
        WSACleanup();
        return 1;
    }
    row.dwLocalPort = htons((u_short)localPort);

    if (inet_pton(AF_INET, remoteIP, &row.dwRemoteAddr) != 1) {
        fprintf(stderr, "Invalid remote IP address: %s\n", remoteIP);
        WSACleanup();
        return 1;
    }
    row.dwRemotePort = htons((u_short)remotePort);

    DWORD res = SetTcpEntry(&row);
    if (res == NO_ERROR) {
        printf("Connection %s:%d -> %s:%d reset successfully.\n",
            localIP, localPort, remoteIP, remotePort);
    }
    else {
        fprintf(stderr, "Failed to reset connection. Error: %lu\n", res);
    }

    WSACleanup();
    return 0;
}


✅ Compile

cl /EHsc TcpReset.c iphlpapi.lib ws2_32.lib

This produces TcpReset.exe. Place it somewhere in your PATH for easy use.


🖥 How to use

Reset a specific connection:

TcpReset.exe 192.168.1.9 10831 192.168.1.10 10831

If you give the wrong IP/port, it will validate and warn you.


🚀 Optional: Batch reset mode

We can also extend it so you can call:

TcpReset.exe -p 10831

to find and reset all connections on port 10831 automatically (like netstat | findstr 10831 but with reset).

TcpSend


网站公告

今日签到

点亮在社区的每一天
去签到