Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am going to develop a socket server and clients for data communication. The socket server and clients has developed with TCPListener / TCPClient.

Following code has prepared for the string data, but i don't have experience on the structure data. Please help

VB
Public Sub doSendMsg(ByVal Msg As String)
        Try
            Dim sendBytes As [Byte]()
            Dim networkStream As NetworkStream = clientSocket.GetStream()
            sendBytes = Encoding.ASCII.GetBytes(Msg)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            networkStream.Flush()
        Catch ex As Exception
            Dim sf As StackFrame = New StackFrame
            Dim mb As MethodBase = sf.GetMethod
            Console.WriteLine(mb.Name & " Failure : " & ex.Message)
        End Try
End Sub
Posted
Updated 27-Oct-11 6:22am
v2

You probably need to use Serialization[^] to convert your object(s) into byte streams.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Oct-11 12:53pm    
This is the way, my 5.
--SA
Richard MacCutchan 27-Oct-11 12:59pm    
Thanks again; it's interesting how much one can learn by reading questions and other peoples' answers.
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
 
Share this answer
 
v2
Comments
P.Salini 28-Oct-11 1:53am    
added <pre></pre> tags
Richard MacCutchan 28-Oct-11 4:57am    
Sorry but my VB skills are not too good. However, I cannot see an obvious flaw in your code. I can only suggest that you add some more debug statements on both the client and server side to see if you can discover what is happening.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900