System.Net.Sockets.SocketException: 通常每个套接字地址(协议/网络
点击右边红色标题查看本文完整版:System.Net.Sockets.SocketException: 通常每个套接字地址(协议/网络 地址 端口)只允许使用一次
未处理的异常: System.Net.Sockets.SocketException: 通常每个套接字地址(协议网络 地址 端口)只允许使用一次。
................................................................................
例子代码如下
C:\mo>csc /r:mscorlib.dll /r:system.dll /out:c:\gethostname.exe /t:exe c:\gethostname.cs
................................................................................
using System;
using System.Net;
using System.Net.Sockets;
public class App
{
public static void Main()
{
Socket socket = new Socket
(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp
);
String strHostName = null;
String strHostAddress = null;
IPAddress ipHost = null;
IPEndPoint ipEP = new IPEndPoint
(
ipHost = Dns.GetHostByName( strHostName = System.Net.Dns.GetHostName() ).AddressList[ 0 ],
80
);
try
{
socket.Bind( ipEP );
}
catch( Exception ex )
{
Console.WriteLine( "Bind error: {0}", ex.Message );
return;
}
Console.WriteLine( "Host name is {0}", strHostName );
Console.WriteLine( "Host address is {0}", strHostAddress = ipHost.ToString() );
Console.WriteLine( "Maximum allowed Port Address is {0}", IPEndPoint.MaxPort );
Console.WriteLine( "Minimum allowed Port Address is {0}", IPEndPoint.MinPort );