標籤

2011年9月18日 星期日

VB TCP 對外通訊與接收

.Net FrameWork 在 TCP/IP 通訊上支援的十分好. 幾乎只要 GOOGLE 之後就可以找出答案. 真正的問題在於, 接收和發送, 如果要顯示在 FORM 上, 仍然必須用 "委派" 的方式. 天下文章一大抄, 只要抄對了就好.
如果你考慮使用多工, 要小心通訊埠多次開啟的錯誤. 而主要是採用 TcpListener 為主. 重點要注意的是, 對外通訊必須使用 "192.168.XX.XX" 或 "10.X.X.X" 的 IP, 而不能使用 "127.0.0.1". 經實驗, 127.0.0.1 在 VB 對 VB 連線是沒問題的, 但是ANDROID 對 PC 連, 就會連不到.
另外, 收到的字元長度, 緩衝區定義為 1024, 長度就是 1024, 即使只傳了1個字, 其長度依然是 1024, 其它的字元都是 CHR(0). 所以你在判斷ANDROID傳回什麼答案時, 必須將CHR(0) 去掉才能進行判斷.
中文部份, 使用 UNICODE ENCODING 是可以接收到. 但是顯示時會有亂碼, (理論上 ANDROID 應該也是 UNICODE). 所以在轉換時, 請特別小心.
以下程式為支援 andriod 與 PC 通訊的 PC 端程式.
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Forms
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Private Class CSState
Public myTcpListener As TcpListener
Public ClientSocket As Socket
Public mystring As String
End Class
Private myDatatable As New DataTable
Private myTcpListener As TcpListener
Delegate Sub SetMsgCallBack(ByVal state As Object)
Private Sub DisplayMsg1(ByVal state As Object)
Dim myObj As New CSState
myObj = CType(state, CSState)
If Me.DataGridView1.InvokeRequired Then
Dim d As New SetMsgCallBack(AddressOf DisplayMsg1)
Me.Invoke(d, New Object() {myObj})
Else
Dim xRow As DataRow = myDatatable.NewRow()
xRow.Item(0) = CType(myObj.ClientSocket.RemoteEndPoint, IPEndPoint).Address.ToString()
xRow.Item(1) = CType(myObj.ClientSocket.RemoteEndPoint, IPEndPoint).Port.ToString()
xRow.Item(2) = myObj.mystring
myDatatable.Rows.Add(xRow)
End If
End Sub
Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
myDatatable.Columns.Add("IP")
myDatatable.Columns.Add("Port")
myDatatable.Columns.Add("Data")
DataGridView1.DataSource = myDatatable
DataGridView1.Columns(2).Width = 150
Dim iPort As Integer
iPort = "1024"
Dim ListenThread As New Thread(AddressOf StartListen)
ListenThread.IsBackground = True
ListenThread.Start(iPort)

End Sub
Private Sub StartListen(ByVal state As Object)
Dim iPort As Integer
iPort = CType(state, Integer)
myTcpListener = New TcpListener(IPAddress.Any, iPort)
Try
Dim ClientSocket As Socket
myTcpListener.Start()
Dim iCount As Integer = 0
Do
ClientSocket = myTcpListener.AcceptSocket()
If ClientSocket.Connected = True Then
Dim myObj As New CSState
myObj.myTcpListener = myTcpListener
myObj.ClientSocket = ClientSocket
myObj.mystring = Now.ToString("yyyy/MM/dd HH:mm:ss") & "已連線"
DisplayMsg1(myObj)
Dim ReceiveThread As New Thread(AddressOf ReceiveData)
ReceiveThread.IsBackground = True
ReceiveThread.Start(myObj)
iCount += 1
End If
Loop
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
Private Sub ReceiveData(ByVal state As Object)
Dim myObj As New CSState
myObj.ClientSocket = CType(state, CSState).ClientSocket
myObj.myTcpListener = CType(state, CSState).myTcpListener
myObj.mystring = ""
Dim myNetworkStream As New NetworkStream(myObj.ClientSocket)
Dim InBytesCount As Integer = 0
Dim myReceiveBytes(1023) As Byte
Dim i As Integer = 0
While True
Try
InBytesCount = myNetworkStream.Read(myReceiveBytes, 0, myReceiveBytes.Length)
System.Threading.Thread.Sleep(100)
If InBytesCount = 0 Then
Exit While
End If
myObj.mystring = Replace(Encoding.GetEncoding(950).GetString(myReceiveBytes).TrimEnd().TrimStart(), Chr(0), "")
DisplayMsg1(myObj)
'If (myObj.mystring.Equals("xxx")) Then
Dim myWriteBuffer As Byte() = Encoding.GetEncoding(950).GetBytes("Received: " + myObj.mystring)
myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length)
System.Threading.Thread.Sleep(100)
'End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
Exit Sub
End Try
End While
End Sub

沒有留言:

張貼留言