vb.net客户端/服务器大数据问题 [英] vb.net client / server big data question

查看:89
本文介绍了vb.net客户端/服务器大数据问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们感谢时间和帮助我有一个客户端/服务器应用程序通过网络发送XML文件我的问题是

当我有一个大的XML数据必须从缓冲区字节4096到524288



如果缓冲区在4096上好几次其他时间不完整





这是我的服务器代码:



guys thanks for time and help i have a client/server app for send XML file over the network my problem is
when i have a big XML data have to up the buffers byte from 4096 to 524288

if the buffer is on 4096 some times fine others time incomplete


this my server code:

<pre>Imports System
Imports System.Threading
Imports System.Net.Sockets
Imports System.IO
Imports System.Text
Imports HwServerManager.hwservermanager_utility
Public Class HwServerManagerTcpServer
    Private Structure ClientInfo

        Public Socket As Socket
        Public Thread As Thread
        Public LastRecData As String
    End Structure
    Private tcpLsn As TcpListener
    Private ClientsPool As New Hashtable()
    Private tcpThd As Thread
    Private IDCurrentClient As Net.IPEndPoint
    Private TCPPortListener As String
    Private _Running As Boolean = True
    Public Event NewConnection(ByVal IDTerminal As Net.IPEndPoint)
    Public Event RecData(ByVal IDTerminal As Net.IPEndPoint)
    Public Event FinishedConn(ByVal IDTerminal As Net.IPEndPoint)
    Property ListenerPort() As String
        Get
            ListenerPort = TCPPortListener
        End Get
        Set(ByVal Value As String)
            TCPPortListener = Value
        End Set
    End Property
    Public Sub TCPListener()
        tcpLsn = New TcpListener(New System.Net.IPEndPoint(0, ListenerPort))
        tcpLsn.Start()
        tcpThd = New Thread(AddressOf WaitForClient)
        tcpThd.IsBackground = True
        tcpThd.Start()
    End Sub
    Public Function GetData(ByVal IDClient As Net.IPEndPoint) As String
        Dim RequestClientInfo As ClientInfo
        RequestClientInfo = ClientsPool(IDClient)
        GetData = RequestClientInfo.LastRecData
    End Function
    Public Sub CloseConn(ByVal IDClient As Net.IPEndPoint)
        Dim CurrentClientInfo As ClientInfo
        CurrentClientInfo = ClientsPool(IDClient)
        CurrentClientInfo.Socket.Close()
    End Sub
    Public Sub Close()
        Dim CurrentClientInfo As ClientInfo
        For Each CurrentClientInfo In ClientsPool.Values
            Call CloseConn(CurrentClientInfo.Socket.RemoteEndPoint)
        Next
    End Sub
    Public Sub SendData(ByVal IDClient As Net.IPEndPoint, ByVal Data As String, Optional Channel As Integer = 0)
        Dim Client As ClientInfo
        Data = String.Format("{0}{1}{2}{3}{4}", Chr(2), Channel, Chr(3), RTrim(Data), Chr(7))
        Client = ClientsPool(IDClient)
        Client.Socket.Send(Encoding.ASCII.GetBytes(Data))
    End Sub
    Public Sub Broadcast(ByVal Data As String, Optional Channel As Integer = 0)
        Dim Client As ClientInfo
        Data = String.Format("{0}{1}{2}{3}{4}", Chr(2), Channel, Chr(3), RTrim(Data), Chr(7))
        For Each Client In ClientsPool.Values
            SendData(Client.Socket.RemoteEndPoint, Data)
        Next
    End Sub
    Public Sub TerminateAll()
        Try
            _Running = False
            tcpLsn.Stop()
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub
    Private Sub WaitForClient()
        Dim CurrentClientInfo As ClientInfo = Nothing
        With CurrentClientInfo
            While _Running
                .Socket = tcpLsn.AcceptSocket()
                IDCurrentClient = .Socket.RemoteEndPoint
                .Thread = New Thread(AddressOf ReadSocket)
                SyncLock Me
                    ClientsPool.Clear()
                    ClientsPool.Add(IDCurrentClient, CurrentClientInfo)
                End SyncLock
                RaiseEvent NewConnection(IDCurrentClient)
                .Thread.Start()
            End While
            System.Threading.Thread.Sleep(1000)
            .Thread.Abort()
        End With
    End Sub
    Private Sub ReadSocket()
        Dim IDReal As Net.IPEndPoint
        Dim PulData() As Byte
        Dim CurrentClientInfo As ClientInfo
        Dim Ret As Integer = 0
        IDReal = IDCurrentClient
        CurrentClientInfo = ClientsPool(IDReal)
        With CurrentClientInfo
            While True
                If .Socket.Connected Then
                    PulData = New Byte(4096) {}
                    Try
                        Ret = .Socket.Receive(PulData, PulData.Length, SocketFlags.None)
                        If Ret > 0 Then
                            .LastRecData = Encoding.ASCII.GetString(Decompress(PulData))
                            ClientsPool(IDReal) = CurrentClientInfo
                            RaiseEvent RecData(IDReal)
                        Else
                            RaiseEvent FinishedConn(IDReal)
                            Exit While
                        End If
                    Catch e As Exception
                        If Not .Socket.Connected Then
                            RaiseEvent FinishedConn(IDReal)
                            Exit While
                        End If
                    End Try
                End If
            End While
            Call CloseThread(IDReal)
        End With
    End Sub
    Private Sub CloseThread(ByVal IDClient As Net.IPEndPoint)
        Dim CurrentClientInfo As ClientInfo
        CurrentClientInfo = ClientsPool(IDClient)
        Try
            CurrentClientInfo.Thread.Abort()
        Catch e As Exception
            SyncLock Me
                ClientsPool.Remove(IDClient)
            End SyncLock
        End Try
    End Sub
