-
socket problem:
I seem to be reading in weird characters from the socket...
orginal string
"*HELLO*"
after i read in 1 byte this is what is stored in szData
"*
when i read in the next value
"H
and so on...
but only "* is copied accross to m_Message.
???
Confused :confused:
Code:
Public Class Client
Private m_asynResult As IAsyncResult
Private pfnCallBack As AsyncCallback
Private m_socClient As Socket
Private m_Message As String
Private m_DataBuffer(0) As Byte
Private m_IP As String
Public Sub OnConnect()
Dim ipAdd As Net.IPAddress = Net.IPAddress.Parse(m_IP)
Dim iPortNo As Int16 = 8221
Dim remoteEP As New Net.IPEndPoint(ipAdd, iPortNo)
Try
m_socClient = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
m_socClient.Connect(remoteEP)
WaitForData()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub WaitForData()
Try
If pfnCallBack Is Nothing Then pfnCallBack = New AsyncCallback(AddressOf OnDataReceived)
m_asynResult = m_socClient.BeginReceive(m_DataBuffer, 0, m_DataBuffer.Length, SocketFlags.None, pfnCallBack, Nothing)
Catch ex As Exception
End Try
End Sub
Private Sub OnDataReceived(ByVal asyn As IAsyncResult)
Try
Dim iRx As Integer = 0
iRx = m_socClient.EndReceive(asyn)
Dim chars(iRx + 1) As Char
Dim d As System.Text.Decoder = System.Text.Encoding.UTF8.GetDecoder()
Dim charLen As Integer = d.GetChars(m_DataBuffer, 0, iRx, chars, 0)
Dim szData As String = New System.String(chars)
m_Message += szData
WaitForData()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class