1

I'm using C# with visual studio 2010 on windows 7
just trying a simple server/ client example to learn about socket programming for like 2 weeks now but then yesterday when I started debugging I got this exception (An attempt was made to access a socket in a way forbidden by its access permission). even though I've turned off the firewall and anti virus. plus checked the security tab and set full trust .

I read many articles saying we can't use TCP on windows 7 but it was working alright ! and it still works , I just need to restart the computer . but restarting every time I need to run is not a solution .

any ideas??

update:

static void Main(string [] args)
    {
        Program progDomain = new Program();
        progDomain.clients = new List<ClientManager>();

        if ( args.Length == 0 )
        {
            progDomain.serverPort = 8000;
            progDomain.serverIP = IPAddress.Any;
        }
        if ( args.Length == 1 )
        {
            progDomain.serverIP = IPAddress.Parse(args [0]);
            progDomain.serverPort = 8000;
        }
        if ( args.Length == 2 )
        {
            progDomain.serverIP = IPAddress.Parse(args [0]);
            progDomain.serverPort = int.Parse(args [1]);
        }

        progDomain.bwListener = new BackgroundWorker();
        progDomain.bwListener.WorkerSupportsCancellation = true;
        progDomain.bwListener.DoWork += new DoWorkEventHandler(progDomain.StartToListen);
        progDomain.bwListener.RunWorkerAsync();

        Console.WriteLine("*** Listening on port {0}{1}{2} started.Press ENTER to shutdown server. ***\n",progDomain.serverIP.ToString(),":",progDomain.serverPort.ToString());

        Console.ReadLine();

        progDomain.DisconnectServer();
    }

    private void StartToListen(object sender , DoWorkEventArgs e)
    {
        this.listenerSocket = new Socket(AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp);
        this.listenerSocket.Bind(new IPEndPoint(this.serverIP , this.serverPort));
        this.listenerSocket.Listen(200);
        while ( true )
            this.CreateNewClientManager(this.listenerSocket.Accept());
    }

here's the piece of code where the exception fires. nothing much since it fires right from the start.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
Katia
  • 679
  • 2
  • 15
  • 42
  • Can you provide the code you're using? – M.Babcock Dec 18 '11 at 21:49
  • You could also use the TcpListener class. – rekire Dec 18 '11 at 22:26
  • @rekire can you explain more why do I need TcpListener for this exception? or is it a general advice :) – Katia Dec 18 '11 at 22:38
  • What's the value of `args [0]`? Is it a C-style program name? Or is it a Ruby-style first "argument" of the program? – sarnold Dec 18 '11 at 22:51
  • Just general. I use that class and have not that problem. I can restart my tcp server so often I like without problems :-) @sarnold I don't know Ruby but indeed it is the first argument. – rekire Dec 18 '11 at 23:04
  • @sarnold it has no value because I'm running it with no parameters . if ( args.Length == 0 ) ... and use the default values for testing. – Katia Dec 18 '11 at 23:20
  • @rekire oh thank you . I guess there are still so many things to learn about socket programming . – Katia Dec 18 '11 at 23:21

1 Answers1

1

Like written in the comments check the TcpListener class. That class makes your life a littel easier.

rekire
  • 47,260
  • 30
  • 167
  • 264