问题描述
我需要下面的代码在完成与服务器的通信后发送 FIN/ACK。我之前使用过套接字,您明确告诉它关闭连接,此时 NIC 发送 FIN/ACK。在这里我没有那种控制,并且很好奇是否有办法强制 FIN/ACK。当我终止程序时,会发送 FIN/ACK。
我尝试解构保持 TCP 连接打开的对象,但我不确定它是否能正常工作。我试过把它放在自己的线程上,并将其设置为无效。
要在 Visual Studio 中运行此代码,您需要添加服务器引用 (https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl) 并将其命名为“Onvif_Video_Player”。
using System;
using System.Text;
using System.Windows.Forms;
using Onvif_Video_Player.OnvifDevice;
using System.ServiceModel.Channels;
using System.ServiceModel;
namespace Onvif_Video_Player
{
public partial class Form1 : Form
{
//Form1 Interactive Objecst
string IPAddress;
String userName;
string password;
public Form1()
{
InitializeComponent();
}
private void GetProfiles_Click(object sender,EventArgs e)
{
IPAddress = txtBox_IPAddress.Text;
userName = TxtBox_UserName.Text;
password = TxtBox_Password.Text;
GetonvifProfiles();
while (true)
{
//Do other stuff
}
}
private void GetonvifProfiles()
{
UriBuilder deviceUri;
System.ServiceModel.Channels.CustomBinding binding;
deviceUri = new UriBuilder("Http:/onvif/device_service");
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Digest;
deviceUri.Host = IPAddress;
var messageElement = new TextMessageEncodingBindingElement();
messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.soap12,AddressingVersion.None);
System.ServiceModel.Channels.CustomBinding customBinding = new System.ServiceModel.Channels.CustomBinding(messageElement,httpTransport);
binding = new System.ServiceModel.Channels.CustomBinding(new TextMessageEncodingBindingElement(MessageVersion.soap12,Encoding.UTF8),httpTransport);
DeviceClient Onvif_Device = new DeviceClient(binding,new EndpointAddress(deviceUri.ToString()));
OnvifDevice.Service[] service = Onvif_Device.GetServices(false);
///////////////////////////Send FIN/ACK//////////////////////////
}
}
}
另一个想法是,如果我能弄清楚这个 TCP 连接是在哪个端口上打开的,我可以手动构造一个字符串来在该端口上发送一个 FIN/ACK。
如下推荐,我尝试使用。也许我做错了:我首先将创建 TCP 连接的函数移动到公共类,并让该类从 Idisposable 继承。
类函数....
namespace Onvif_Video_Player
{
public class CustomBindingsAndClients : Idisposable
{
void Idisposable.dispose()
{
int i = 0;
}
public void GetProfiles(string IP,string IuserName,string password)
{
UriBuilder deviceUri = new UriBuilder("Http:/onvif/device_service");
System.ServiceModel.Channels.CustomBinding binding;
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Digest;
deviceUri.Host = "192.168.1.65";
var messageElement = new TextMessageEncodingBindingElement();
messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.soap12,new EndpointAddress(deviceUri.ToString()));
OnvifDevice.Service[] service = Onvif_Device.GetServices(true);
binding = null;
}
}
}
然后在代码的主体中将实例包装在 using 语句中...
private void GetProfiles_Click(object sender,EventArgs e)
{
IPAddress = txtBox_IPAddress.Text;
userName = TxtBox_UserName.Text;
password = TxtBox_Password.Text;
using (CustomBindingsAndClients TEMP = new CustomBindingsAndClients())
{
TEMP.GetProfiles(IPAddress,userName,password);
}
}
TCP 连接仍然保持打开状态,直到应用程序关闭。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)