Jump to content

Tcp Client - Server Socket Communications


joecool2005

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...