Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only one usage of each socket address (protocol/network address/port) is normally permitted?

I've been looking for a serious solution on google and I only get "Registry 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 and if it fails, I stop the server.

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);             }         } 
like image 326
WeinForce Avatar asked Jan 24 '17 18:01

WeinForce


People also ask

What is socket address combination?

Socket address is the combination of an IP address and port number. The telephone connection is the combination of a phone number and a particular extension.


1 Answers

  1. Open CMD and type: netstat -a
  2. Take a look in the Local Address column.
  3. Look at the port portion.
  4. If the port in your program is already active(in use) in another program, you should use another port or kill the active process to make the port free.
  5. I changed my port in my program to something else.

It Worked!

Big thanks to: @DavidSchwartz, @Gusman

like image 155
WeinForce Avatar answered Sep 19 '22 07:09

WeinForce