Only one usage of each socket address (protocol/ne

2020-05-26 10:03发布

I've been looking for a serious solution on google and i only get "Regisrty solutions" kind of stuff which i don't think even relate to my problem.

For some reason i get this Error, while i'm only starting the TcpListner once, and when+if fails i stop the server. I really don't get it. Here is my code:

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);
            }
        }

I will really appreciate your help!

2条回答
Emotional °昔
2楼-- · 2020-05-26 10:26
  1. Open cmd
  2. Type netstat –ano
  3. List of process with their ports will be opened
  4. Search ‘process ID’ of the port you are unable to use (in my case port 11020)
  5. Open task Manager and Stop that process
  6. Now your port is ready to use :)
查看更多
Juvenile、少年°
3楼-- · 2020-05-26 10:36
  1. I opened CMD and typed in : netstat -a
  2. I took a look in the Local Address column.
  3. I took a look at the port portion.
  4. I saw that the port in my program is already active( in use ) in another program.
  5. I changed my port in my program to something else.

    It Worked!

    Big thanks to: @DavidSchwartz, @Gusman

查看更多
登录 后发表回答