SocketException driving me crazy:(

I'm new to c#.
When I try the following program (a very simple tcpserver) I get the following error msg..

Unhandled Exception: System.Net.Sockets.SocketException: An attempt was made to
access a socket in a way forbidden by its access permissions
at System.Net.Sockets.Socket.Listen(Int32 backlog)
at MainClass.Main()

This is the code..

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass
{
public static void Main()
{

IPEndPoint ip = new IPEndPoint(IPAddress.Any,9999);
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

socket.Bind(ip);
socket.Listen(10);
Console.WriteLine("Waiting for a client...");
Socket client = socket.Accept();
IPEndPoint clientep =(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",clientep.Address, clientep.Port);

string welcome = "Welcome";
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length,SocketFlags.None);

Console.WriteLine("Disconnected from {0}",clientep.Address);
client.Close();
socket.Close();
}
}

[1279 byte] By [SDan] at [2008-1-7]
# 1

Try running the code under Administrator privileged account

KarthikeyaPavanKumar.B at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2

thats what I did but it doesn't work. any ideas?
-Dan
SDan at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3
Did you try to add SocketPermission?

li4shi2 at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4

If you're running the program from a network drive (i do this because all my projects are saved on my server with a raid), go to the properties of the project, then to the "Security" tab and check "Enabled ClickOnce Security Settings". Make it a Full Trust application. Recompile and run again. .Net applications can't use things like sockets, serial ports etc if the application is run from a non-trusted location unless the appropriate security settings have been applied to the project.

AndrewMcNab at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 5
This is a Winsock error (its not anything to do with CAS (Code Access Security) permissions, that would produce the exception I've included at the bottom). So as always :-) we go the to Winsock documentation, and at Windows Sockets Error Codes (Windows) http://msdn2.microsoft.com/en-us/library/ms740668.aspx tt says:
WSAEACCES
10013

Permission denied.
An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for sendto without broadcast permission being set using setsockopt(SO_BROADCAST).

Another possible reason for the WSAEACCES error is that when the bind function is called (on Windows NT 4 SP4 or later), another application, service, or kernel mode driver is bound to the same address with exclusive access. Such exclusive access is a new feature of Windows NT 4 SP4 and later, and is implemented by using the SO_EXCLUSIVEADDRUSE option.


So, is there another program already running on port 9999? (Before you run your program. Do netstat -an at a command-prompt). Try a different port number and see if it works.

((The CAS error when running from a network share is the following:

C:>"\\zzzzpc1\zzz\temp\tcpSDan.exe"

Unhandled Exception: System.Security.SecurityException: Request for the permission of type 'System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at MainClass.Main()
The action that failed was:
Demand
The type of the first permission that failed was:
System.Net.SocketPermission
The first permission that failed was:
<IPermission class="System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1">
...
...
))

AlanJ.McFarlane at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified