2013-12-10 2 views
0

나는 내가 네트워크를 사용하도록 설정된 DCHP에 연결하면 내장 된 장치의 IP 주소를 발견하기 위해 UDP를 사용하려면 지금 this tutorial아이폰 OS 마이크로 칩 이더넷 Discoverer는

로 소켓 송/수신 할 수 있습니다. GUI 기반의 iOS 앱이 UDP 탐색 요청을 브로드 캐스팅하면 임베디드 장치가 요청을 수신하고 iOS 응용 프로그램에 회신하여 네트워크상의 존재와 IP 주소를 알릴 수 있습니다.

내 앱은 검색 요청을 위해 PORT 30303을 수신합니다.

이 PC 응용 프로그램의 오픈 소스는 다음과 같습니다

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Net.Sockets; 

namespace Embedded_Device_Discoverer 
{ 
    public partial class Form1 : Form 
    { 
     public delegate void AsyncCallback(IAsyncResult ar); 
     public delegate void AddTolstDiscoveredDevices(object o); 

     private UdpState GlobalUDP; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     struct UdpState 
     { 
      public System.Net.IPEndPoint EP; 
      public System.Net.Sockets.UdpClient UDPClient; 
     } 

     private void cmdDiscoverDevices_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       // Clear the listbox of all current discovery responses 
       listView1.Items.Clear(); 

       // Try to send the discovery request message 
       byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery: Who is out there?"); 
       GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 30303)); 
      } 
      catch 
      { 
       MessageBox.Show("Unable to transmit discovery message. Check network connectivity and ensure that no other instances of this program are running.", "Error", MessageBoxButtons.OK); 
       return; 
      } 
     } 

     public void AddDiscoveryEntry(object o) 
     { 
      //lstDiscoveredDevices.Items.Add(o); 
      listView1.Items.Add(new ListViewItem(((string)(o)).Split('\n'))); 
     } 

     public void ReceiveCallback(IAsyncResult ar) 
     { 
      UdpState MyUDP = (UdpState)ar.AsyncState; 

      // Obtain the UDP message body and convert it to a string, with remote IP address attached as well 
      string ReceiveString = Encoding.ASCII.GetString(MyUDP.UDPClient.EndReceive(ar, ref MyUDP.EP)); 
      ReceiveString = MyUDP.EP.Address.ToString() + "\n" + ReceiveString.Replace("\r\n", "\n"); 

      // Configure the UdpClient class to accept more messages, if they arrive 
      MyUDP.UDPClient.BeginReceive(ReceiveCallback, MyUDP); 

      // Write the received UDP message text to the listbox in a thread-safe manner 
      //lstDiscoveredDevices.Invoke(new AddTolstDiscoveredDevices(AddDiscoveryEntry), ReceiveString); 
      listView1.Invoke(new AddTolstDiscoveredDevices(AddDiscoveryEntry), ReceiveString); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      try 
      { 
       GlobalUDP.UDPClient = new UdpClient(); 
       GlobalUDP.EP = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 30303); 
       System.Net.IPEndPoint BindEP = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 30303); 
       byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery: Who is out there?"); 

       // Set the local UDP port to listen on 
       GlobalUDP.UDPClient.Client.Bind(BindEP); 

       // Enable the transmission of broadcast packets without having them be received by ourself 
       GlobalUDP.UDPClient.EnableBroadcast = true; 
       GlobalUDP.UDPClient.MulticastLoopback = false; 

       // Configure ourself to receive discovery responses 
       GlobalUDP.UDPClient.BeginReceive(ReceiveCallback, GlobalUDP); 

       // Transmit the discovery request message 
       GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 30303)); 
      } 
      catch 
      { 
       MessageBox.Show("Unable to transmit discovery message. Check network connectivity and ensure that no other instances of this program are running.", "Error", MessageBoxButtons.OK); 
       return; 
      } 
     } 

     private void listView1_ItemActivate(object sender, EventArgs e) 
     { 
      System.Diagnostics.Process.Start("http://" + listView1.SelectedItems[0].Text); 
     } 
    } 
} 
내가 iOS 및 목표 C와 함께 할 수있는 방법

?

감사합니다

답변