Strange problem with System.Net.Sockets.TcpClient()

Discussion in 'ASP .Net' started by Torsten Brasch, Jan 6, 2004.

  1. Hi All and Happy New Year ;)

    I have a very strange problem with System.Net.Sockets.TcpClient().
    For some reason, the number of bytes I can receive is limited to 5460 bytes.
    I made sure that the server really sends everything I expect it to send. I
    am actually sending XML messages and this limit is a pain in the ...

    Here is the code:
    --------------------------------------------------------------------------
    Dim tcpclient As New System.Net.Sockets.TcpClient()
    tcpclient.Connect(Server_IP, portNumber)

    Dim NWStream As NetworkStream = tcpclient.GetStream
    Dim bytesToSend As Byte() = Encoding.ASCII.GetBytes(pInText)

    '---send the text
    NWStream.Write(bytesToSend, 0, bytesToSend.Length)

    '---read back the text
    Dim bytesToRead(tcpclient.ReceiveBufferSize) As Byte

    ' HERE IS THE PROBLEM - numBytesRead for some reason is limited to 5460
    bytes
    Dim numBytesRead As Int32 = NWStream.Read(bytesToRead, 0,
    tcpclient.ReceiveBufferSize)Result = Encoding.ASCII.GetString(bytesToRead,
    0, numBytesRead)

    tcpclient.Close()

    --------------------------------------------------------------------------

    I would very much appreciate if somebody had a clue of what is going on
    here.

    Thanks,
    Torsten
    --
    In the force if Yoda's so strong, construct a sentence with words in the
    proper order then why can't he?
     
    Torsten Brasch, Jan 6, 2004
    #1
    1. Advertising

  2. Torsten Brasch

    Chris Botha Guest

    Actually you have to read until the bytes read = 0 (or until an error
    occurs), and concatenate each segment read, building up the message.
    Sockets behavior can be strange, depending on the environment conditions
    (like machine busy, network flakey, etc) and in some cases you may receive
    only a couple of hundred bytes of the total message with one read.
    The NetworkStream class may have some magic built in that I don't know
    about, so try adjusting the ReceiveBufferSize, but I won't bet on it.

    "Torsten Brasch" <> wrote in message
    news:%...
    > Hi All and Happy New Year ;)
    >
    > I have a very strange problem with System.Net.Sockets.TcpClient().
    > For some reason, the number of bytes I can receive is limited to 5460

    bytes.
    > I made sure that the server really sends everything I expect it to send. I
    > am actually sending XML messages and this limit is a pain in the ...
    >
    > Here is the code:
    > --------------------------------------------------------------------------
    > Dim tcpclient As New System.Net.Sockets.TcpClient()
    > tcpclient.Connect(Server_IP, portNumber)
    >
    > Dim NWStream As NetworkStream = tcpclient.GetStream
    > Dim bytesToSend As Byte() = Encoding.ASCII.GetBytes(pInText)
    >
    > '---send the text
    > NWStream.Write(bytesToSend, 0, bytesToSend.Length)
    >
    > '---read back the text
    > Dim bytesToRead(tcpclient.ReceiveBufferSize) As Byte
    >
    > ' HERE IS THE PROBLEM - numBytesRead for some reason is limited to 5460
    > bytes
    > Dim numBytesRead As Int32 = NWStream.Read(bytesToRead, 0,
    > tcpclient.ReceiveBufferSize)Result = Encoding.ASCII.GetString(bytesToRead,
    > 0, numBytesRead)
    >
    > tcpclient.Close()
    >
    > --------------------------------------------------------------------------
    >
    > I would very much appreciate if somebody had a clue of what is going on
    > here.
    >
    > Thanks,
    > Torsten
    > --
    > In the force if Yoda's so strong, construct a sentence with words in the
    > proper order then why can't he?
    >
    >
     
    Chris Botha, Jan 6, 2004
    #2
    1. Advertising

  3. I've set ReceiveBufferSize to 32768, just for a test - no change. In fact,
    in my example code, the system sets it to 8192 by default. I just did a few
    more tests, as soon as I send 5461 bytes, that 1 additional byte is lost. I
    always just get 5460 bytes out of the stream.


    "Chris Botha" <> wrote in message
    news:...
    > Actually you have to read until the bytes read = 0 (or until an error
    > occurs), and concatenate each segment read, building up the message.
    > Sockets behavior can be strange, depending on the environment conditions
    > (like machine busy, network flakey, etc) and in some cases you may receive
    > only a couple of hundred bytes of the total message with one read.
    > The NetworkStream class may have some magic built in that I don't know
    > about, so try adjusting the ReceiveBufferSize, but I won't bet on it.
    >
    > "Torsten Brasch" <> wrote in message
    > news:%...
    > > Hi All and Happy New Year ;)
    > >
    > > I have a very strange problem with System.Net.Sockets.TcpClient().
    > > For some reason, the number of bytes I can receive is limited to 5460

    > bytes.
    > > I made sure that the server really sends everything I expect it to send.

    I
    > > am actually sending XML messages and this limit is a pain in the ...
    > >
    > > Here is the code:

    >
    > --------------------------------------------------------------------------
    > > Dim tcpclient As New System.Net.Sockets.TcpClient()
    > > tcpclient.Connect(Server_IP, portNumber)
    > >
    > > Dim NWStream As NetworkStream = tcpclient.GetStream
    > > Dim bytesToSend As Byte() = Encoding.ASCII.GetBytes(pInText)
    > >
    > > '---send the text
    > > NWStream.Write(bytesToSend, 0, bytesToSend.Length)
    > >
    > > '---read back the text
    > > Dim bytesToRead(tcpclient.ReceiveBufferSize) As Byte
    > >
    > > ' HERE IS THE PROBLEM - numBytesRead for some reason is limited to 5460
    > > bytes
    > > Dim numBytesRead As Int32 = NWStream.Read(bytesToRead, 0,
    > > tcpclient.ReceiveBufferSize)Result =

    Encoding.ASCII.GetString(bytesToRead,
    > > 0, numBytesRead)
    > >
    > > tcpclient.Close()
    > >

    >
    > --------------------------------------------------------------------------
    > >
    > > I would very much appreciate if somebody had a clue of what is going on
    > > here.
    > >
    > > Thanks,
    > > Torsten
    > > --
    > > In the force if Yoda's so strong, construct a sentence with words in the
    > > proper order then why can't he?
    > >
    > >

    >
    >
     
    Torsten Brasch, Jan 6, 2004
    #3
  4. Ok, I think I solved it:

    Instead of
    Dim numBytesRead As Int32 = NWStream.Read(bytesToRead, 0,
    tcpclient.ReceiveBufferSize)

    I am doing this now:

    ' Ok, let's try with reading every single byte
    Dim n As Int32 = 0
    Dim i As Int32 = 1

    While i <> 0
    i = NWStream.Read(bytesToRead, n, 1)
    n += 1
    Response.Write(CStr(i) + "|" + CStr(n) + "<br>")
    Response.Flush()
    End While

    This actually works. I get the full data now BUT if I send more than 8K
    (8192 bytes), it throws an exception saying 'index out of range".

    So that is the ReceiveBuffer that still is 8K even though I am sending more
    than that.
    The MS documentation makes it clear though: You don't have to worry about
    the size of ReceiveBuffer, the system dynamically increases it - well,
    obviously NOT ;(

    I can set it manually, let's say to 32768 bytes - but I am confident that
    one of my users will hit that limit as well - would have been so nice to
    have it increased dynamically.

    Thanks for your hint with reading until the result is ZERO!!! ;)

    Thanks,
    Torsten


    "Chris Botha" <> wrote in message
    news:...
    > Actually you have to read until the bytes read = 0 (or until an error
    > occurs), and concatenate each segment read, building up the message.
    > Sockets behavior can be strange, depending on the environment conditions
    > (like machine busy, network flakey, etc) and in some cases you may receive
    > only a couple of hundred bytes of the total message with one read.
    > The NetworkStream class may have some magic built in that I don't know
    > about, so try adjusting the ReceiveBufferSize, but I won't bet on it.
    >
    > "Torsten Brasch" <> wrote in message
    > news:%...
    > > Hi All and Happy New Year ;)
    > >
    > > I have a very strange problem with System.Net.Sockets.TcpClient().
    > > For some reason, the number of bytes I can receive is limited to 5460

    > bytes.
    > > I made sure that the server really sends everything I expect it to send.

    I
    > > am actually sending XML messages and this limit is a pain in the ...
    > >
    > > Here is the code:

    >
    > --------------------------------------------------------------------------
    > > Dim tcpclient As New System.Net.Sockets.TcpClient()
    > > tcpclient.Connect(Server_IP, portNumber)
    > >
    > > Dim NWStream As NetworkStream = tcpclient.GetStream
    > > Dim bytesToSend As Byte() = Encoding.ASCII.GetBytes(pInText)
    > >
    > > '---send the text
    > > NWStream.Write(bytesToSend, 0, bytesToSend.Length)
    > >
    > > '---read back the text
    > > Dim bytesToRead(tcpclient.ReceiveBufferSize) As Byte
    > >
    > > ' HERE IS THE PROBLEM - numBytesRead for some reason is limited to 5460
    > > bytes
    > > Dim numBytesRead As Int32 = NWStream.Read(bytesToRead, 0,
    > > tcpclient.ReceiveBufferSize)Result =

    Encoding.ASCII.GetString(bytesToRead,
    > > 0, numBytesRead)
    > >
    > > tcpclient.Close()
    > >

    >
    > --------------------------------------------------------------------------
    > >
    > > I would very much appreciate if somebody had a clue of what is going on
    > > here.
    > >
    > > Thanks,
    > > Torsten
    > > --
    > > In the force if Yoda's so strong, construct a sentence with words in the
    > > proper order then why can't he?
    > >
    > >

    >
    >
     
    Torsten Brasch, Jan 6, 2004
    #4
  5. Torsten Brasch

    Chris Botha Guest

    It should not crash and don't read 1 byte at a shot, it is too slow, read at
    least 5400 (if the max transfer size is 5460 as per your previous message).
    It works even if the message is shorter, because you look at the bytes read
    count after the read and not the size given as parameter (the size given as
    parameter depicts your data buffer size).
    Look at the example in the "NetworkStream.Read" in the documentation,
    they read blocks of 1024 bytes and build the message in a loop.

    "Torsten Brasch" <> wrote in message
    news:...
    > Ok, I think I solved it:
    >
    > Instead of
    > Dim numBytesRead As Int32 = NWStream.Read(bytesToRead, 0,
    > tcpclient.ReceiveBufferSize)
    >
    > I am doing this now:
    >
    > ' Ok, let's try with reading every single byte
    > Dim n As Int32 = 0
    > Dim i As Int32 = 1
    >
    > While i <> 0
    > i = NWStream.Read(bytesToRead, n, 1)
    > n += 1
    > Response.Write(CStr(i) + "|" + CStr(n) + "<br>")
    > Response.Flush()
    > End While
    >
    > This actually works. I get the full data now BUT if I send more than 8K
    > (8192 bytes), it throws an exception saying 'index out of range".
    >
    > So that is the ReceiveBuffer that still is 8K even though I am sending

    more
    > than that.
    > The MS documentation makes it clear though: You don't have to worry about
    > the size of ReceiveBuffer, the system dynamically increases it - well,
    > obviously NOT ;(
    >
    > I can set it manually, let's say to 32768 bytes - but I am confident that
    > one of my users will hit that limit as well - would have been so nice to
    > have it increased dynamically.
    >
    > Thanks for your hint with reading until the result is ZERO!!! ;)
    >
    > Thanks,
    > Torsten
    >
    >
    > "Chris Botha" <> wrote in message
    > news:...
    > > Actually you have to read until the bytes read = 0 (or until an error
    > > occurs), and concatenate each segment read, building up the message.
    > > Sockets behavior can be strange, depending on the environment conditions
    > > (like machine busy, network flakey, etc) and in some cases you may

    receive
    > > only a couple of hundred bytes of the total message with one read.
    > > The NetworkStream class may have some magic built in that I don't know
    > > about, so try adjusting the ReceiveBufferSize, but I won't bet on it.
    > >
    > > "Torsten Brasch" <> wrote in message
    > > news:%...
    > > > Hi All and Happy New Year ;)
    > > >
    > > > I have a very strange problem with System.Net.Sockets.TcpClient().
    > > > For some reason, the number of bytes I can receive is limited to 5460

    > > bytes.
    > > > I made sure that the server really sends everything I expect it to

    send.
    > I
    > > > am actually sending XML messages and this limit is a pain in the ...
    > > >
    > > > Here is the code:

    > >

    >
    > --------------------------------------------------------------------------
    > > > Dim tcpclient As New System.Net.Sockets.TcpClient()
    > > > tcpclient.Connect(Server_IP, portNumber)
    > > >
    > > > Dim NWStream As NetworkStream = tcpclient.GetStream
    > > > Dim bytesToSend As Byte() = Encoding.ASCII.GetBytes(pInText)
    > > >
    > > > '---send the text
    > > > NWStream.Write(bytesToSend, 0, bytesToSend.Length)
    > > >
    > > > '---read back the text
    > > > Dim bytesToRead(tcpclient.ReceiveBufferSize) As Byte
    > > >
    > > > ' HERE IS THE PROBLEM - numBytesRead for some reason is limited to

    5460
    > > > bytes
    > > > Dim numBytesRead As Int32 = NWStream.Read(bytesToRead, 0,
    > > > tcpclient.ReceiveBufferSize)Result =

    > Encoding.ASCII.GetString(bytesToRead,
    > > > 0, numBytesRead)
    > > >
    > > > tcpclient.Close()
    > > >

    > >

    >
    > --------------------------------------------------------------------------
    > > >
    > > > I would very much appreciate if somebody had a clue of what is going

    on
    > > > here.
    > > >
    > > > Thanks,
    > > > Torsten
    > > > --
    > > > In the force if Yoda's so strong, construct a sentence with words in

    the
    > > > proper order then why can't he?
    > > >
    > > >

    > >
    > >

    >
    >
     
    Chris Botha, Jan 6, 2004
    #5
  6. Your read loop is incorrect. You are assuming that all the data will be
    available in the first read. THis is not true, as TCP is a stream based
    transfer, and record boundaries are not supported.

    I am assuming that the data coming back is ASCII text. So, you an code your
    Read loop as follows: (in C# syntax)

    MemoryStream ms = new MemoryStream();
    byte [] bytesToRead = new byte[tcpclient.ReadBufferSize];
    int numBytesRead = -1;

    while(true) {
    try {
    numBytesRead = NWStream.Read((bytesToRead, 0,
    tcpclient.ReceiveBufferSize);
    if(numBytesRead > 0)
    ms.Write(bytesToRead,0,numBytesRead);
    else
    break;
    } catch {
    throw;
    }
    }

    ms.Seek(0,SeekOrigin.Begin);
    StreamReader sr = new StreamReader(ms,Encoding.ASCII);
    Result = sr.ReadToEnd();
    sr.Close();
    ms.Close();

    NWStream.Close();
    tcpclient.Close();


    --
    Remove "user" from the email address to reply to the author.

    This posting is provided "AS IS" with no warranties, and confers no rights

    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm




    "Torsten Brasch" <> wrote in message
    news:#...
    > Hi All and Happy New Year ;)
    >
    > I have a very strange problem with System.Net.Sockets.TcpClient().
    > For some reason, the number of bytes I can receive is limited to 5460

    bytes.
    > I made sure that the server really sends everything I expect it to send. I
    > am actually sending XML messages and this limit is a pain in the ...
    >
    > Here is the code:
    > --------------------------------------------------------------------------
    > Dim tcpclient As New System.Net.Sockets.TcpClient()
    > tcpclient.Connect(Server_IP, portNumber)
    >
    > Dim NWStream As NetworkStream = tcpclient.GetStream
    > Dim bytesToSend As Byte() = Encoding.ASCII.GetBytes(pInText)
    >
    > '---send the text
    > NWStream.Write(bytesToSend, 0, bytesToSend.Length)
    >
    > '---read back the text
    > Dim bytesToRead(tcpclient.ReceiveBufferSize) As Byte
    >
    > ' HERE IS THE PROBLEM - numBytesRead for some reason is limited to 5460
    > bytes
    > Dim numBytesRead As Int32 = NWStream.Read(bytesToRead, 0,
    > tcpclient.ReceiveBufferSize)Result = Encoding.ASCII.GetString(bytesToRead,
    > 0, numBytesRead)
    >
    > tcpclient.Close()
    >
    > --------------------------------------------------------------------------
    >
    > I would very much appreciate if somebody had a clue of what is going on
    > here.
    >
    > Thanks,
    > Torsten
    > --
    > In the force if Yoda's so strong, construct a sentence with words in the
    > proper order then why can't he?
    >
    >
     
    Feroze [MSFT], Jan 7, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Summers

    TcpClient

    Jeremy Summers, Oct 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    436
    Jeremy Summers
    Oct 21, 2003
  2. =?Utf-8?B?SmltIFc=?=

    TCPClient connect slow only on XP Pro ASP on some boxes

    =?Utf-8?B?SmltIFc=?=, Sep 30, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    1,961
    =?Utf-8?B?SmltIFc=?=
    Nov 1, 2004
  3. Aaron

    tcpclient socket webrequest

    Aaron, Apr 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    809
    Kevin Spencer
    Apr 18, 2005
  4. Tamas Bojcan

    Tcpclient problem with proxy server

    Tamas Bojcan, Jul 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    838
    Tamas Bojcan
    Jul 21, 2005
  5. Frank

    TCPClient App Question

    Frank, Dec 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    388
    tdavisjr
    Dec 19, 2005
Loading...

Share This Page