End Class





这是mi客户端代码



this is mi client code

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.IO
Imports HwServerManager.hwservermanager_utility
Public Class HwServerManagerTcpClient
    Private Stm As Stream
    Private _HostIp As String
    Private _HostPort As String
    Public Event ConnectionFinish()
    Public Event ReceivedData(ByVal Data As String)
    Public Property HostIP() As String
        Get
            HostIP = _HostIp
        End Get

        Set(ByVal Value As String)
            _HostIp = Value
        End Set

    End Property
    Public Property HostPort() As String
        Get
            HostPort = _HostPort
        End Get
        Set(ByVal Value As String)
            _HostPort = Value
        End Set
    End Property
    Dim tcpClnt As TcpClient
    Dim tcpThd As Thread

    Public Sub Connect()
        tcpClnt = New TcpClient()
        tcpClnt.Connect(_HostIp, _HostPort)
        Stm = tcpClnt.GetStream()
        tcpThd = New Thread(AddressOf ReadSocket)
        tcpThd.Start()
    End Sub
    Public Sub SendData(ByVal Data As String, Optional Channel As Integer = 0)
        Dim WriteBuffer() As Byte
        Data = String.Format("{0}{1}{2}{3}{4}", Chr(2), Channel, Chr(3), Data, Chr(7))
        WriteBuffer = Compress(Encoding.ASCII.GetBytes(Data))
        If Not (Stm Is Nothing) Then
            Stm.Write(WriteBuffer, 0, WriteBuffer.Length)
        End If
        Stm.Close()
    End Sub
    Public Sub Disconnect()
        Try
            If tcpClnt.Connected Then tcpClnt.Close()
            If tcpThd.IsAlive() Then tcpThd.Abort()
            RaiseEvent ConnectionFinish()
        Catch e As Exception
        End Try
    End Sub
    Private Sub ReadSocket()
        Dim ReadBuffer() As Byte
        While True
            Try
                ReadBuffer = New Byte(4096) {}
                Stm.Read(ReadBuffer, 0, ReadBuffer.Length)
                RaiseEvent ReceivedData(Encoding.ASCII.GetString(ReadBuffer))
            Catch e As Exception
                Exit While
            End Try
        End While
        RaiseEvent ConnectionFinish()
    End Sub
End Class

推荐答案

查看官方文档这里 [ ^ ]。我认为你必须在 maxRequestLength 属性中设置一个大整数。
See the official documentation here[^]. I think that you must set a big integer into the maxRequestLength attribute.


这篇关于vb.net客户端/服务器大数据问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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