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,

Matt

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();
}

Re: TCPClient Performance Severly Lags Behind VB6 Winsock by Jon

Jon
Thu Jul 22 16:25:04 CDT 2004

Swiftusw <swiftus32@yahoo.com> wrote:
> 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?

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: TCPClient Performance Severly Lags Behind VB6 Winsock by swiftus32

swiftus32
Fri Jul 23 13:04:34 CDT 2004

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] <skeet@pobox.com> wrote in message news:<MPG.1b6a410ff0d72c9798af76@msnews.microsoft.com>...
> Swiftusw <swiftus32@yahoo.com> wrote:
> > 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?
>
> 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?