none
PDA上廣播用法 RRS feed

  • 問題

  • 在pda上開發了一個可以接收和發送廣播訊息的程式

    在模擬器上執行沒問題…但到了實機pda上卻出現錯誤了

    請問是在用法上有誤嗎…為什麼模擬器上就ok?



    http://yhco.net/try.gif  錯誤的圖片

            uc = New UdpClient(CInt(txtLocalPort.Text))
            GroupAddress = IPAddress.Parse("255.255.255.255")  <---出現如圖的錯誤
            RemoteEP = New IPEndPoint(GroupAddress, CInt(txtRemotePort.Text))
            ' Me.Invoke(New EventHandler(AddressOf Receving))
            rT = New Threading.Thread(AddressOf Receving)
            rT.Start()
    2006年9月28日 下午 07:35

解答

  • 看起來是在這一行出錯,

    GroupAddress = IPAddress.Parse("255.255.255.255")

    可以是PDA環境上有什麼問題,你可以catch這個exception,

    再把相關的錯誤資訊印出來。

    2006年10月2日 上午 06:52

所有回覆

  • Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Text
    Public Class 電子折價券
        Inherits System.Windows.Forms.Form
        Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
        Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

    #Region " Windows Form 設計工具產生的程式碼 "

        Public Sub New()
            MyBase.New()

            '此為 Windows Form 設計工具所需的呼叫。
            InitializeComponent()

            '在 InitializeComponent() 呼叫之後加入所有的初始設定

        End Sub

        'Form 覆寫 Dispose 以清除元件清單。
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            MyBase.Dispose(disposing)
        End Sub

        '注意: 以下為 Windows Form 設計工具所需的程序
        '您可以使用 Windows Form 設計工具進行修改。
        '請勿使用程式碼編輯器來修改這些程序。
        Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents txtLocalPort As System.Windows.Forms.TextBox
        Friend WithEvents txtRemotePort As System.Windows.Forms.TextBox
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Private Sub InitializeComponent()
            Me.MainMenu1 = New System.Windows.Forms.MainMenu
            Me.TextBox1 = New System.Windows.Forms.TextBox
            Me.ListBox1 = New System.Windows.Forms.ListBox
            Me.Label1 = New System.Windows.Forms.Label
            Me.txtLocalPort = New System.Windows.Forms.TextBox
            Me.txtRemotePort = New System.Windows.Forms.TextBox
            Me.Button1 = New System.Windows.Forms.Button
            '
            'TextBox1
            '
            Me.TextBox1.Font = New System.Drawing.Font("標楷體", 14.25!, System.Drawing.FontStyle.Regular)
            Me.TextBox1.Location = New System.Drawing.Point(16, 40)
            Me.TextBox1.Size = New System.Drawing.Size(208, 30)
            Me.TextBox1.Text = ""
            '
            'ListBox1
            '
            Me.ListBox1.Location = New System.Drawing.Point(16, 80)
            Me.ListBox1.Size = New System.Drawing.Size(208, 134)
            '
            'Label1
            '
            Me.Label1.Font = New System.Drawing.Font("新細明體", 11.25!, System.Drawing.FontStyle.Regular)
            Me.Label1.Location = New System.Drawing.Point(24, 8)
            Me.Label1.Size = New System.Drawing.Size(64, 24)
            Me.Label1.Text = "localport"
            '
            'txtLocalPort
            '
            Me.txtLocalPort.Location = New System.Drawing.Point(88, 8)
            Me.txtLocalPort.Size = New System.Drawing.Size(48, 22)
            Me.txtLocalPort.Text = "10000"
            '
            'txtRemotePort
            '
            Me.txtRemotePort.Location = New System.Drawing.Point(144, 8)
            Me.txtRemotePort.Size = New System.Drawing.Size(48, 22)
            Me.txtRemotePort.Text = "10000"
            '
            'Button1
            '
            Me.Button1.Font = New System.Drawing.Font("新細明體", 14.25!, System.Drawing.FontStyle.Bold)
            Me.Button1.Location = New System.Drawing.Point(16, 224)
            Me.Button1.Size = New System.Drawing.Size(208, 32)
            Me.Button1.Text = "Send"
            '
            '電子折價券
            '
            Me.Controls.Add(Me.Button1)
            Me.Controls.Add(Me.txtRemotePort)
            Me.Controls.Add(Me.txtLocalPort)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.ListBox1)
            Me.Controls.Add(Me.TextBox1)
            Me.Menu = Me.MainMenu1
            Me.Text = "電子折價券"

        End Sub

    #End Region
        Private uc As UdpClient
        Private LocalPort As Integer
        Private RemotePort As Integer
        Private GroupAddress As IPAddress
        Private RemoteEP As IPEndPoint
        Private rT As Threading.Thread
        Private bKeepRunning As Boolean = True
        Public Min As Integer

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim a, b, c, d, f, g, h, i, x, y As Double
            Dim Z As Integer
            'Dim Min As Integer
            x = 120.3282
            y = 22.65162
            a = (((y - 22.64565) * 0.86) ^ 2 + (x - 120.33031) ^ 2) ^ 0.5 * 111320
            b = (((y - 22.65186) * 0.86) ^ 2 + (x - 120.32877) ^ 2) ^ 0.5 * 111320
            c = (((y - 22.65363) * 0.86) ^ 2 + (x - 120.32924) ^ 2) ^ 0.5 * 111320
            d = (((y - 22.65313) * 0.86) ^ 2 + (x - 120.33148) ^ 2) ^ 0.5 * 111320
            f = (((y - 22.65363) * 0.86) ^ 2 + (x - 120.33074) ^ 2) ^ 0.5 * 111320
            g = (((y - 22.64878) * 0.86) ^ 2 + (x - 120.32337) ^ 2) ^ 0.5 * 111320
            h = (((y - 22.64594) * 0.86) ^ 2 + (x - 120.32288) ^ 2) ^ 0.5 * 111320
            i = (((y - 22.6455) * 0.86) ^ 2 + (x - 120.32399) ^ 2) ^ 0.5 * 111320
            Dim ddd() As Integer = {a, b, c, d, f}
            For Z = 0 To UBound(ddd)
                If ddd(Z) < ddd(Min) Then Min = Z
            Next
            Dim ttt() As String = {"7-11大豐門市  高雄市三民區正忠路222號", "OK便利店工專店  高雄市三民區建工路411號", "7-11本館門市  高雄市三民區大昌一路6號", "7-11本元門市  高雄市三民區建工路317號", "萊爾富建工店  高雄市三民區建工路304號", "OK便利店建德店  高雄市三民區建工路493號", "7-11雙興門市  高雄市三民區建興路313號", "全家便利建興店  高雄市三民區建興路274號"}
            TextBox1.Text = "約" & ddd(Min) & "公尺" & "  " & ttt(Min)


            uc = New UdpClient(CInt(txtLocalPort.Text))
            GroupAddress = IPAddress.Parse("255.255.255.255")
            RemoteEP = New IPEndPoint(GroupAddress, CInt(txtRemotePort.Text))
            ' Me.Invoke(New EventHandler(AddressOf Receving))
            rT = New Threading.Thread(AddressOf Receving)
            rT.Start()

        End Sub
        Public Sub Receving()

            Try
                While bKeepRunning
                    Thread.Sleep(2000)
                    Dim data() As Byte = uc.Receive(RemoteEP)
                    Dim strData As String = Encoding.Unicode.GetString(data, 0, data.Length)
                    ListBox1.Items.Add(strData)
                End While
            Catch ex As Exception
                MsgBox("發生以下的錯誤:" & ex.Message)
            End Try
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ch As String
            If Min = 0 Or 2 Or 3 Or 6 Then
                ch = 1
            End If
            If Min = 1 Or 5 Then
                ch = 2
            End If
            If Min = 4 Then
                ch = 3
            End If
            If Min = 7 Then
                ch = 4
            End If

            Try

                Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(ch)
                uc.Send(sendBytes, sendBytes.Length, RemoteEP)
            Catch ex As Exception
                MsgBox("發生以下的錯誤:" & ex.Message)
            End Try

        End Sub

    End Class

    2006年9月29日 上午 03:47
  • 看起來是在這一行出錯,

    GroupAddress = IPAddress.Parse("255.255.255.255")

    可以是PDA環境上有什麼問題,你可以catch這個exception,

    再把相關的錯誤資訊印出來。

    2006年10月2日 上午 06:52