Home All Groups Group Topic Archive Search About
Author
31 Oct 2006 12:38 AM
Rich
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(portNumber)
        tcpListener.Start()
        Try
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Dim networkStream As NetworkStream = tcpClient.GetStream()
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0,
CInt(tcpClient.ReceiveBufferSize))
Dim clientdata As String = Encoding.Unicode.GetString(bytes)
Dim responseString As String = TextBox1.Text
Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            tcpClient.Close()
            tcpListener.Stop()
        Catch e As Exception
        End Try
        GoTo H
    End Sub


        Try
Dim tcpClient As New System.Net.Sockets.TcpClient()
            tcpClient.Connect(ip, port)
Dim networkStream As NetworkStream = tcpClient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(TextBox2.Text)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0,
CInt(tcpClient.ReceiveBufferSize))
Dim returndata As String = Encoding.Unicode.GetString(bytes)
                Me.Close()
            Else
                If Not networkStream.CanRead Then
                    MsgBox("Data cannot be written.")
                    tcpClient.Close()
                Else
                    If Not networkStream.CanWrite 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.ToString)
            MsgBox(ex.Message)
        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.

Author
31 Oct 2006 5:11 AM
Tom Shelton
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
Author
31 Oct 2006 10:55 PM
Rich
I figured out an easy way to fix it, when I looked at the line:
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

The intellisence says :
tcpClient.Receive 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
Author
1 Nov 2006 2:16 AM
Tom Shelton
Rich wrote:
> I figured out an easy way to fix it, when I looked at the line:
> networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
>
> The intellisence says :
> tcpClient.Receive 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