Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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.

 

 

 

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

Edited by Mark82
  • Administrators
Posted (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. Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...