断开某tcp连接

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


//********************************************************************** 
// Version: V1.0 
// Coder: WinEggDrop 
// Date Release: NULL 
// Purpose: Kill An Active TCP Connection 
// Test PlatForm: Win 2K Pro And Server SP4 
// Compiled On: LCC 3.0,May Compile On VC++ 6.0(Not Test Yet) 
//********************************************************************** 

#include <WinSock2.h>
#include <windows.h> 
#include <stdio.h> 
#include <iphlpapi.h> 


#pragma comment (lib, "iphlpapi.lib")
#pragma comment (lib, "ws2_32.lib")


// Function ProtoType Declaration 
//--------------------------------------------------------------------------------------------------------------------------- 
BOOL KillTCPConnection(const char *LocalAddress,const char *LocalPort,const char *RemoteAddress,const char *RemotePort); 
BOOL IsDigits(const char *String); 
void Usage(const char *Command); 
//--------------------------------------------------------------------------------------------------------------------------- 
// End Of Fucntion ProtoType Declaration 

// Main Function 
int main(int argc,char *argv[]) 
{ 
	if (argc!=5)     // There Are Not 5 Arguement In Total 
	{ 
		Usage(argv[0]);     // Display The Usage 
		return -1;    // Quit The Program 
	} 

	KillTCPConnection(argv[1],argv[2],argv[3],argv[4]);     // Kill The TCP Connection 
	return 0;     // Quit The Program 
}// End Of Main Function 

//------------------------------------------------------------------------- 
// Purpose: To Display The Usage Of The Program 
// Return Type: Void 
// Parameters:  Const Char *Command 
//------------------------------------------------------------------------- 
void Usage(const char *Command) 
{ 
	printf("\r\nUsage: %s LocalAddress LocalPort RemoteAddress,RemotePort\r\n",Command);     // Display The Usage 
	printf("Example: %s 192.168.0.1 1234 12.12.12.12 22222\r\n",Command);     // Display A Example 
}// End Of Usage Function 

//------------------------------------------------------------------------- 
// Purpose: To Check Whether The String Is Really A Number 
// Return Type: Boolean 
// Parameters:  Const Char *String 
//------------------------------------------------------------------------- 
BOOL IsDigits(const char *String) 
{ 
	int StringLength = strlen(String);    // Get The Length Of The String 

	for (int i = 0;i < StringLength;i++)     // Check Every Character Of The String 
	{ 
		if (String[i] < 48 || String[i] > 57)    // The Character Is Not A Digit 
		{ 
			return FALSE;    // Return False 
		} 
	} 
	return TRUE;     // Return True As All Characters Are Digits 
}// End Of IsDigits Function 

//-------------------------------------------------------------------------------------------- 
// Purpose: To Kill An Active TCP Connection 
// Return Type: Boolean 
// Parameters: 
//            1.Const Char *LocalAddress  --> The Local Address Of The TCP Connection
//            2.Const Char *LocalPort     --> The Local Port Of The TCP Connection 
//            3.Const Char *RemoteAddress --> The Remote Address Of The TCP Connection 
//            4.Const Char *RemotePort    --> The Remote Port Of The TCP Connection 
//-------------------------------------------------------------------------------------------- 
BOOL KillTCPConnection(const char *LocalAddress,const char *LocalPort,const char *RemoteAddress,const char *RemotePort) 
{ 
	MIB_TCPROW  TcpRow;    // Declare A TCP Raw 

	if (!IsDigits(LocalPort))    // The Local Port Is Not A Number 
	{ 
		printf("Invalid Local Port\r\n");     // Display Error Message 
		return FALSE;    // Return False 
	} 

	if (atoi(LocalPort) < 0 || atoi(LocalPort) > 65535)     // The Local Port Is Out Of Bound 
	{ 
		printf("Local Port Out Of Bound\r\n");      // Display Error Message 
		return FALSE;    // Return False 
	} 

	if (!IsDigits(RemotePort))      // The Remote Port Is Not A Number 
	{ 
		printf("Invalid Remote Port\r\n");    // Display Error Message 
		return FALSE;    // Return False 
	} 

	if (atoi(RemotePort) < 0 || atoi(RemotePort) > 65535)      // The Remote Port Is Out Of Bound 
	{ 
		printf("Remote Port Out Of Bound\r\n");     // Display Error Message 
		return FALSE;    // Return False 
	} 

	// Set The TCP Row Entry 
	TcpRow.dwLocalPort = htons(atoi(LocalPort)); 
	TcpRow.dwRemotePort = htons(atoi(RemotePort)); 
	TcpRow.dwLocalAddr = inet_addr(LocalAddress); 
	TcpRow.dwRemoteAddr = inet_addr(RemoteAddress); 
	TcpRow.dwState = MIB_TCP_STATE_DELETE_TCB;     // Flag To Indicate The System To End That TCP Connection 

	if (SetTcpEntry(&TcpRow) == NO_ERROR)    // Call The API With No Error 
	{ 
	    printf("Delete The TCP Connection %s:%s-->%s:%s Successfully\r\n",LocalAddress,LocalPort,RemoteAddress,RemotePort);    // Display Successful Message 
	    return TRUE;     // Return True 
	} 

	// Some Error Must Be Occurred 
	printf("Fail To Delete The TCP Connection Error Code:%d\r\n",GetLastError());      // Display The Error Code 
	return FALSE;    // Return False 
}// End Of KillTCPConnection Function 
// End Of File

关键函数 settcpentry


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值