TCP Connect

Mark82

Newcomer
Joined
Jun 22, 2003
Messages
12
I wrote this code to try and connect to a tcp server, send a string and then recieve the reply string. But it seems that the 'TCPClient.Connect(HostIP, PortNum)' line seems to be taking about 5-6 seconds. where as if i use VB6 and Winsock control it takes 0.5-1 seconds to connect. Is there some way i can get that same sort of performance out of .net??

Thanks for any help.



Visual Basic:
Dim TCPClient As New TcpClient

        Try
            TCPClient.Connect(HostIP, PortNum)
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Function
        End Try

        Dim networkStream As NetworkStream = TCPClient.GetStream()
        If networkStream.CanWrite And networkStream.CanRead Then
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("@01EX E5 00:52" & vbCr)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            Dim bytes(TCPClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(TCPClient.ReceiveBufferSize))
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            Return (returndata)
        Else
            If Not networkStream.CanRead Then
                'Console.WriteLine("cannot not write data to this stream")
                TCPClient.Close()
            Else
                If Not networkStream.CanWrite Then
                    'Console.WriteLine("cannot read data from this stream")
                    TCPClient.Close()
                End If
            End If
        End If

        TCPClient.Close()
    End Function
 
Last edited:
Do you get the performance difference from VB.Net and VB 6 running from the same machine? Can't see any obvious reason why it should be so slow - also never experienced any delays in using the connect method myself though.
 
Last edited:
Back
Top