2011-04-06 3 views
-2

Visual Studio에서 중단 점을 설정 한 후 C#에서 UDP 데몬 클래스를 만듭니다. "시도한 작업 유형이 참조 된 개체. " this::ip::Address::ScopeId::base. ScopeId는 예외 System.Net.Sockets.SocketException을 throw합니다. 오류 코드는 10045/OperationNotSupported입니다.UDP 데몬 클래스 : 시도한 작업이 참조 된 개체 유형에 대해 지원되지 않습니다.

호출 코드 :

namespace Foo.Tester 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var TestDaemon = new UDPDaemon(); 
      TestDaemon.port = 9999; 
      TestDaemon.Start(); 
      ... 

UDPDaemon 등급 :

{ 
    public class UDPDaemon 
    { 

     public int receivedDataLength; 
     public byte[] data; 
     public IPEndPoint ip; 
     public Socket socket; 
     public IPEndPoint sender; 
     public EndPoint Remote; 
     public string raw; 
     public int port { get; set; } 
     public LogRow row; 


     public UDPDaemon() 
     { 
      ip = new IPEndPoint(IPAddress.Any, port); 
      socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
      sender = new IPEndPoint(IPAddress.Any, 0); 
      Remote = (EndPoint)(sender); 
     } 
     public void Start() 
     { 
      socket.Bind(ip); 
      while (true) 
      { 
       data = new byte[1024]; 
       receivedDataLength = socket.ReceiveFrom(data, ref Remote); 
       raw = Encoding.ASCII.GetString(data, 0, receivedDataLength); 
       row = new LogRow(raw); 
       //Will eventually move to Queue, but just print it for now 
       Console.WriteLine(row.ClientIp); 
      } 
     } 
    } 
} 
  1. 무엇이 예외와 예외가 무엇을 의미 하는가를 일으키는?
  2. VS에서 중단 점을 설정하면 예외가 어떻게 만듭니 까?
  3. 방금이 언어를 배우기 시작 했으므로 다른 언어로 알고있는 것이 좋습니다. 생성자 내부 포트를 사용하기 원하기 때문에
+0

= 새로운 IPEndPoint (IPAddress.Any, 포트); 그리고 그 소켓 예외를 일으키는, 그래서 이것은 기본 C# OO 개념의 일부 일종이다 ... –

+1

아, 지금은 IP가 건설 도중 할당됩니다 개체의 인스턴스이며 그 이후까지 포트가 할당되지 않습니다. –

답변

1

, 당신은 예를 들어,하지 나중에 설정하려면 생성자의 인자로 전달해야합니다 포트가 IP '로 설정하지 않을

public class UDPDaemon 
{ 
    public int receivedDataLength; 
    public byte[] data; 
    public IPEndPoint ip; 
    public Socket socket; 
    public IPEndPoint sender; 
    public EndPoint Remote; 
    public string raw; 
    public int Port { get; private set; } 
    public LogRow row; 

    public UDPDaemon(int port) 
    { 
     Port = port; 
     ip = new IPEndPoint(IPAddress.Any, port); 
     socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
     sender = new IPEndPoint(IPAddress.Any, 0); 
     Remote = (EndPoint)(sender); 
    } 
.... 
관련 문제