Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my soket is working properly but it make slow my system after some time
VB
Public Class HANDLESERVER
        Dim CLIENTSOCKET As TcpClient
        Dim CLNO As String
        Public Sub STARTCLIENT(ByVal INCLIENTSOCKET As TcpClient, ByVal CLINENO As String)
            Me.CLIENTSOCKET = INCLIENTSOCKET
            Me.CLNO = CLINENO
            Dim CTTHREAD As Threading.Thread = New Thread(AddressOf RESPONSEREQUEST)
            CTTHREAD.Start()
        End Sub
        Private Sub RESPONSEREQUEST()
            Dim REQUESTCOUNT As Integer
            Dim DATAFROMCLIENT As String
            REQUESTCOUNT = 0
            While (True)
                Try
                    REQUESTCOUNT = REQUESTCOUNT + 1
                    Dim NETWORKSTREAM As NetworkStream = CLIENTSOCKET.GetStream()
                    Dim BYTESFROM(10024) As Byte
                    NETWORKSTREAM.Read(BYTESFROM, 0, CInt(CLIENTSOCKET.ReceiveBufferSize))
                    For I As Integer = 0 To 10024
                        If BYTESFROM(I) = 0 Then
                            NEXTPOS = I
                            Exit For
                        End If
                    Next
                    DATAFROMCLIENT = System.Text.Encoding.ASCII.GetString(BYTESFROM)
                    DATAFROMCLIENT = DATAFROMCLIENT.Substring(0, NEXTPOS)
                    DataFrimAts = DATAFROMCLIENT
                    If DataFrimAts <> "" Then
                        Dim D As New DLG_tRIAL(AddressOf _TIMER_DATA)
                        fmData.rchWithoutParse.Invoke(D)
                    End If
                    Array.Clear(BYTESFROM, 0, 10024)

                    NEXTPOS = 0

                Catch ex As Exception
                End Try
            End While
        End Sub
    End Class

VB
Sub Socket_listner()
        'Public Listnerport As Integer
        Dim serversocket4 As New TcpListener(ATCPORT)
        Dim clientsocket4 As TcpClient

        Dim counter As Integer
        serversocket4.Start()
        counter = 0

        While (True)
            counter += 1
            clientsocket4 = serversocket4.AcceptTcpClient
            Dim client2 As New HANDLESERVER
            client2.STARTCLIENT(clientsocket4, "1")

        End While
        clientsocket4.Close()
        serversocket4.Stop()
    End Sub

how to make it more faster and system independent
Posted
Updated 19-Oct-12 9:48am
v2

1 solution

Their is only one easy way that you can make it go faster and that is to use a background worker. The code itself has nothing to do to how fast or how slow your application works, it is where you place the code. I suspect you have a timer or call the class (I don't know because i am not a mind reader). This is the way I would attempt it.

1. First add 2 background workers to your project.
2. In the first background worker put your first code in.
VB
Public Class HANDLESERVER
        Dim CLIENTSOCKET As TcpClient
        Dim CLNO As String
        Public Sub STARTCLIENT(ByVal INCLIENTSOCKET As TcpClient, ByVal CLINENO As String)
            Me.CLIENTSOCKET = INCLIENTSOCKET
            Me.CLNO = CLINENO
            Dim CTTHREAD As Threading.Thread = New Thread(AddressOf RESPONSEREQUEST)
            CTTHREAD.Start()
        End Sub
        Private Sub RESPONSEREQUEST()
            Dim REQUESTCOUNT As Integer
            Dim DATAFROMCLIENT As String
            REQUESTCOUNT = 0
            While (True)
                Try
                    REQUESTCOUNT = REQUESTCOUNT + 1
                    Dim NETWORKSTREAM As NetworkStream = CLIENTSOCKET.GetStream()
                    Dim BYTESFROM(10024) As Byte
                    NETWORKSTREAM.Read(BYTESFROM, 0, CInt(CLIENTSOCKET.ReceiveBufferSize))
                    For I As Integer = 0 To 10024
                        If BYTESFROM(I) = 0 Then
                            NEXTPOS = I
                            Exit For
                        End If
                    Next
                    DATAFROMCLIENT = System.Text.Encoding.ASCII.GetString(BYTESFROM)
                    DATAFROMCLIENT = DATAFROMCLIENT.Substring(0, NEXTPOS)
                    DataFrimAts = DATAFROMCLIENT
                    If DataFrimAts <> "" Then
                        Dim D As New DLG_tRIAL(AddressOf _TIMER_DATA)
                        fmData.rchWithoutParse.Invoke(D)
                    End If
                    Array.Clear(BYTESFROM, 0, 10024)
 
                    NEXTPOS = 0
 
                Catch ex As Exception
                End Try
            End While
        End Sub
    End Class

3. In second background worker put your second code in.
VB
Sub Socket_listner()
        'Public Listnerport As Integer
        Dim serversocket4 As New TcpListener(ATCPORT)
        Dim clientsocket4 As TcpClient
 
        Dim counter As Integer
        serversocket4.Start()
        counter = 0
 
        While (True)
            counter += 1
            clientsocket4 = serversocket4.AcceptTcpClient
            Dim client2 As New HANDLESERVER
            client2.STARTCLIENT(clientsocket4, "1")
 
        End While
        clientsocket4.Close()
        serversocket4.Stop()
    End Sub

4. Now you need to stop your program from checking for illegalcrossthreadcalls. Put this code in.
VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub

5. know when you want to run your code you simply run the background worker.
VB
BackgroundWorker1.RunWorkerAsync()
BackgroundWorker2.RunWorkerAsync()

5. DONE

This should do the trick :) Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900