这只是许多可能的实现方式之一,也应该制定一个如何建立一个“翻译”,然后一个“TranslatedConnection”。显然(或不这样),可以在基类和接口内概述发送和接收方法,以允许跨网络通信(如果需要的话)。
类'连接'为任何类型的连接定义了一个合适的基础。 'NetworkConnection'增加了对网络的要求,最后是'IPNetworkConnection'来实现IP协议所需的逻辑。这也可以扩展到'TcpNetworkConnection',然后扩展到'TcpIpNetworkConnection',或者以任何其他想要的方式,例如'SerialConnection'或'EthernetConnection',然后建立一个类以允许跨媒体通信。 'SerialToEthernetNetworkConnection'等。
例如:
#region Copyright
/*
This file came from Managed Media Aggregation, You can always find the latest version @ https://net7mma.codeplex.com/
[email protected]/(SR. Software Engineer ASTI Transportation Inc. http://www.asti-trans.com)
Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction,
* including without limitation the rights to :
* use,
* copy,
* modify,
* merge,
* publish,
* distribute,
* sublicense,
* and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
*
* [email protected] should be contacted for further details.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE,
* ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* v//
*/
#endregion
namespace Media.Sockets
{
#region TcpNetworkConnection
public class TcpNetworkConnection : NetworkConnection
{
#region Statics
public static System.Net.NetworkInformation.TcpConnectionInformation[] TcpConnectionInformation
{
get { return IPNetworkConnection.IPGlobalProperties.GetActiveTcpConnections(); }
}
#endregion
public TcpNetworkConnection(string name, bool shouldDispose) : base(name, shouldDispose) { }
}
#endregion
}
#region Copyright
/*
This file came from Managed Media Aggregation, You can always find the latest version @ https://net7mma.codeplex.com/
[email protected]/(SR. Software Engineer ASTI Transportation Inc. http://www.asti-trans.com)
Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction,
* including without limitation the rights to :
* use,
* copy,
* modify,
* merge,
* publish,
* distribute,
* sublicense,
* and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
*
* [email protected] should be contacted for further details.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE,
* ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* v//
*/
#endregion
namespace Media.Sockets
{
#region TcpNetworkConnection
public class TcpIPNetworkConnection : IPNetworkConnection
{
#region Statics
public static System.Net.NetworkInformation.TcpConnectionInformation[] TcpConnectionInformation
{
get { return IPNetworkConnection.IPGlobalProperties.GetActiveTcpConnections(); }
}
#endregion
public TcpIPNetworkConnection() : base(string.Empty) { }
}
#endregion
}
都是有效的实现,但用于不同的目的。
在“通常”中,是指执行依赖,还是进行正确的关闭/关闭调用?因为我特别想在客户端应用程序未正常关闭时终止提示。在程序突然终止时套接字关闭是否与显式系统调用相同? – ShayPal5
我的意思是“通常”是指远程对等设备正常关机的意义。我用过的每个Berkeley套接字实现都采用相同的方式。导致异常关闭的一种方法是关闭套接字,同时仍然有数据留在缓冲区中读取。取决于许多因素,您可能或可能无法依赖套接字层来确定客户端是否被终止。使用应用程序级别的消息可能会更好(例如,类似于SMTP“QUIT”命令)。 –
谢谢!进一步的研究表明,我使用的套接字实现的行为也是这样,所以我现在可以指望在readfds中弹出的封闭套接字。你的回答真的帮了我很大的忙! – ShayPal5