C# 多网卡 Server Listen

来源:互联网 发布:浙江大学网络认证 编辑:程序博客网 时间:2024/06/18 10:28
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

时间: 2004/5/20
作者: Robert
参考: MSDN
电邮: zsc771120@yahoo.com.cn
关键词: TcpListener IPEndPoint IPAddress IPAddress.Any
目的: 帮助受监听多网卡IP地址困扰的朋友

 

VCBCB中做一个Server的监听程序,只需要指定端口,然后监听(Listen)就行了.C#找不到这个函数了,慢慢看MSDN,怎么需要指定IPPort才能监听,那么多网卡的机器应该怎么席程序呢?下面的程序可以解释怎么去做.

 

TcpListener 类别会提供简易的方法,用以在封锁的同步模式中聆听 (Listen) 和接受输入的连接要求。您可以使用 TcpClient Socket 来连接 TcpListener 。使用 IPEndPoint 、本机 IP 地址和连接端口编号,或者就只用连接埠编号来建立 TcpListener 。如果您想要基础服务供货商为您指派那些值,请指定 Any 给本机 IP 地址和 0 给本机连接埠编号。如果选择要这样做,您可以使用 Listenerclasslocalendpointtopic.htm">LocalEndpoint 来识别指派的信息。

使用 Listenerclassstarttopic.htm">Start 方法开始聆听输入的连接要求。 Start 将会让输入的连接进入队列等候,一直到不是呼叫了 Listenerclassstoptopic.htm">Stop 方法,就是已经将 MaxConnections 排入队列为止。使用 Listenerclassacceptsockettopic.htm">AcceptSocket Listenerclassaccepttcpclienttopic.htm">AcceptTcpClient ,从输入的连接要求队列取出连接。这两个方法将会封锁。如果想要避免封锁,您可以先使用 Listenerclasspendingtopic.htm">Pending 方法来判断连接要求是否可在队列中取得。

呼叫 Stop 方法关闭 TcpListener

 

这个建构函式可以让您指定要聆听输入连接尝试的本机 IP 地址和连接端口编号。使用这个建构函式之前,您必须使用所需的本机 IP 地址和连接端口编号来建立 IPEndPoint 。将这个 IPEndPoint 当作 localEP 参数传递给建构函式。

如果您不在乎要指派哪个本机地址,则可使用 IPAddress.Any 做为地址参数以建立 IPEndPoint ,而基础服务供货商将会指派最适当的网络地址。如果您有多个网络接口,这可能有助于简化应用程序。如果您不在乎要使用哪个本机连接埠,则可以指定 0 做为连接埠编号来建立 IPEndPoint 。在这种情况下,服务供货商将会指派介于 1024 5000 之间的可用连接埠编号。如果您使用这个途径,则可以藉由使用 Listenerclasslocalendpointtopic.htm">LocalEndpoint 属性来探索什么局域网络地址和连接端口编号已经被指派。

呼叫 Listenerclassstarttopic.htm">Start 方法以开始聆听输入的连接尝试。

 

IPEndPoint 类别包含主机以及应用程序连接到主机服务所需的通讯端口信息。藉由结合主机的 IP 地址和服务的通讯端口编号, IPEndPoint 类别形成连接至服务的连接点 (Connection Point)

 

[C#]

//Creates an instance of the TcpListener class by providing a local endpoint.

 

IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];

IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);

 

try{

    TcpListener tcpListener = new TcpListener(ipLocalEndPoint);

}

catch ( Exception e ){

    Console.WriteLine( e.ToString());

}

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击