joecool2005 Posted May 29, 2009 Share Posted May 29, 2009 Hi,I try to do a communication by using TCP over IP sockets.This is my code. Dim tcpClient As New TcpClient()tcpClient.Connect("127.0.0.1", 8000)Dim netStream As NetworkStream = tcpClient.GetStream()Dim sendBytes As [Byte]() = Encoding.UTF8.GetBytes("Is anybody there?")netStream.Write(sendBytes, 0, sendBytes.Length)Dim bytes(tcpClient.ReceiveBufferSize) As BytenetStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))Dim returndata As String = Encoding.ASCII.GetString(bytes)Response.Write(returndata) When I do Response.Write(returndata), it does not display all the string.Looks like the string is truncated.Why?thxjoe Link to comment Share on other sites More sharing options...
jesh Posted May 29, 2009 Share Posted May 29, 2009 You're only reading from 0 to ReceiveBufferSize. Is the data longer than the ReceiveBufferSize?Typically, when you work with sockets, you read the data in a while loop:http://msdn.microsoft.com/en-us/library/sy...px#snippetGroup Link to comment Share on other sites More sharing options...
joecool2005 Posted June 1, 2009 Author Share Posted June 1, 2009 Thank you for your answer.I have another problem. On those lines, looks like nothing is written. Dim netStream As NetworkStream = tcpClient.GetStream() Dim sendBytes As [Byte]() = Encoding.UTF8.GetBytes("Is anybody there?") netStream.Write(sendBytes, 0, sendBytes.Length) I did the read like this by using a loop and did not find the stuff that I've written If netStream.CanRead Then Dim returndata As StringBuilder = New StringBuilder() Dim bytes(tcpClient.ReceiveBufferSize) As Byte Dim numberOfBytesRead As Integer = 0 Do numberOfBytesRead = netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) returndata.AppendFormat("{0}",Encoding.ASCII.GetString(bytes,0,numberOfBytesRead)) Loop While netStream.DataAvailable Response.Write(returndata) End if Any idea? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.