iOS Microchip Ethernet Discoverer(iOS Microchip 以太网发现器)
问题描述
我可以使用套接字发送/接收 本教程
I can send/receive with socket as this tutorial
现在,我想在插入启用 DCHP 的网络时使用 UDP 来发现嵌入式设备的 IP 地址.借助基于 GUI 的 iOS 应用程序广播 UDP 发现请求,嵌入式设备可以接收请求并回复 iOS 应用程序,宣布它们在网络上的存在和 IP 地址.
Now, i want to use UDP to discover an embedded device’s IP address when plugged into a DCHP enabled network. With a GUI based iOS app broadcasting a UDP discovery request, embedded devices can receive the request and reply to the iOS app, announcing their presence and IP address on the network.
我的应用将监听 PORT 30303
的发现请求.
My app will listen to the PORT 30303
for a discovery request.
这是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('
')));
}
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() + "
" + ReceiveString.Replace("
", "
");
// 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 和 Objective C 来做到这一点?
How can i do it with iOS and Objective C?
谢谢
推荐答案
您可以从这里开始进行设备发现
You can start from here for device discovery
https://github.com/cybergarage/CyberLink4C
这篇关于iOS Microchip 以太网发现器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS Microchip 以太网发现器
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01