收发Byte数组而不要收发String。以下内容仅供参考: Private Sub tcpCAS_DataArrival(ByVal bytesTotal As Long) Dim i As Long Dim iBuf() As Byte Dim lnx As String On Error Resume Next ' 收当前流 ReDim iBuf(bytesTotal - 1) tcpCAS.GetData iBuf 'log每个收到的字节 i = 0 lnx = "cas-->BYTE:" + Right("0000000" + Hex(i), 8) + "-" For i = 0 To bytesTotal - 1 lnx = lnx + " " + Right("0" + Hex(iBuf(i)), 2) If i Mod 16 = 15 Then debug.print lnx lnx = "cas-->BYTE:" + Right("0000000" + Hex(i + 1), 8) + "-" End If Next i = bytesTotal - 1 If i Mod 16 <> 15 Then debug.print lnx End If ……
UTF转码参考:Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long '常用的代码页: const cpUTF8 =65001 const cpGB2312 = 936 const cpGB18030=54936 const cpUTF7 =65000 Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String Dim bufSize As Long bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0) MultiByteToUTF16 = Space(bufSize) MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize End FunctionFunction UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte() Dim bufSize As Long Dim arr() As Byte bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0) ReDim arr(bufSize - 1) WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0 UTF16ToMultiByte = arr End FunctionPrivate Sub Command1_Click() MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8) End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim iBuf() As Byte ReDim iBuf(bytesTotal - 1)
Private Sub tcpCAS_DataArrival(ByVal bytesTotal As Long)
Dim i As Long
Dim iBuf() As Byte
Dim lnx As String
On Error Resume Next
' 收当前流
ReDim iBuf(bytesTotal - 1)
tcpCAS.GetData iBuf
'log每个收到的字节
i = 0
lnx = "cas-->BYTE:" + Right("0000000" + Hex(i), 8) + "-"
For i = 0 To bytesTotal - 1
lnx = lnx + " " + Right("0" + Hex(iBuf(i)), 2)
If i Mod 16 = 15 Then
debug.print lnx
lnx = "cas-->BYTE:" + Right("0000000" + Hex(i + 1), 8) + "-"
End If
Next
i = bytesTotal - 1
If i Mod 16 <> 15 Then
debug.print lnx
End If
……
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
'常用的代码页:
const cpUTF8 =65001
const cpGB2312 = 936
const cpGB18030=54936
const cpUTF7 =65000
Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String
Dim bufSize As Long
bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0)
MultiByteToUTF16 = Space(bufSize)
MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize
End FunctionFunction UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte()
Dim bufSize As Long
Dim arr() As Byte
bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0)
ReDim arr(bufSize - 1)
WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0
UTF16ToMultiByte = arr
End FunctionPrivate Sub Command1_Click()
MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8)
End Sub
ReDim iBuf(bytesTotal - 1)
Winsock1(Index).GetData iBuf Text2.Text = Text2.Text & "服务器" & Winsock1(Index).RemoteHost & "说的话" & iBuf & Chr$(13) & Chr$(10)
End Sub调试提示“类型不匹配”???