ACAE
asked on
Sockets : application becomes very slow
Hello,
I'm writing a PDA application in vb.net using the compact framework. This application features a winsock listener to capture message from the host application.
The problem is : after receiving a message, my application becomes really slow. Does anyone have a clue how this can be solved ?
I'm writing a PDA application in vb.net using the compact framework. This application features a winsock listener to capture message from the host application.
The problem is : after receiving a message, my application becomes really slow. Does anyone have a clue how this can be solved ?
can you post some code snippets...
ASKER
OK, here it comes :
I created as class called 'NavisionHost' :
-------------------------- ---------- ----------
Imports System.Net.Sockets
Imports System.Text
Public Class NavisionHost
Const READ_BUFFER_SIZE As Integer = 65536
Private MyTCPClient As TcpClient
Private readBuffer(READ_BUFFER_SIZ E) As Byte
Public Sub New(ByVal client As TcpClient)
Me.MyTCPClient = client
' This starts the asynchronous read thread. The data will be saved into
' readBuffer.
ReDim readBuffer(READ_BUFFER_SIZ E)
Me.MyTCPClient.GetStream.B eginRead(r eadBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End Sub
Public Event LineReceived(ByVal sender As NavisionHost, ByVal Data As String)
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Dim liPosition As Integer = 0
Try
' Ensure that no other threads try to use the stream at the same time.
SyncLock MyTCPClient.GetStream
' Finish asynchronous read into readBuffer and get number of bytes read.
BytesRead = MyTCPClient.GetStream.EndR ead(ar)
End SyncLock
' Convert the byte array the message was saved into, minus one for the
' Chr(13).
If BytesRead > 0 Then
strMessage = Encoding.ASCII.GetString(r eadBuffer, 0, BytesRead - 1)
RaiseEvent LineReceived(Me, strMessage)
End If
' Ensure that no other threads try to use the stream at the same time.
SyncLock MyTCPClient.GetStream
' Start a new asynchronous read into readBuffer.
MyTCPClient.GetStream.Begi nRead(read Buffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch ex As Exception
End Try
End Sub
End Class
-----------------------
This class is used on the form that contains the listener :
-----------------------
Private myTCPlistener As TcpListener
Private myListenerThread As Threading.Thread = Nothing
'Enable listener
If Not (myListenerThread Is Nothing) Then
myTCPlistener.Stop()
myTCPlistener = Nothing
myListenerThread = Nothing
End If
myListenerThread = New Threading.Thread(AddressOf DoListen)
myListenerThread.Start()
Private Sub DoListen()
StartDoListen:
Try
If ucPublicInfo.LoggedOn And Not (ucPublicInfo.connIPAddres s Is Nothing) Then
If Not (myTCPlistener Is Nothing) Then
myTCPlistener.Stop()
myTCPlistener = Nothing
End If
myTCPlistener = New TcpListener(ucPublicInfo.c onnIPAddre ss, ucPublicInfo.iPortNum)
myTCPlistener.Start()
Do
If Not (ucNavisionHost Is Nothing) Then
ucNavisionHost = Nothing
End If
ucNavisionHost = New NavisionHost(myTCPlistener .AcceptTcp Client)
' Create an event handler to allow the UserConnection to communicate
' with the window.
AddHandler ucNavisionHost.LineReceive d, AddressOf OnLineReceived
Loop Until False
End If
Catch ex As Exception
GoTo StartDoListen
End Try
End Sub
Private Sub OnLineReceived(ByVal sender As NavisionHost, ByVal psdata As String)
'In this procedure the received data (string containing XML document) is processed
'....
End Sub
Is this sufficient ?
I created as class called 'NavisionHost' :
--------------------------
Imports System.Net.Sockets
Imports System.Text
Public Class NavisionHost
Const READ_BUFFER_SIZE As Integer = 65536
Private MyTCPClient As TcpClient
Private readBuffer(READ_BUFFER_SIZ
Public Sub New(ByVal client As TcpClient)
Me.MyTCPClient = client
' This starts the asynchronous read thread. The data will be saved into
' readBuffer.
ReDim readBuffer(READ_BUFFER_SIZ
Me.MyTCPClient.GetStream.B
End Sub
Public Event LineReceived(ByVal sender As NavisionHost, ByVal Data As String)
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Dim liPosition As Integer = 0
Try
' Ensure that no other threads try to use the stream at the same time.
SyncLock MyTCPClient.GetStream
' Finish asynchronous read into readBuffer and get number of bytes read.
BytesRead = MyTCPClient.GetStream.EndR
End SyncLock
' Convert the byte array the message was saved into, minus one for the
' Chr(13).
If BytesRead > 0 Then
strMessage = Encoding.ASCII.GetString(r
RaiseEvent LineReceived(Me, strMessage)
End If
' Ensure that no other threads try to use the stream at the same time.
SyncLock MyTCPClient.GetStream
' Start a new asynchronous read into readBuffer.
MyTCPClient.GetStream.Begi
End SyncLock
Catch ex As Exception
End Try
End Sub
End Class
-----------------------
This class is used on the form that contains the listener :
-----------------------
Private myTCPlistener As TcpListener
Private myListenerThread As Threading.Thread = Nothing
'Enable listener
If Not (myListenerThread Is Nothing) Then
myTCPlistener.Stop()
myTCPlistener = Nothing
myListenerThread = Nothing
End If
myListenerThread = New Threading.Thread(AddressOf
myListenerThread.Start()
Private Sub DoListen()
StartDoListen:
Try
If ucPublicInfo.LoggedOn And Not (ucPublicInfo.connIPAddres
If Not (myTCPlistener Is Nothing) Then
myTCPlistener.Stop()
myTCPlistener = Nothing
End If
myTCPlistener = New TcpListener(ucPublicInfo.c
myTCPlistener.Start()
Do
If Not (ucNavisionHost Is Nothing) Then
ucNavisionHost = Nothing
End If
ucNavisionHost = New NavisionHost(myTCPlistener
' Create an event handler to allow the UserConnection to communicate
' with the window.
AddHandler ucNavisionHost.LineReceive
Loop Until False
End If
Catch ex As Exception
GoTo StartDoListen
End Try
End Sub
Private Sub OnLineReceived(ByVal sender As NavisionHost, ByVal psdata As String)
'In this procedure the received data (string containing XML document) is processed
'....
End Sub
Is this sufficient ?
Here is some samples you might want to look at....
https://www.experts-exchange.com/questions/21172715/System-Net-Sockets-Explained-1-2.html
https://www.experts-exchange.com/questions/21172715/System-Net-Sockets-Explained-1-2.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It becomes slow immediately after receiving the data. I replaced the pocessing of the data with a simple messagebox and it remains slow. Could it have something to do with the size of the receivebuffer ?
ASKER
Hi,
I hope this helps, I think I found the reason why it becomes slow but I don't know how to solve this.
In the event 'streamreceiver' I replaced
If BytesRead > 0 Then
strMessage = Encoding.ASCII.GetString(r eadBuffer, 0, BytesRead - 1)
RaiseEvent LineReceived(Me, strMessage)
End If
with a messagebox, and this messagebox keeps showing on the PDA. Do I need an endread or something ?
I hope this helps, I think I found the reason why it becomes slow but I don't know how to solve this.
In the event 'streamreceiver' I replaced
If BytesRead > 0 Then
strMessage = Encoding.ASCII.GetString(r
RaiseEvent LineReceived(Me, strMessage)
End If
with a messagebox, and this messagebox keeps showing on the PDA. Do I need an endread or something ?
ASKER
Oops, just a bit too fast. I have an endread just before the messagebox. Then why does the message appear again ?
ASKER
Another bit of information that might help : the first time the event is called, 40 bytes are received, then always 0 (which is normal as there is nothing send)
ASKER
I found a solution : if bytesread is zero then i perform an exit sub. This seems to be working for now. Thanx for your help anyway.