Many thanks for your hints. I have tried, it works.
However, I face the other problem is that
[Structure]
Structure CSLM02_UserLoginInfo
Public UserName As String
Public Password As String
End Structure
[Client]
Public Function send_socket(ByVal sendStructure As CSLM02_UserLoginInfo) As Boolean
Try
Dim networkStream As NetworkStream = serverTCPClient.GetStream()
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
formatter.Serialize(networkStream, sendStructure)
Return True
Catch ex As Exception
Console.WriteLine(ex.Message.ToString)
Return False
End Try
End Function
[Server]
Public Sub run_clientSocketThread()
Try
While True
Try
If clientSocketTcpClient.Available > 0 Then
Try
Dim stream As NetworkStream = clientSocketTcpClient.GetStream
Console.WriteLine("clientSocket.Available > 0 : " & clientSocketTcpClient.Available.ToString)
Dim numberOfBytesRead As Integer = 0
Dim byteReceived As Byte()
ReDim byteReceived(clientSocketTcpClient.Available - 1)
stream.Read(byteReceived, 0, clientSocketTcpClient.Available)
Dim formatter As IFormatter = New BinaryFormatter
Dim data As CSLM02_UserLoginInfo = CType(formatter.Deserialize(stream), CSLM02_UserLoginInfo)
Dim dataType As System.Type = data.GetType
Console.WriteLine("Data Received" & vbCrLf & "User : " & data.UserName.ToString & vbCrLf & "Pass : " & data.Password)
Catch ex As Exception
Console.WriteLine(ex.Message.ToString)
End Try
Else
Thread.Sleep(1000)
End If
Catch ex As Exception
Console.WriteLine(ex.Message.ToString)
End Try
End While
Catch ex As Exception
Console.WriteLine(ex.Message.ToString)
End Try
End Sub
[Result]
I keep send the [user1,pass1], [user2,pass2], [user3,pass3] ...etc,
the TCPServer keep received the data from the network stream and tried to handle in
Dim data As CSLM02_UserLoginInfo = CType(formatter.Deserialize(stream), CSLM02_UserLoginInfo)
However, I missed some data even the network stream received the [user1,pass1], [user3,pass3],...etc. What do i get from the server result as below. May I know any thing wrong in my code?
clientSocket.Available > 0 : 215
Data Received
User : user2
Pass : pass2
clientSocket.Available > 0 : 215
Data Received
User : user4
Pass : pass4
clientSocket.Available > 0 : 215
Data Received
User : user6
Pass : pass6
clientSocket.Available > 0 : 215
Data Received
User : user8
Pass : pass8
clientSocket.Available > 0 : 215
Data Received
User : user10
Pass : pass10