我一直在谷歌上寻找一个严肃的解决方案,但我只得到“注册表解决方案”之类的东西,我认为这些东西与我的问题无关。
由于某种原因,我收到此错误,而我只启动
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);
}
}
netstat -a
成功了!
非常感谢:@DavidSchwartz、@Gusman
选项1
netstat -ano | findstr ":80"
- 其中“80”是您要搜索的端口号。taskkill /PID <PID> /F
(其中 <PID>
是需要杀死的 PID)。选项2
如果上述选项 1 不起作用,请尝试重新启动计算机。
为了找到进程,使用PowerShell很容易:
$theCulpritPort="8001"
Get-NetTCPConnection -LocalPort $theCulpritPort `
| Select-Object -Property "OwningProcess", @{'Name' = 'ProcessName';'Expression'={(Get-Process -Id $_.OwningProcess).Name}} `
| Get-Unique
继续前进,如果您需要终止进程,请在最后通过管道传输停止进程:
$theCulpritPort="8001"
Get-NetTCPConnection -LocalPort $theCulpritPort -ErrorAction Ignore `
| Select-Object -Property @{'Name' = 'ProcessName';'Expression'={(Get-Process -Id $_.OwningProcess).Name}} `
| Get-Unique `
| Stop-Process -Name {$_.ProcessName} -Force
就我而言,问题是 appsetting.json 中的
Endpoints
配置:
"Kestrel": {
"Endpoints": {
//"Http": {
// "Url": "http://0.0.0.0:9446"
//},
"HttpsInlineCertFile": {
"Url": "https://0.0.0.0:9446",
"Certificate": {
"Location": "LocalMachine",
"Path": "....pfx"
}
}
},
"Certificates": {
"Default": {
"Path": "....pfx"
}
}
}
一旦我注释掉了
Http
部分(如上所示),问题就消失了。