C# Socket 关闭服务,如何关闭当前已经连接的socket
RT。采用C#
求解决方案
目前server的socket主动close之后,listening没了。但是还有2个ES状态。
说明服务器只是关闭了监听,但是客户端的连接没有主动close。
--------------------编程问答-------------------- 在关闭窗体的时候就要判断是否连接,如果连接,关闭服务 --------------------编程问答-------------------- 做个心跳机制,客户端隔断时间连接下服务器。 --------------------编程问答--------------------
private bool Connect_Utils()
{
bool flag = true;
try
{
if (serverProxy == null || serverProxy.State != CommunicationState.Opened)
{
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
binding.TransferMode = TransferMode.Buffered;
binding.TransactionFlow = false;
binding.TransactionProtocol = TransactionProtocol.OleTransactions;
binding.MaxBufferPoolSize = 2147483647;
binding.MaxBufferSize = 2147483647;
binding.MaxConnections = 32;
binding.MaxReceivedMessageSize = 2147483647;
binding.ReaderQuotas.MaxArrayLength = 2147483647;
string addr = "net.tcp://192.168.1.2:9000/server";
serverProxy = new ServerProxy(new InstanceContext(this), binding, new EndpointAddress(addr));
serverProxy.Open();//打开服务
flag = true;
}
}
catch (Exception Ex)
{
MessageBox.Show("连接服务器时发生错误" + Ex.Message, "连接提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
flag = false;
}
return flag;
}
以上为连接服务
以下为关闭服务
--------------------编程问答-------------------- http://blog.csdn.net/fengyarongaa/article/details/6631953 --------------------编程问答-------------------- 可能我描述的有问题或不太清楚。
if(serverProxy == null || serverProxy.State != CommunicationState.Opened)
{
serverProxy.Close();
}
我的意思是我是server端。
Listen之后,一直在等待客户端连接(同步短连接)
现在服务器在运行,我想手工停止server服务器,现在的程序是listen是停止了,客户端肯定是连接不上来了。
但是之前已经连接的客户端,只要客户端没主动关闭,还是可以直接发送接收数据,因为连接的状态为ESTABLISHED状态。
我的问题是服务器主动关闭后,要求lister停止,并关闭所有已经连接的客户端(服务器主动关闭客户端的连接)
补充:.NET技术 , C#