我用的是vs2005 vb-智慧型裝置ppc2003裝置應用程式
我要做的是從伺服器端接收廣播訊息
我的伺服器是用pc,接收裝置是用摸擬器
因為在pc上都可以接收,所以問題是在摸擬器這邊
下面是我的程式,錯誤訊息在最下面
-------------------------------------------------------------------------------------
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Object
Imports System.Collections.Specialized
Public Class Form1
Public uc As UdpClient
Public LocalPort As Integer
Public RemotePort As Integer
Public GroupAddress As IPAddress
Public remotEndPt As IPEndPoint
Public rT As Threading.Thread
Public bKeepRunning As Boolean = True
Public cKeepRunning As Boolean = True
Public stri_dus As String
Public str_dus As String
Public class_rb() As String
Public class_st() As String
Public ttl As Integer
Public encoding As System.Text.UnicodeEncoding = New System.Text.UnicodeEncoding
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
GroupAddress = IPAddress.Parse("224.0.1.1")
LocalPort = Int32.Parse("7777")
RemotePort = Int32.Parse("7777")
ttl = Int32.Parse("32")
uc = New UdpClient(LocalPort)
remotEndPt = New IPEndPoint(GroupAddress, RemotePort)
rT = New Thread(AddressOf Receving)
rT.Start()
Catch
MessageBox.Show("My text", "My caption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End Try
End Sub
---------------------------------------------------------------------------------------------
Public Sub Receving()
Try
While bKeepRunning
Thread.Sleep(2000)
Dim data() As Byte = uc.Receive(remotEndPt)
Dim strData As String = encoding.GetString(data, 0, data.Length)
TextBox1.Text = "接收中..."
Dim get_stri As String = strData.Substring(0, 1)
Dim stri_dus As String
If RadioButton1.Checked = True Then
stri_dus = "$"
End If
If RadioButton2.Checked = True Then
stri_dus = "%"
End If
If RadioButton3.Checked = True Then
stri_dus = "@"
End If
If RadioButton4.Checked = True Then
stri_dus = "*"
End If
If get_stri = stri_dus Then
Dim get_stri2 As String = strData.Substring(1)
ListBox1.Items.Add(get_stri2)
End If
End While
Catch ex As Exception
MessageBox.Show("My text", "My caption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End Try
End Sub
------------------------------------------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
uc.JoinMulticastGroup(GroupAddress, ttl)
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
MessageBox.Show("啟動")
End Sub
-----------------------------------------------------------------------------------------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
uc.DropMulticastGroup(GroupAddress)
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
uc.Close()
rT.Abort()
TextBox1.Text = ""
MessageBox.Show("停止接收")
End Sub
End Class
------------------------------------------------------------------------------------------
按Button1的錯誤訊息:
第一個可能發生的例外狀況類型 'System.IO.FileNotFoundException' 發生於 mscorlib.dll
第一個可能發生的例外狀況類型 'System.Net.Sockets.SocketException' 發生於 System.dll
按Button2的錯誤訊息:
第一個可能發生的例外狀況類型 'System.Threading.ThreadAbortException' 發生於 System.dll
第一個可能發生的例外狀況類型 'System.Threading.ThreadAbortException' 發生於 DeviceApplication1.exe
內文搜尋

X