2015-10-08 48 views
0

我正在學習如何使用UDP與同一臺機器上的其他應用程序進行通信,因此我正在通過下面的程序來獲取這些概念,但是當我運行它時,出現錯誤消息,他說:C#簡單的UDP通信

型 'System.Net.Sockets.SocketException' 未處理的異常發生在System.dll中

我缺少什麼或者做錯了嗎?有人可以請簡單解釋一下如果程序運行會發生什麼?謝謝

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net; 
using System.Net.Sockets; 


namespace udpClient 
{ 
    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      byte[] data = new byte[1024]; 
      string input, stringData; 

      IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); 
      Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 

      string welcome = "Hello, are you there?"; 
      data = Encoding.ASCII.GetBytes(welcome); 
      server.SendTo(data, data.Length, SocketFlags.None, ipep); 

      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); 
      EndPoint Remote = (EndPoint)sender; 

      data = new byte[1024]; 
      int recv = server.ReceiveFrom(data, ref Remote); 

      Console.WriteLine("Message received from {0}:", Remote.ToString()); 
      Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); 

      // In Unity3D, replace this with update or coroutine. 
      while (true) 
      { 
       input = Console.ReadLine(); 

       if (input == "exit") 
        break; 

       server.SendTo(Encoding.ASCII.GetBytes(input), Remote); 
       data = new byte[1024]; 
       recv = server.ReceiveFrom(data, ref Remote); 
       stringData = Encoding.ASCII.GetString(data, 0, recv); 
       Console.WriteLine(stringData); 
      } 

      Console.WriteLine("Stopping client .."); 
      server.Close(); 

      // Console.WriteLine("\nPress any key ..."); 
      // Console.ReadKey(); 
     } 
    } 
} 
+1

檢查:http://stackoverflow.com/questions/2370388/socketexception-address-incompatible-with-requested-protocol –

+3

什麼是完整的錯誤消息,並在哪一行是上調? –

+0

@Rahul這個例子是這個異常的_many_原因之一。 OP需要檢查他們的InnerException。 – CodeCaster

回答

0

看看你的代碼的這個快速修復,請注意這個結構僅適用於演示,它不適用於生產。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 

namespace testUDP 
{ 
class Program 
{ 

    static volatile Boolean receivingThreadStarted = false; 

    public static void DoReceiveFrom() 
    { 
     byte[] data = new byte[1024]; 
     IPEndPoint sender = new IPEndPoint(IPAddress.Any, 9050); 
     EndPoint Remote = (EndPoint)sender; 
     Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
     server.Bind(sender); 
     receivingThreadStarted = true; 
     int recv = server.ReceiveFrom(data, ref Remote); 
     Console.WriteLine("Message received from {0}:", Remote.ToString()); 
     Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); 
    } 


    static void Main(string[] args) 
    { 
     byte[] data = new byte[1024]; 
     string input; 

     IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); 
     Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 

     ThreadStart threadDelegate = new ThreadStart(DoReceiveFrom); 
     Thread newThread = new Thread(threadDelegate); 
     newThread.Start(); 

     // Waiting to start the receiving thread. 
     while (!receivingThreadStarted) Thread.Sleep(100); 

     string welcome = "Hello, are you there?"; 
     data = Encoding.ASCII.GetBytes(welcome); 
     server.SendTo(data, data.Length, SocketFlags.None, ipep); 

     while (true) 
     { 
      input = Console.ReadLine(); 

      if (input == "exit") 
       break;    
     } 

     Console.WriteLine("Stopping client .."); 
     server.Close(); 

    } 
} 

}

+0

**非常感謝你** ...它像一個魅力!我將研究代碼以進一步學習它。如果我需要澄清,我會再發表評論。歡呼 – Joshua

+0

嗨哈鬆,你能詳細談談'IPAddress.Any,9050'嗎?這是否意味着它可以在任何機器上運行(即任何IP地址),並通過端口9050(控制檯端口?)發送?此外,從{0}收到的消息「,remote.ToString()',當我運行該程序時顯示」127.0.0.1:65226「的部分 - 是否又是一個端口號? – Joshua