如何终止Websocket连接?
如何终止Websocket连接?我不是说关闭两端的连接,而是在“中间”中断连接。我需要测试一些必须在重新连接时发生的应用程序逻辑(通过SocketIO处理)如何终止Websocket连接?,websocket,Websocket,如何终止Websocket连接?我不是说关闭两端的连接,而是在“中间”中断连接。我需要测试一些必须在重新连接时发生的应用程序逻辑(通过SocketIO处理) 不,拔掉网络电缆并不重要,因为我无法在单元测试中真正实现自动化:-)此外,我只想切断一个特定连接,而不是全部连接。这很痛苦,但这是可能的。你可以找到一个解释。代码如下: using System; using System.Collections; using System.Runtime.InteropServices; /// <
不,拔掉网络电缆并不重要,因为我无法在单元测试中真正实现自动化:-)此外,我只想切断一个特定连接,而不是全部连接。这很痛苦,但这是可能的。你可以找到一个解释。代码如下:
using System;
using System.Collections;
using System.Runtime.InteropServices;
/// <summary>
/// This is a class for disconnecting TCP connections.
/// You can get a list of all connections and close by a connection, localIP,
/// remoteIP, localPort and remotePort.
/// </summary>
public class Disconnecter {
//Enumeration of the states
public enum State{
All=0,
Closed=1,
Listen=2,
Syn_Sent=3,
Syn_Rcvd=4,
Established=5,
Fin_Wait1=6,
Fin_Wait2=7,
Close_Wait=8,
Closing=9,
Last_Ack=10,
Time_Wait=11,
Delete_TCB=12
}
//Connection info
private struct MIB_TCPROW{
public int dwState;
public int dwLocalAddr;
public int dwLocalPort;
public int dwRemoteAddr;
public int dwRemotePort;
}
//API to get list of connections
[DllImport("iphlpapi.dll")]
private static extern int GetTcpTable(IntPtr pTcpTable,ref int pdwSize,bool bOrder);
//API to change status of connection
[DllImport("iphlpapi.dll")]
//private static extern int SetTcpEntry(MIB_TCPROW tcprow);
private static extern int SetTcpEntry(IntPtr pTcprow);
//Convert 16-bit value from network to host byte order
[DllImport("wsock32.dll")]
private static extern int ntohs(int netshort);
//Convert 16-bit value back again
[DllImport("wsock32.dll")]
private static extern int htons(int netshort);
//Close all connection to the remote IP
public static void CloseRemoteIP(string IP){
MIB_TCPROW[] rows = getTcpTable();
for(int i=0; i<rows.Length; i++){
if(rows[i].dwRemoteAddr==IPStringToInt(IP)){
rows[i].dwState = (int) State.Delete_TCB;
IntPtr ptr = GetPtrToNewObject(rows[i]);
int ret = SetTcpEntry(ptr);
}
}
}
//Close all connections at current local IP
public static void CloseLocalIP(string IP){
MIB_TCPROW[] rows = getTcpTable();
for(int i=0; i<rows.Length; i++){
if(rows[i].dwLocalAddr==IPStringToInt(IP)){
rows[i].dwState = (int) State.Delete_TCB;
IntPtr ptr = GetPtrToNewObject(rows[i]);
int ret = SetTcpEntry(ptr);
}
}
}
//Closes all connections to the remote port
public static void CloseRemotePort(int port){
MIB_TCPROW[] rows = getTcpTable();
for(int i=0; i<rows.Length; i++){
if(port==ntohs(rows[i].dwRemotePort)){
rows[i].dwState = (int) State.Delete_TCB;
IntPtr ptr = GetPtrToNewObject(rows[i]);
int ret = SetTcpEntry(ptr);
}
}
}
//Closes all connections to the local port
public static void CloseLocalPort(int port){
MIB_TCPROW[] rows = getTcpTable();
for(int i=0; i<rows.Length; i++){
if(port==ntohs(rows[i].dwLocalPort)){
rows[i].dwState = (int) State.Delete_TCB;
IntPtr ptr = GetPtrToNewObject(rows[i]);
int ret = SetTcpEntry(ptr);
}
}
}
//Close a connection by returning the connectionstring
public static void CloseConnection(string connectionstring){
try{
//Split the string to its subparts
string[] parts = connectionstring.Split('-');
if(parts.Length!=4) throw new Exception("Invalid connectionstring - use the one provided by Connections.");
string[] loc = parts[0].Split(':');
string[] rem = parts[1].Split(':');
string[] locaddr = loc[0].Split('.');
string[] remaddr = rem[0].Split('.');
//Fill structure with data
MIB_TCPROW row = new MIB_TCPROW();
row.dwState = 12;
byte[] bLocAddr = new byte[]{byte.Parse(locaddr[0]),byte.Parse(locaddr[1]),byte.Parse(locaddr[2]),byte.Parse(locaddr[3])};
byte[] bRemAddr = new byte[]{byte.Parse(remaddr[0]),byte.Parse(remaddr[1]),byte.Parse(remaddr[2]),byte.Parse(remaddr[3])};
row.dwLocalAddr = BitConverter.ToInt32(bLocAddr,0);
row.dwRemoteAddr = BitConverter.ToInt32(bRemAddr,0);
row.dwLocalPort = htons(int.Parse(loc[1]));
row.dwRemotePort = htons(int.Parse(rem[1]));
//Make copy of the structure into memory and use the pointer to call SetTcpEntry
IntPtr ptr = GetPtrToNewObject(row);
int ret = SetTcpEntry(ptr);
if(ret==-1) throw new Exception("Unsuccessful");
if(ret==65) throw new Exception("User has no sufficient privilege to execute this API successfully");
if(ret==87) throw new Exception("Specified port is not in state to be closed down");
if(ret!=0) throw new Exception("Unknown error ("+ret+")");
}catch(Exception ex){
throw new Exception("CloseConnection failed ("+connectionstring+")! ["+ex.GetType().ToString()+","+ex.Message+"]");
}
}
//Gets all connections
public static string[] Connections(){
return Connections(State.All);
}
//Gets a connection list of connections with a defined state
public static string[] Connections(State state){
MIB_TCPROW[] rows = getTcpTable();
ArrayList arr = new ArrayList();
foreach(MIB_TCPROW row in rows){
if(state == State.All || state == (State)row.dwState){
string localaddress = IPIntToString(row.dwLocalAddr) +":"+ ntohs(row.dwLocalPort);
string remoteaddress = IPIntToString(row.dwRemoteAddr) + ":" + ntohs(row.dwRemotePort);
arr.Add(localaddress + "-" + remoteaddress + "-" + ((State)row.dwState).ToString() + "-" + row.dwState);
}
}
return (string[])arr.ToArray(typeof(System.String));
}
//The function that fills the MIB_TCPROW array with connectioninfos
private static MIB_TCPROW[] getTcpTable(){
IntPtr buffer = IntPtr.Zero; bool allocated=false;
try{
int iBytes=0;
GetTcpTable(IntPtr.Zero, ref iBytes, false); //Getting size of return data
buffer=Marshal.AllocCoTaskMem(iBytes); //allocating the datasize
allocated=true;
GetTcpTable(buffer, ref iBytes, false); //Run it again to fill the memory with the data
int structCount=Marshal.ReadInt32(buffer); // Get the number of structures
IntPtr buffSubPointer = buffer; //Making a pointer that will point into the buffer
buffSubPointer = (IntPtr)((int)buffer + 4); //Move to the first data (ignoring dwNumEntries from the original MIB_TCPTABLE struct)
MIB_TCPROW[] tcpRows = new MIB_TCPROW[structCount]; //Declaring the array
//Get the struct size
MIB_TCPROW tmp = new MIB_TCPROW();
int sizeOfTCPROW = Marshal.SizeOf(tmp);
//Fill the array 1 by 1
for(int i=0; i<structCount; i++){
tcpRows[i] = (MIB_TCPROW)Marshal.PtrToStructure(buffSubPointer, typeof(MIB_TCPROW)); //copy struct data
buffSubPointer = (IntPtr)((int)buffSubPointer + sizeOfTCPROW ); //move to next structdata
}
return tcpRows;
}catch(Exception ex){
throw new Exception("getTcpTable failed! ["+ex.GetType().ToString()+","+ex.Message+"]");
}finally{
if(allocated) Marshal.FreeCoTaskMem(buffer); //Free the allocated memory
}
}
private static IntPtr GetPtrToNewObject(object obj){
IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(obj));
Marshal.StructureToPtr(obj,ptr,false);
return ptr;
}
//Convert an IP string to the INT value
private static int IPStringToInt(string IP){
if(IP.IndexOf(".")<0) throw new Exception("Invalid IP address");
string[] addr = IP.Split('.');
if(addr.Length!=4) throw new Exception("Invalid IP address");
byte[] bytes = new byte[]{byte.Parse(addr[0]),byte.Parse(addr[1]),byte.Parse(addr[2]),byte.Parse(addr[3])};
return BitConverter.ToInt32(bytes,0);
}
//Convert an IP integer to IP string
private static string IPIntToString(int IP){
byte[] addr = System.BitConverter.GetBytes(IP);
return addr[0] +"."+addr[1] +"."+addr[2] +"."+addr[3];
}
}
使用系统;
使用系统集合;
使用System.Runtime.InteropServices;
///
///这是一个用于断开TCP连接的类。
///您可以获得所有连接的列表,并通过连接localIP关闭,
///remoteIP、localPort和remotePort。
///
公共类断开器{
//州的列举
公共枚举状态{
全部=0,
闭合=1,
听=2,
同步发送=3,
Syn_Rcvd=4,
已建立=5,
Fin_Wait1=6,
Fin_Wait2=7,
关闭\u等待=8,
收盘价=9,
最后确认=10,
等待时间=11,
删除\u TCB=12
}
//连接信息
私有结构MIB_TCPROW{
公共国家;
公共int-dwLocalAddr;
公共int dwLocalPort;
公共int-dwRemoteAddr;
公共远程端口;
}
//获取连接列表的API
[DllImport(“iphlapi.dll”)]
私有静态extern int GetTcpTable(IntPtr pTcpTable,ref int pdwSize,bool bOrder);
//用于更改连接状态的API
[DllImport(“iphlapi.dll”)]
//专用静态外部设置程序(MIB_TCPROW TCPROW);
专用静态外部int SetTcpEntry(IntPtr pTcprow);
//将16位值从网络转换为主机字节顺序
[DllImport(“wsock32.dll”)]
私有静态外部intntohs(intnetshort);
//重新转换16位值
[DllImport(“wsock32.dll”)]
私有静态外部内部htons(内部netshort);
//关闭与远程IP的所有连接
公共静态void CloseRemoteIP(字符串IP){
MIB_TCPROW[]行=getTcpTable();
对于(int i=0;iWebSocket连接是否在浏览器中运行?如果是,您是否可以使用JavaScript获取WebSocket对象引用并调用websocketInstance.close();?这似乎比在较低的操作系统级别上执行要容易一些
然后,您可以使用类似的工具来控制浏览器实例并执行关闭WebSocket连接的JavaScript。也可以使用其他类似的库来执行此类操作。Windows,更可取。我现在使用Sysinternal的TCPView手动终止连接,但我仍在寻找一种方法来完成此程序简而言之。我希望我不必潜入shell脚本就可以做到这一点。对于手动测试,它可以终止连接。您应该提到这是C代码(至少在我看来它像C代码)。