hi, 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
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