SocketException让我发疯:( [英] SocketException driving me crazy:(
问题描述
我是c#的新手。
当我尝试以下程序(一个非常简单的tcpserver)时,我收到以下错误信息。
未处理的异常:System.Net.Sockets.SocketException:试图以一种禁止其访问权限的方式访问套接字
在MainClass.Main()上的System.Net.Sockets.Socket.Listen(Int32 backlog)
>这是代码..
使用System;
使用System.Net;
使用System.Net.Sockets;
使用System.Text;
类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.Writ eLine("与{0}在端口{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();
}
尝试运行Administrator 特权下的代码跨度>帐户
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();
}
}
Try running the code under Administrator privileged account
这篇关于SocketException让我发疯:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!