.NETGURU
TCPClient Performance Severly Lags Behind VB6 Winsock
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.performance.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Swiftusw
I'm writing a connector for a client to replace an old VB6 system. The
connector works, but is several times slower than the older VB6
connector. I have isolated the problem to be in the connection to the
server. All we are doing is sending a input stream to the server and
receiving an output stream in response. The code is below. Can anybody
see why this would run so much slower than VB6 using the Winsock
ActiveX control?

TIA,

Dot Net Guru

public string SendMessage(string server, int port, string
requestMessage)
{
    TcpClient client = new TcpClient();

    client.SendBufferSize = 1024;
    client.ReceiveBufferSize = 1024;

    StringBuilder responseData = new StringBuilder();
    
    byte[] data = Encoding.ASCII.GetBytes(requestMessage);    

    client.Connect(server, port);

    NetworkStream stream = client.GetStream();    

    stream.Write(data, 0, data.Length);

    data = new byte[client.ReceiveBufferSize];        
    int bytes = 0;

    while(true)
    {    
        bytes = stream.Read(data, 0, data.Length);
        if(bytes>0){
         responseData.Append(Encoding.ASCII.GetString(data, 0, bytes));
        }
        else{
     break;
        }
    }
        
    stream.Close();
    client.Close();

    return responseData.ToString();
}
Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Swiftusw <Click here to reveal e-mail address> wrote:
[Original message clipped]

Have you tried using a longer receive buffer? It might be worth using a
network analyser to find out what socket options the Winsock control is
using.

Are you seeing much load on either system?

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Swiftusw
I figured it out -- The original while{} loop over-ran while trying to
capture every byte that came through. By having the thread sleep on
each iteration of the while loop, it allowed the buffer to fill up.
Basically, all I did was change the while loop to this:

do
{    
    data = new byte[client.ReceiveBufferSize];
    bytes = stream.Read(data, 0, data.Length);
    responseData.Append(Encoding.ASCII.GetString(data, 0, bytes));        
    System.Threading.Thread.Sleep(500);
}
while(stream.DataAvailable);

Jon Skeet [C# MVP] <Click here to reveal e-mail address> wrote in message news:<Click here to reveal e-mail address>...
[Original message clipped]

Reply to this message...
 
 
System.Net.Sockets.NetworkStream
System.Net.Sockets.TcpClient
System.Text.Encoding
System.Text.StringBuilder
System.Threading.Thread




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification