TCP Transfer issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rich

    #1

    TCP Transfer issues

    I am writing two programs that are part of a Bulletin board system.
    The program works right, but when information is downloaded, the server
    program refuses to send more than 9 Kilobytes of data to the client.
    How can this be fixed?

    The code is:

    Server (Running in a separate thread from UI):
    H:
    Const portNumber As Integer = 8000
    Dim tcpListener As New TcpListener(por tNumber)
    tcpListener.Sta rt()
    Try
    Dim tcpClient As TcpClient = tcpListener.Acc eptTcpClient()
    Dim networkStream As NetworkStream = tcpClient.GetSt ream()
    Dim bytes(tcpClient .ReceiveBufferS ize) As Byte
    networkStream.R ead(bytes, 0,
    CInt(tcpClient. ReceiveBufferSi ze))
    Dim clientdata As String = Encoding.Unicod e.GetString(byt es)
    Dim responseString As String = TextBox1.Text
    Dim sendBytes As [Byte]() = Encoding.Unicod e.GetBytes(resp onseString)
    networkStream.W rite(sendBytes, 0, sendBytes.Lengt h)
    tcpClient.Close ()
    tcpListener.Sto p()
    Catch e As Exception
    End Try
    GoTo H
    End Sub


    Try
    Dim tcpClient As New System.Net.Sock ets.TcpClient()
    tcpClient.Conne ct(ip, port)
    Dim networkStream As NetworkStream = tcpClient.GetSt ream()
    If networkStream.C anWrite And networkStream.C anRead Then
    Dim sendBytes As [Byte]() = Encoding.Unicod e.GetBytes(Text Box2.Text)
    networkStream.W rite(sendBytes, 0, sendBytes.Lengt h)
    Dim bytes(tcpClient .ReceiveBufferS ize) As Byte
    networkStream.R ead(bytes, 0,
    CInt(tcpClient. ReceiveBufferSi ze))
    Dim returndata As String = Encoding.Unicod e.GetString(byt es)
    Me.Close()
    Else
    If Not networkStream.C anRead Then
    MsgBox("Data cannot be written.")
    tcpClient.Close ()
    Else
    If Not networkStream.C anWrite Then
    MsgBox("Data cannot be read.")
    tcpClient.Close ()
    End If
    End If
    End If
    Catch ex As Exception
    MsgBox("Error! Sever is not functioning!")
    MsgBox(ex.ToStr ing)
    MsgBox(ex.Messa ge)
    End Try


    Sorry about the sloppy coding. This is partially based on something i
    found on Egghead cafe. If it matters, I am using a fast computer with
    Windows XP Professional.

  • Tom Shelton

    #2
    Re: TCP Transfer issues


    Rich wrote:
    I am writing two programs that are part of a Bulletin board system.
    The program works right, but when information is downloaded, the server
    program refuses to send more than 9 Kilobytes of data to the client.
    Hmmm, right about the size of the normal network buffer size....
    How can this be fixed?
    >
    Well, looking at the code, tells me that you are only doing a single
    read. When you read data off a socket, it is not necessarily available
    in a single read. That's why you need to either know exactly how many
    bytes you are to receive, so you can read until you have that number of
    bytes (which may involve multiple reads in a loop or buffering the data
    if your going to use async sockets) or you need to have a end-of-stream
    marker of some kind (CR/LF is a common choice for string data).

    HTH,
    Tom Shelton

    Comment

    • Rich

      #3
      Re: TCP Transfer issues

      I figured out an easy way to fix it, when I looked at the line:
      networkStream.R ead(bytes, 0, CInt(tcpClient. ReceiveBufferSi ze))

      The intellisence says :
      tcpClient.Recei ve Buffer Size as Integer
      Gets or sets the size of the receive buffer.

      Changing this number to a greater value (I changed it to 65,536 for
      now) resolves the issue. The CInt says how much data to read. I have
      not needed to change any settings in the server. I will experiment to
      find out if a greater CInt makes it slow (like 100 MB download).

      -Rich

      Comment

      • Tom Shelton

        #4
        Re: TCP Transfer issues


        Rich wrote:
        I figured out an easy way to fix it, when I looked at the line:
        networkStream.R ead(bytes, 0, CInt(tcpClient. ReceiveBufferSi ze))
        >
        The intellisence says :
        tcpClient.Recei ve Buffer Size as Integer
        Gets or sets the size of the receive buffer.
        >
        Changing this number to a greater value (I changed it to 65,536 for
        now) resolves the issue. The CInt says how much data to read. I have
        not needed to change any settings in the server. I will experiment to
        find out if a greater CInt makes it slow (like 100 MB download).
        >
        -Rich
        Rich,

        Changing the buffer size may solve your problem for the moment, but it
        is not really a good long term solution... The standard way of doing
        this is to, as I said to do multiple reads until you get the right
        amount of data. If you look at most protocols, there are indicators
        that tell you how much data to expect or an end-of-transmission marker.

        --
        Tom Shelton

        Comment

        Working...