如何通过TCPsocket发送和接收结构,字符串,整数数据? [英] How to send and receive structure, string, interger data via TCPsocket?

查看:126
本文介绍了如何通过TCPsocket发送和接收结构,字符串,整数数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将开发用于数据通信的套接字服务器和客户端.套接字服务器和客户端已使用TCPListener/TCPClient开发.

以下代码已为字符串数据做好了准备,但是我对结构数据没有经验.请帮忙

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

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

推荐答案

您可能需要使用序列化 [
You probably need to use Serialization[^] to convert your object(s) into byte streams.


非常感谢您的提示.我试过了,可以用.

但是,我面临的另一个问题是

[结构]
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


[结果]
我不断发送[user1,pass1],[user2,pass2],[user3,pass3] ... etc,
TCPServer不断接收来自网络流的数据,并尝试在
中进行处理 昏暗的数据作为CSLM02_UserLoginInfo = CType(formatter.Deserialize(stream),CSLM02_UserLoginInfo)

但是,即使网络流接收到[user1,pass1],[user3,pass3]等,我也错过了一些数据.我从服务器结果中得到什么,如下所示.我可以知道我的代码有什么问题吗?


[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


这篇关于如何通过TCPsocket发送和接收结构,字符串,整数数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