通常只允许使用每个套接字地址(协议/网络地址/端口)?

Wei*_*rce 8 c# sockets tcp tcplistener socketexception

我一直在谷歌寻找一个严肃的解决方案,我只得到"Regisrty解决方案"的东西,我不认为甚至与我的问题有关.

由于某种原因,我得到这个错误,而我只启动一次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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

我将非常感谢你的帮助!

Wei*_*rce 14

  1. 我打开CMD并输入:netstat -a
  2. 我看了一下Local Address列.
  3. 我看了一下端口部分.
  4. 我看到程序中的端口已在另一个程序中处于活动状态(正在使用中).
  5. 我将程序中的端口更改为其他内容.

    有效!

    非常感谢:@DavidSchwartz,@ Gusman

  • 我知道这是一个旧答案,但在设备已经指向端口的生产环境中,在代码中切换端口并不容易,因为这会破坏功能。另一个答案更好。 (2认同)

the*_*ude 9

选项1

  1. 打开命令提示符。
  2. 类型netstat -ano | findstr ":80"- 其中“80”是您要搜索的端口号。
  3. 查看结果中的最后一列 - PID。
  4. 对于要杀死的每个正在运行的 PID,请taskkill /PID <PID> /F在命令提示符窗口中执行(<PID>需要杀死的 PID在哪里)。

选项 2

如果上面的选项 1 不起作用,请尝试重新启动您的机器。


小智 8

  1. 打开cmd
  2. 输入netstat –ano
  3. 将打开带有其端口的进程列表
  4. 搜索您无法使用的端口的“进程ID”(在我的情况下为端口11020)
  5. 打开任务管理器并停止该过程
  6. 现在您的端口可以使用了:)


Mir*_*tei 7

为了找到进程,使用 PowerShell 很容易:

$theCulpritPort="8001"
Get-NetTCPConnection -LocalPort $theCulpritPort `
| Select-Object -Property "OwningProcess", @{'Name' = 'ProcessName';'Expression'={(Get-Process -Id $_.OwningProcess).Name}} `
| Get-Unique
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

展望未来,如果您需要终止进程,请在最后通过管道传输 Stop-Process:

$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
Run Code Online (Sandbox Code Playgroud)