下面这些代码分别放在什么位置?
在订单编号框中输入订单编号,点击“查询”按钮,通过下面的代码获取订单内容:
result = UDPCommunication("%G|" & orderNum & "%")
然后进行内容解析:
If result IsNot Nothing Then
If result.StartsWith("%") AndAlso result.EndsWith("%") Then
Session("OrderNum") = orderNum '记录订单编号
Dim orders() As String = result.Trim("%").Split("$") '分离主表和明细
Dim orderdetails() As String = orders(0).Split("|")
txtName.Value = orderdetails(0)
txtOrderDate.Value = orderdetails(1)
txtDeliverDate.Value = orderdetails(2)
If orders.Length = 2 Then
If orders(1).StartsWith("@") AndAlso orders(1).EndsWith("@") Then
Dim detailArry() As String = orders(1).Trim("@").Split("@") '分离不同明细
For Each de As String In detailArry
Dim details() As String = de.Split("|")
Dim dr As DataRow
dr = order.NewRow()
For i As Integer = 0 To details.Length - 1
dr(i) = details(i) '定义的字段必须和返回值的字段顺序一致
Next
order.Rows.Add(dr)
Next
End If
End If
End If
End If
这里定义一个函数来和Foxtable的UDPClient进行通讯,如
Function UDPCommunication(ByVal input As String) As String
Dim udp As New System.Net.Sockets.UdpClient(0)
Dim ip As New System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 52177) '定义通讯的网络端点,地址和端口号要和和UDPClient启动时的一致
Dim bytes() As Byte = System.Text.Encoding.Unicode.GetBytes(input)
udp.Send(bytes, bytes.Length, ip) '发送数据
Dim result As String = "!false"
Dim sdt As Date = Date.Now
While Date.Now < sdt.AddSeconds(1) '等1秒
If udp.Available > 0 Then
Dim remoteIp As New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Dim rbytes() As Byte = udp.Receive(remoteIp)
result = System.Text.Encoding.Unicode.GetString(rbytes) '获取返回值
End If
End While
udp.Close()
Return result
End Function