【发布时间】:2017-06-09 17:24:34
【问题描述】:
我一直在谷歌上寻找一个严肃的解决方案,但我只得到“注册表解决方案”之类的东西,我认为这些东西甚至与我的问题无关。
由于某种原因,我收到此错误,而我只启动了一次 TcpListner,当它失败时,我会停止服务器。
这是我的代码:
class Program
{
private static string ServerName = "";
private static string UserName = "";
private static string Password = "";
private static string dbConnectionSring = "";
private static X509Certificate adminCertificate;
private static byte[] readBuffer = new byte[4096];
static void Main(string[] args)
{
Console.WriteLine("Please grant SQL Server access to the Admin Server:\n");
Console.Write("Server Name: ");
ServerName = Console.ReadLine();
Console.Write("\nUser Name: ");
UserName = Console.ReadLine();
Console.Write("\nPassword: ");
Password = PasswordMasker.Mask(Password);
dbConnectionSring = SQLServerAccess.CreateConnection(ServerName, UserName, Password);
adminCertificate = Certificate.GenerateOrImportCertificate("AdminCert.pfx", "randomPassword");
try
{
Console.WriteLine("Initializing server on the WildCard address on port 443...");
TcpListener listener = new TcpListener(IPAddress.Any, 443);
try
{
Console.WriteLine("Starting to listen at {0}: 443...", IPAddress.Any);
//the backlog is set to the maximum integer value, but the underlying network stack will reset this value to its internal maximum value
listener.Start(int.MaxValue);
Console.WriteLine("Listening... Waiting for a client to connect...");
int ConnectionCount = 0;
while (true)
{
try
{
listener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), listener);
ConnectionCount++;
Console.WriteLine(
" Accepted connection #" + ConnectionCount.ToString());
}
catch (SocketException err)
{
Console.WriteLine("Accept failed: {0}", err.Message);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Listening failed to start.");
listener.Stop();
Console.WriteLine(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine("Initialiazing server Failed.");
Console.WriteLine(ex.Message);
}
}
【问题讨论】:
-
问题是另一个程序已经在监听那个端口了...
-
您可能想尝试不同的端口或绑定到特定的本地 IP 地址,而不是全部。
-
@DavidSchwartz 我怎么知道哪些端口可以免费使用?
-
您是这台机器的管理员吗?如果是这样,您应该知道它正在运行哪些服务以及它们使用哪些端口。如果没有,您应该与管理这台机器上运行的服务的人员交谈。如果需要,您可以从
netstat -tan之类的命令开始盘点。 -
端口 443 是 HTTPS 的默认端口,因此您的机器上可能正在运行 http 服务器。
标签: c# sockets tcp tcplistener socketexception