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]
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">
...
...
))