首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

System.Net.Sockets.SocketException: 通常每个套接字地址(协议/网络 地址 端口)只允许使用一次

2014-01-28 
未处理的异常: 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 );         

热点排行