|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
TCP Transfer issuesThe 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. Rich wrote:
> I am writing two programs that are part of a Bulletin board system. Hmmm, right about the size of the normal network buffer size....> 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? 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 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 wrote:
> I figured out an easy way to fix it, when I looked at the line: Rich,> 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 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
Why does activex control run 3x+ faster in vb5 than .net?
IsNumeric question Access dynamic controls by name? Format string in DataGrid How to turn off Managed Debugging Assistant in VB.NET (2005) OT-develing on several machines Replacing Double quotes with TWO Single Quotes Multiple TcpListeners on the same IP address From VB Newbie: The editor is driving me nuts. Windows Service not starting automatically |
|||||||||||||||||||||||