Solved

Sockets : application becomes very slow

Posted on 2004-10-19
9
217 Views
Last Modified: 2010-04-23
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 ?
0
Comment
Question by:ACAE
  • 6
  • 2
9 Comments
 
LVL 9

Expert Comment

by:checoo
ID: 12347285
can you post some code snippets...
0
 

Author Comment

by:ACAE
ID: 12347363
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_SIZE) 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_SIZE)
        Me.MyTCPClient.GetStream.BeginRead(readBuffer, 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.EndRead(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(readBuffer, 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.BeginRead(readBuffer, 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.connIPAddress Is Nothing) Then
                If Not (myTCPlistener Is Nothing) Then
                    myTCPlistener.Stop()
                    myTCPlistener = Nothing
                End If
                myTCPlistener = New TcpListener(ucPublicInfo.connIPAddress, ucPublicInfo.iPortNum)
                myTCPlistener.Start()
                Do
                    If Not (ucNavisionHost Is Nothing) Then
                        ucNavisionHost = Nothing
                    End If
                    ucNavisionHost = New NavisionHost(myTCPlistener.AcceptTcpClient)

                    ' Create an event handler to allow the UserConnection to communicate
                    ' with the window.
                    AddHandler ucNavisionHost.LineReceived, 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 ?
0
 
LVL 27

Expert Comment

by:planocz
ID: 12348584
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 9

Accepted Solution

by:
checoo earned 500 total points
ID: 12354900
is your app becoming slow immediately after receiving the data or it becomes slow during the XML processing in OnLineReceived
0
 

Author Comment

by:ACAE
ID: 12355626
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 ?
0
 

Author Comment

by:ACAE
ID: 12355782
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(readBuffer, 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 ?

0
 

Author Comment

by:ACAE
ID: 12355789
Oops, just a bit too fast.  I have an endread just before the messagebox.  Then why does the message appear again ?
0
 

Author Comment

by:ACAE
ID: 12355854
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)
0
 

Author Comment

by:ACAE
ID: 12367492
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 Shortcut (VB) 4 45
VB.NET 1 21
Convert datetime to time string 10 19
VB.net Progress Bar - Maximum Value too large 2 4
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now